16

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?

DMTintner
  • 14,405
  • 5
  • 35
  • 32
  • Similar question - "Extending or including - what is better in Twig?": http://stackoverflow.com/questions/7051743/extending-or-including-what-is-better-in-twig – Isinlor Jul 08 '16 at 12:19

1 Answers1

23

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.

Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
  • 1
    ok thanks, but being my question is: being that embed allows flexibility, why not just always use it. even if your template does not require a flexible structure now, it might in the future and this would prevent you having to make that change – DMTintner Oct 31 '13 at 17:01
  • 1
    I updated my answer, You can use embed everywhere, It's a matter of design. I personally used to use embed only when needed. – Ahmed Siouani Oct 31 '13 at 21:22
  • thanks, can you confirm that there are no performance differences between the two though? – DMTintner Nov 01 '13 at 21:59
  • Yes, there's no major performance issues as it did the same job as the extend helper. Performance would be a serious constraint in case you're Embedding controllers calls inside your template. – Ahmed Siouani Nov 04 '13 at 16:36