简体   繁体   中英

Twig embed vs include, any reason to use include

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM