Twig 使用常量作为属性
Twig use constant as attribute
我想在 twig 模板中将常量作为属性传递。
代码如下所示:
{% if presenter.hasErrors(constant('Admin\App\ViewPresenters\EditorPresenter::FORM_ERRORS') )%}
并抛出错误 Message: Argument 1 passed to Admin\App\ViewPresenters::hasErrors() must be of the type string, null given, called in /vendor/twig/twig/src/Extension/CoreExtension.php on line 1527
,据我所知,twig 没有识别出它是一个常量。有什么wat可以在twig中传递常量作为参数吗?
在 twig
中,反斜杠用于转义特殊字符,例如{{ '\'' }}
将输出 '
。因此,要在 twig
中创建一个反斜杠,您需要 "escape" 反斜杠
{{ constant('Admin\App\ViewPresenters\EditPresenter::FORM_ERRORS') }}
你可以看到输出源码的区别twigfiddle
:
{{ constant('Admin\App\ViewPresenters\EditPresenter::FORM_ERRORS') }}
PHP 来源
echo twig_escape_filter($this->env, twig_constant("AdminAppViewPresentersEditorPresenter::FORM_ERRORS"), "html", null, true);
{{ constant('Admin\App\ViewPresenters\EditorPresenter::FORM_ERRORS') }}
PHP 来源
echo twig_escape_filter($this->env, twig_constant("Admin\App\ViewPresenters\EditorPresenter::FORM_ERRORS"), "html", null, true);
我想在 twig 模板中将常量作为属性传递。
代码如下所示:
{% if presenter.hasErrors(constant('Admin\App\ViewPresenters\EditorPresenter::FORM_ERRORS') )%}
并抛出错误 Message: Argument 1 passed to Admin\App\ViewPresenters::hasErrors() must be of the type string, null given, called in /vendor/twig/twig/src/Extension/CoreExtension.php on line 1527
,据我所知,twig 没有识别出它是一个常量。有什么wat可以在twig中传递常量作为参数吗?
在 twig
中,反斜杠用于转义特殊字符,例如{{ '\'' }}
将输出 '
。因此,要在 twig
中创建一个反斜杠,您需要 "escape" 反斜杠
{{ constant('Admin\App\ViewPresenters\EditPresenter::FORM_ERRORS') }}
你可以看到输出源码的区别twigfiddle
:
{{ constant('Admin\App\ViewPresenters\EditPresenter::FORM_ERRORS') }}
PHP 来源
echo twig_escape_filter($this->env, twig_constant("AdminAppViewPresentersEditorPresenter::FORM_ERRORS"), "html", null, true);
{{ constant('Admin\App\ViewPresenters\EditorPresenter::FORM_ERRORS') }}
PHP 来源
echo twig_escape_filter($this->env, twig_constant("Admin\App\ViewPresenters\EditorPresenter::FORM_ERRORS"), "html", null, true);