gpt4 book ai didi

phalcon - 随处可访问的宏

转载 作者:行者123 更新时间:2023-12-03 16:05:25 26 4
gpt4 key购买 nike

我正在尝试像这样为我的模板创建宏:

{%- 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 %}

问题是它在 View 根目录的 macros.volt 文件中,我不知道如何或在哪里包含它,所以它随处可用。我尝试在根布局 (index.volt) 中使用 include 和 partial 函数,但仍然不起作用。甚至在我尝试使用它的模板文件中也不行。我做错了什么,如何解决?

另一件事是如何为数组中的某些键设置值。我显然尝试了 {% set attributes['class'] = 'form-control' %},但它不起作用。

最佳答案

很棒的解决方案,在 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;
});

关于phalcon - 随处可访问的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19222321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com