Twig embed vs include, any reason to use include Twig embed vs include, any reason to use include symfony symfony

Twig embed vs include, any reason to use include


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.