Twig 嵌入与包含,任何使用包含的理由 [英] Twig embed vs include, any reason to use include

查看:21
本文介绍了Twig 嵌入与包含,任何使用包含的理由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不总是使用嵌入?

Why not just always use embed?

根据文档,嵌入标签结合了包含和扩展标签的行为.

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

我知道在很多情况下您不需要扩展而只想呈现另一个模板的内容,但在我看来,您可以始终使用 embed 并达到此目的.嵌入是否有任何性能影响需要注意?我遗漏了任何功能差异?

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?

推荐答案

我将总结 嵌入包括如下,

  • 两者都添加到了 twig 中,以使 HTML 代码片段可重复使用.
  • 两者都用于模板的功能分离(页脚、页眉...)
  • 两者都是动态的.
  • Include 用于包装具有非灵活 HTML 结构的代码.
  • 嵌入提供了灵活性.
  • 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.

根据差异,Include应该只使用 当您需要将模板拆分为许多功能子模板并在其他地方重用包装的代码时.

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.

虽然 Embed 用于自定义可重用模板.

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.

这篇关于Twig 嵌入与包含,任何使用包含的理由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆