Question

I am trying to create macro for my templates like this:

{%- macro bField(form, name, attributes) %}
    <p class="form-group" ng-class="{has-error: !{{ form.name }}.{{ name }}.$valid}">
        {{ form.label(name) }}
        {#{% set attributes['class'] = 'form-control' %}#}
        {{ form.render(name, attributes) }}
        {% include 'forms/validation-messages.volt' %}
    </p>
{%- endmacro %}

The issue is it is in macros.volt file in views root and I dont have any idea how or where to include it so it is available everywhere. I tried in root layout (index.volt) with include and partial functions but still doesnt work. Not even in template file I am trying to use it in. What am I doing wrong, how to fix this?

Another thing is how do I set value on certain key in array. I obviously tried {% set attributes['class'] = 'form-control' %}, but it doesnt work.

Was it helpful?

Solution

Awesome Solution, found on the Phalcon Forums. Customized just a little to my situation.

Suggestion was to extend the Volt Engine Class and then load each macro file during \Phalcon\Mvc\View\Engine\Volt::getCompiler.

// extended class to load the macros before parse time
class VoltC extends \Phalcon\Mvc\View\Engine\Volt
{
    public function getCompiler()
    {
      if (empty($this->_compiler))
      {
        parent::getCompiler();

        // add macros that need initialized before parse time
        $this->partial("macros/form");
      }

      return parent::getCompiler();
    }
}
$di->set("voltEngine", function( $view, $di ){
    $volt = new VoltC($view, $di);

    $volt->setOptions(array(
        "compiledPath" => "../app/tmp/cache/",
        "compiledExtension" => ".cmp",
        'compileAlways' => true
    ));

    return $volt;
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top