bigfork/silverstripe-template-yield

为Silverstripe模板添加yield功能

安装: 693

依赖项: 0

建议者: 0

安全性: 0

星标: 1

关注者: 4

分支: 0

开放问题: 0

类型:silverstripe-vendormodule

2.0.0 2022-07-01 15:18 UTC

This package is auto-updated.

Last update: 2024-08-29 05:56:36 UTC


README

为Silverstripe添加了对<% section %><% yield %>标签的支持。

⚠️ 这是一个通过使用HTTP中间件执行字符串替换来绕过缺失的扩展钩子构建的概念验证。有关更多信息,请参阅限制部分。

使用示例

您可以为当前渲染过程中任何模板中存在的标签提供的“yield”内容。

<!-- Page.ss -->
<head>
    <% yield 'MetaTags' %>
</head>

<!-- Includes/Pagination.ss -->
<% section 'MetaTags' %>
    <meta rel="next" href="{$NextLink}" />
    <meta rel="prev" href="{$PrevLink}" />
<% end_section %>

<div class="pagination">
    <ul>
        ...

您还可以提供回退内容

<head>
    <!-- If nothing includes a <% section 'MetaTitle' %>, the following fallback will be rendered -->
    <% yield 'MetaTitle' %>
        <title>Some default meta title</title>
    <% end_yield %>
</head>

还有内联(“打开”)标签,这对于提供如CSS类之类的yield内容非常有用

<!-- Page.ss -->
<body class="<% yield 'BodyClass' %>">

</body>

<!-- Layout/MyPage.ss -->
<% section 'BodyClass', 'some-css-class' %>

<div class="typography">
    ... etc
</div>

如上所述,您还可以为内联标签提供回退

<!-- Will return fallback-class if no <% section 'BodyClass' %> is defined -->
<body class="<% yield 'BodyClass', 'fallback-class' %>">

</body>

限制

由于Silverstripe核心中尚未设置合适的钩子,因此存在以下限制

  • 这仅适用于通过HTTP请求返回的模板,因为它依赖于HTTP中间件来注入yield内容。如果您需要用于其他内容(例如电子邮件或服务器端渲染),您需要自己调用BlockProvider::yieldIntoString()
  • 虽然嵌套部分/yield看起来可以工作,但这些还没有经过彻底测试