Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig embed vs include, any reason to use include

Tags:

php

twig

symfony

Why not just always use embed?

According to the docs the embed tag combines the behavior of the include and extend tags.

I understand that in a lot of cases you don't need to do extend and only want to render the contents of another template, but it seems to me like you could just always use embed and serve that purpose. Are there any performance ramifications of embed to be aware of? Any functionality differences I'm missing?

like image 226
DMTintner Avatar asked Oct 31 '13 15:10

DMTintner


1 Answers

I would summarize the differences and similarities between Embed and Include as follow,

  • Both were added to twig to make HTML code snippets reusable.
  • Both are used for functional separation of templates (footer, header, ...)
  • Both are dynamic.
  • Include is used to wrap code that has non flexible HTML structure.
  • Embed allows flexibility.

Based on the differences, Include should be used only when you need to split a template into many functional sub-templates and reuse the wrapped code elsewhere.

While Embed is used when you need to customise the reusable templates.

So, it's more a matter of design than anything else (performance, code execution, ...)

Choose the helper that fit your needs.

like image 175
Ahmed Siouani Avatar answered Oct 13 '22 19:10

Ahmed Siouani