We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

[SOLVED] Dynamically add Variables to Loop

Hi community!

I want to dynamically add metddata in the HTML-header. There are tags that have to be on every site. These are added in a default-parent-controller. But what about the child controllers? Currently I have a loop in the view an add the tags in the parent-controller in the initialize action via

$this->view->myMetaTags = $myMetaTags;

But when I want to add some tags in the child-controller the loop is already filled with the parent tags.

I thought about an eventsmanager on the view with the method beforeRender. I that an acceptable way and how is this done?

greetings Eike


Finally a solved this problem with an event manager. To the "view" in the DependencyInjector a added the following:

$di->set('view', function() use ($config) {
    $view = new \Phalcon\Mvc\View();
    $eventsManager = new Phalcon\Events\Manager();

    $eventsManager->attach("view:beforeRender", MyMetaData::getInstance());

return $view;
}, true);

The MyMetaData collects all the metainformations:

class MyMetaData {
    public $metaData = array();
    public static $instance = null;

     * @return MyMetaData
    public static function getInstance() {
        if (null === self::$instance) {
            self::$instance = new MyMetaData();
        return self::$instance;

    public function __construct() {}

     * @param array $metaData
    public function appendMetaData(array $metaData) {
        if (true === isset($metaData['name']) && true === isset($metaData['content'])) {
            $this->metaData[] = $metaData;

    public function beforeRender($event, $view) {
        $view->myMetaData = $this->metaData;

Then in the code I do the following:

MyMetaData::getInstance()->appendMetaData(array("name" => "fb:admins", "content" => "[email protected]"));