P. Nick
P. Nick

Reputation: 991

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.

Upvotes: 1

Views: 461

Answers (1)

Matias Kinnunen
Matias Kinnunen

Reputation: 8540

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

Upvotes: 2

Related Questions