使用布局可以更轻松地做到这一点:https://docs.expressionengine.com/v2/templates/layouts.html
基本上,您将拥有一个包含基本模板的包装模板,并且您将来自另一个模板的内容提供给该模板。
这样,您只需使用一次 channel:entries 标签即可设置所有数据。
这是我设置变量的基本模板:
{layout="_partials/_wrapper"}
{exp:channel:entries
channel="pages"
disable="categories|pagination|member_data|relationships"}
{layout:set name="extra_header_content"}<script src="/assets/js/my_extra_script.js">{/layout:set}
{layout:set name="browser_title"}{browser_title}{/layout:set}
{layout:set name="seo_description"}{seo_description}{/layout:set}
{layout:set name="page_title"}{page_title}{/layout:set}
{layout:set name="body_content"}{body_text}{/layout:set}
{/exp:channel:entries}
看我在第一行嵌入布局模板。
我的包装模板如下所示:
<!doctype html>
<html class="no-js" lang="nl" dir="ltr">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{layout:browser_title}</title>
<meta name='description' content='{layout:seo_description}' />
<meta name="twitter:description" content="{layout:seo_description}" />
<meta property="og:description" content="{layout:seo_description}">
{layout:extra_header_content}
</head>
<body>
<h1>{layout:page_title}</h1>
{layout:body_content}
</body>
</html>
由于您使用的是 EE 2,因此您需要在其自己的字段组中为每个浏览器标题创建字段。这可能很乏味,但是如果您对字段命名有点逻辑,您可以使用 preload_replace https://docs.expressionengine.com/v2/templates/globals/preload_replacement.html 使您的模板更容易:
假设您有一个名为 news 的频道,将您的字段命名为“news_browser_title”,并为名为 pages 的频道创建一个名为“pages_browser_title”的字段
在您的模板中,您现在可以像这样使用它:
{layout="_partials/_wrapper"}
{preload_replace:channel="pages"}
{exp:channel:entries
channel="pages"
disable="categories|pagination|member_data|relationships"}
{layout:set name="extra_header_content"}<script src="/assets/js/my_extra_script.js">{/layout:set}
{layout:set name="browser_title"}{{channel}_browser_title}{/layout:set}
{layout:set name="seo_description"}{{channel}_seo_description}{/layout:set}
{layout:set name="page_title"}{{channel}_page_title}{/layout:set}
{layout:set name="body_content"}{{channel}_body_text}{/layout:set}
{/exp:channel:entries}
更新:
您可以将包装模板放在您喜欢的任何模板组中。我基本上有一个名为 _partials 的文件夹,其中包含我嵌入到其他模板中的所有模板。
假设您有这样的模板组设置:
_partials
-_wrapper
-_another_template
-_some_other_template
blog
-index
-item
news
-index
-item
在博客或新闻中的每个模板中,您都可以使用 {layout="_partials/_wrapper"} 嵌入相同的包装模板,因为它只需要模板组/模板名称作为输入。
如果您需要更多帮助,请前往 https://expressionengine.stackexchange.com/ 获取更具体的 EE 建议