L
L
lamer2282019-06-03 00:00:39
Twig
lamer228, 2019-06-03 00:00:39

How to add custom twig tag?

Good day.
How to add custom twig tag?
There are standard {{ - }} and {% - %}, but is it possible to add your own, like {dev - dev}?
Or how to do the following:
There is a tag, for example - {{ dev.XXX }} - instead of XXX - methods of a certain php class that return some data.
You can add each one separately, but in a bastard :) Is
it possible somehow automatically? So that all class methods are accessible by such an appeal

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sts, 2019-06-03
@stunoff

https : //symfony.com/doc/current/templating/global_... it is
for symfony, but the essence is the same

namespace App\Service;

class TwigGlobals
{
    public function getInfo()
    {
        return [
            'var_one' => 1,
            'var_two' => 2,
        ];
    }
}

test.html.twig
{% block a %}

    {{ getMainInfo().var_one }}

{% endblock %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question