View helpers

View helpers extend the View functionality adding a possibility to call you own logic inside html templates (view more details about views).

Config

The config helper allows you access to any registered configs. For example the main layout displays a site name as following:

// sourced from: src/Module/Base/view/layout/base.phtml

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title><?= $this->config('site')['name'] ?></title>
    </head>
    ...

You only need to pass a config key to get an access to its values. the config file itself:

<?php

    // sourced from: src/Module/Base/config.php

    return [
        'site' => [
            'name' => 'Test site'
        ],
        ...
    ];

Url

Using the the Url helper you can display a url, based on a some registered route. Lets check a look an example:

// sourced from: src/Module/Base/view/layout/base.phtml

 <body>
    <nav>
        <a href="<?= $this->url('UserController', 'list', 'User') ?>">Users</a>
    </nav>
    ...

To correctly build an url you need to pass there a controller name, a method and a module name. If the helper cannot resolve a route based on received parameters it triggers an Exception.

Partial View

Some times we need to use already created templates in other templates in order to reduce a duplicate code.

Lets consider an example:

We are going to display a user list in many places on the site. So first of all it would be great to create a user template:

// sourced from: src/Module/User/view/partial/user.phtml

<div>
    <b><?= $this->id ?></b>: <?= $this->name ?>
</div>

Now we are ready to use it everywhere.

// sourced from: src/Module/User/view/UserController/list.phtml

<ul>
    <?php foreach ($this->users as $user): ?>
        <li>
            <?= $this->partialView('partial/user', 'User',  $user) ?>
        </li>
    <?php endforeach ?>
</ul>

To use a partial view you need to pass there a path to the template file and a module name where that file is located. And the last parameter is the template’s variables.

Custom

Let’s implement a very simple custom helper for demonstration, let say It would return a random value (suppose it’s a CustomModule).

<?php

    namespace Tiny\Skeleton\Module\CustomModule\EventListener\ViewHelper;

    class ViewHelperRandomListener
    {
        /**
         * @param  Event  $event
         */
        public function __invoke(Event $event)
        {
            // we don't use any arguments in this example
            $arguments = $event->getParams()['arguments'];

            $event->setData(rand());
        }
    }

Then we need to register this listener class in configs:

<?php

    // Module/CustomModule/config.php

    use Tiny\Skeleton\Module\Base\EventListener;
    use Tiny\View\View;
    use Tiny\Skeleton\Module\CustomModule;
    use Tiny\ServiceManager\Factory\InvokableFactory;

    return [
        'service_manager' => [
            'shared' => [
                ...
                // we don't need any dependencies that's why we are using the "InvokableFactory"
                CustomModule\EventListener\ViewHelper\ViewHelperRandomListener::class  => InvokableFactory::class,
            ]
        ],
        'listeners' => [
            // view helper
            ...
            [
                'event'    => View::EVENT_CALL_VIEW_HELPER.'random',
                'listener' => EventListener\ViewHelper\ViewHelperRandomListener::class,
            ],
            ...
        ]
    ];

Now we can use it in templates, like:

<div>
    <b><?= $this->random() ?></b>
</div>