3. Passing variables to templates

We have seen in 2. Hello World! how to create a static template. Let’s send some variables to have a more dynamic display.

src/Controller/MyModuleController.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php

namespace MyCompany\iTop\MyModule\Controller;
use Combodo\iTop\Application\TwigBase\Controller\Controller;
use UserRights;

class MyModuleController extends Controller
{
    public function OperationHelloWorld()
    {
        $aParams['sName'] = UserRights::GetUser();
        $aParams['sDate'] = date("r");
        $this->DisplayPage($aParams);
    }
}

The DisplayPage() method accept an array of parameters. This array is transformed into variables for the Twig template.

Here two variables are created: sName and sDate, we can use them in the template.

templates/HelloWorld.html.twig
1
2
3
4
{% UITitle ForPage {sTitle:'Hello ' ~ sName ~ '!'} %}{% EndUITitle %}
{% UIContentBlock Standard {DataAttributes: {role: 'date'}} %}
    We are currently {{ sDate }}
{% EndUIContentBlock %}

The output is then

../../_images/Hello2.png

The variables can be of any type, for example you can give an array as a variable:

src/Controller/MyModuleController.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php

namespace MyCompany\iTop\MyModule\Controller;
use Combodo\iTop\Application\TwigBase\Controller\Controller;
use UserRights;

class MyModuleController extends Controller
{
    public function OperationHelloWorld()
    {
        $aParams['sName'] = UserRights::GetUser();
        $aParams['sDate'] = date("r");
        $aParams['aQuarter'] = ['January', 'February', 'March'];
        $this->DisplayPage($aParams);
    }
}

Here aQuarter is an array containing some months, we can use it in a selector:

templates/HelloWorld.html.twig
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{% UITitle ForPage {sTitle:'Hello ' ~ sName ~ '!'} %}{% EndUITitle %}
{% UIContentBlock Standard {DataAttributes: {role: 'date'}} %}
    We are currently {{ sDate }}
{% EndUIContentBlock %}

{% UIForm Standard {sId:'my-form'} %}
    Select Month:
    {% UISelect ForSelect {sName:'month'} %}
        {% for index,sMonth in aQuarter %}
            {% UISelectOption ForSelectOption {sValue: index, sLabel: sMonth, bSelected:false} %}
        {% endfor %}
    {% EndUISelect %}
{% EndUIForm %}

The output is:

../../_images/Hello3.png