我正在尝试为我的模板创建宏,如下所示:
{%- 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 %}
问题是它是在视图根目录中的macros.volt文件中,我不知道如何或在何处包含它以便它随处可用。我尝试使用包含和部分函数的根布局(index.volt),但仍然无法正常工作。甚至在模板文件中我都试图使用它。 我做错了什么,如何解决这个问题?
另一件事是如何在数组中的某个键上设置值。我显然尝试{% set attributes['class'] = 'form-control' %}
,但它不起作用。
答案 0 :(得分:6)
真棒解决方案,可在Phalcon Forums找到。根据我的情况定制一点。
建议扩展Volt引擎类,然后在\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;
});