We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon Box / Helper / Partial ? How to create reusable Box in Phalcon?

Hello guys,

Is there any posibility in building view helper to render a html from a template (kind of dynamic partial)? I would like to have a box on my page, that is reading data from database and showing it under content pf the main controller.

edited Jul '14

I try to use Helpers to show my box. Here is how I try to do it, but it doesn't work:

My helper class: https://pastie.org/9412056

Calling helper in view: <?php echo TM\ViewHelpers\HityDnia::generujHityDnia(); ?>

$view->getContent() returns an empty string, where Index/index (Controller/action) normally returns a list;

What I did wrong?



98.9k

Try using Phalcon\Mvc\View\Simple instead of Phalcon\Mvc\View: https://docs.phalcon.io/en/latest/reference/views.html#id2

But If I have view component set in app/confng/services.php

/**

  • Setting up the view component */ $di->set('view', function () use ($config) {

    $view = new View();

    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array( '.volt' => function ($view, $di) use ($config) { $volt = new VoltEngine($view, $di);

        $volt->setOptions(array(
            'compiledPath' => $config->application->cacheDir,
            'compiledSeparator' => '_'
        ));
    
        return $volt;
    },
    '.phtml' => 'Phalcon\Mvc\View\Engine\Php'

    ));

    return $view; }, true);

Can I use it in helper?? How to get this in helper?

edited Jul '14

I've used my Di in helper, but calling helper override controllers view ;/

<?php

namespace TM\ViewHelpers;

use TM\Models\NProgramEmisja;

class HityDnia extends \Phalcon\Tag {

private $_di;

public function generujHityDnia()
{
    $emisjaModel = new NProgramEmisja();
    $emisje = $emisjaModel::find(array('limit' => '10'));
    return $this->przygotuj_widok(array('emisje' => $emisje), 'Index', 'index');
}

public function przygotuj_widok($params, $controller, $action)
{
    $this->_di = $this->getDI();
    return $this->_di->get('view')->render($controller, $action, $params)->getContent();
}

}

Using Phalcon\Mvc\View\Simple doesn't help ;/ me cause i have templates in *.volt - it doesn's see them ;/