M
M
magary42016-12-15 11:35:01
symfony
magary4, 2016-12-15 11:35:01

When is a macro better and when is a custom twig function?

What is the best case to use?
I don’t understand the difference
, plus the same result can be achieved with a simple inclusion

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri, 2016-12-15
@magary4

a custom function will allow you to access other services in the system, receive data, and check something.
for macro and include, all data should be ready.
macros are more convenient than includes for small things, such as inputs, because they can be placed in a bunch in one file and the syntax is simpler.
but doing everything with macros is bad - they are loaded every time, unlike includes, which are loaded only when necessary.
UPD to sum up, I would recommend
1) use custom twig functions when you need some kind of complex logic or request data from the system, but I would not recommend it for generating html, simply because html in php is not good. but in a custom function, you can call render of another template, this is normal, it can just worsen the search for layout for front-end developers (in the case of inclusion, everything is obvious for them).
2) macros - for a bunch of small things that are used often and everywhere (inputs)
3) includes for other cases, that is, when the data is already there and you need to format it in html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question