Header Small Ahoy!

Calling Another Module From A View

With Trongate, you can easily load another module from within an ordinary view file. This can be achieved with just one line of code:

<?= Modules::run("second_modules/some_method") ?>

Why Would You Want To Do This?

There are a variety of situations where this type of functionality can be extremely useful. For example, let's assume you'd like to add a 'Stripe' payment button onto the page. When users click the 'Buy Now' button, they may see a pop-up appear on their screen, inviting the user to add payment details. The inner workings of such a feature are almost certainly complicated enough to warrant having a separate 'stripe' module.

an example of a pop-up payment modal

It's easy to imagine the benefits of having a separate 'stripe' module that's dedicated to handing everything to do with Stripe payments - including 'drawing' the pop-up payment modal.

If such a feature were in place then it could be called from any view file with just one line of code. For example:

calling a module from within a view file

Adding Arguments

When you call modules from view files, arguments can be easily passed into the methods via comma seperation. Here's an example of a variable called $name being passed as an argument:

<?= Modules::run("modules::welcome/greeting", $name) ?>

The equals symbol in the opening PHP short tag above is important. That's because the equals symbol tells PHP to echo (display) the output. Strictly speaking, we don't need to have an equals symbol there. Indeed, we don't even have to display any output whatsoever (perhaps you're calling a method that sends an email, for example). In any event, the code below (which uses long PHP tags) would have the same result as the one line of code shown above.

echo Modules::run("welcome/greeting", $name);