Templating

Dynamic Templates for your content

Let's first clarify potential confusion: There are two types of templates. The ones used for generation with the cli-tool, and the view templates. Here we want to focus on creating dynamic view templates.

learn more about neoan3-apps/template

Note: Alternative template engines

What you will find here is based on the assumption you are using the default neoan3-apps/template package (installs with neoan3). You can easily hook up any template engine as well. As a matter of fact, you can inject your complete own render logic.

Passing variables to the template

component/MyComponent/MyComponent.ctrl.php


        <?php


        namespace Neoan3\Components;

        use Neoan3\Core\Unicore;

        class MyComponentController extends Unicore{


            function init()
            {

                $this->uni('myFrame')
                    ->addRenderParameter('headline', fn() => 'My variable')
                    ->addRenderParameter('content', fn() => 'This is some fine text')
                    // the hook-method accepts 2-3 arguments
                    // the first argument describes the targeted part of the page
                    // (possible values are 'header', 'main' & 'footer')
                    // the second argument provides the wanted template
                    // (here the file /component/MyComponent/myComponent.view.html)
                    // the third argument accepts variables as array
                    // (usually the data will directly come from a model / database)
                    ->hook('main', 'myComponent', [
                        'renderParam' => 'We might also declare render parameters here.'
                    ])
                    ->output();
            }
        }
    

component/MyComponent/myComponent.view.html


        <h1>{{headline}}</h1>
        <p>{{content}}</p>
        <p>{{renderParam}}</p>
    

Result:


        <h1>My variable</h1>
        <p>This is some fine text</p>
        <p>We might also declare render parameters here.</p>
    

A step further

While the full documentation for templating can be found at GitHub, let's look into an examples of advanced usage:


        <!--   assuming pass-in is ['numbers' => ['name' => 'one', 'name' => 'two'] ]     -->
        <h1>Numbers:</h1>
        <ul>
            <li n-for="numbers as number" n-if="number.name != 'one'">{{number.name}}</li>
        </ul>
    

Result:


        <!--   assuming pass-in is ['numbers' => ['name' => 'one', 'name' => 'two'] ]     -->
        <h1>Numbers:</h1>
        <ul>
            <li>two</li>
        </ul>