简体   繁体   English

Codeigniter TWIG:如何访问模板中的全局变量

[英]Codeigniter TWIG: How to access global variable in template

How can I access model functions in .twig templates? 如何访问.twig模板中的模型函数?

{% if($this->my_model->my_function("123")) %}
    Hello World
{% endif %}

This returns Unexpected character "$" 这将返回Unexpected character "$"

EDIT: 编辑:

I realised I can do something like this 我意识到我可以做这样的事情

$this->twig->addGlobal("my_function", $this->my_model->my_function("123") ? true : false)

But considering 123 can be anything, I don't know how I'd allow input of a parameter. 但是考虑到123可以是任何东西,我不知道如何允许输入参数。

Add the model as a global variable instead of the function: 将模型添加为全局变量而不是函数:

$this->twig->addGlobal("my_model", $this->my_model);

Then in Twig you can do ( "123" can be anything): 然后,您可以在Twig中进行操作( "123"可以是任何值):

{% if my_model.my_function("123") %}
    Hello World
{% endif %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM