0
votes

Is there any way to create a view for custom tag in Phalcon so I can just pass the parameters to be rendered?

class MenuModule extends \Phalcon\Tag {
    public static function initialize($param) {
        return $param;
    }
}

In my view I can call

echo MenuModule::initialize('Home Page');

What I want to do is to pass array like:

$menu = array('Home','About','Contact');
echo MenuModule::initialize($menu);

And then in Tag Helper to call a subview to render that array instead of something like this:

class MenuModule extends \Phalcon\Tag {
    public static function initialize($param) {
        $menu = '<ul>';
        foreach($param as $p) {
            $menu .= '<li>' . $p . '</li>';
        }
        $menu .= '</ul>';
        return $menu;
    }
}

This is not that complex, but I want to use views instead of generating HTML inside PHP because of a larger HTML files.

How can I do this please?

1

1 Answers

0
votes

I found solution, Tag Service and Creating your own helpers on official Phalcon documentation.

<?php
use Phalcon\Tag;

class MenuModule extends Tag {
    static public function initialize($param) {
        $menu = '<ul>';
        foreach($param as $p) {
            $menu .= '<li>' . $p . '</li>';
        }
        $menu .= '</ul>';

        return $menu;
    }
}

Then change the definition of the service ‘tag’:

$di['tag'] = function () {
    return new MenuModule();
};

And then in volt access it like:

{{ MenuModule::initialize($param) }}