沃梦达 / 编程问答 / php问题 / 正文

树枝嵌入与包含,任何使用包含的理由

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.

我知道,在很多情况下,您不需要进行扩展,而只想渲染另一个模板的内容,但在我看来,您总是可以使用 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?

推荐答案

总结一下Embed 和 Include 如下,

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

  • 两者都被添加到 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.

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

本文标题为:树枝嵌入与包含,任何使用包含的理由