简体   繁体   中英

Codeigniter TWIG: How to access global variable in template

How can I access model functions in .twig templates?

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

This returns 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.

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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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