【问题标题】:Extending nested blocks, keep children扩展嵌套块,保留子级
【发布时间】:2016-08-12 08:13:28
【问题描述】:

扩展模板时,是否可以更改块的父级而不影响其嵌套的子级?我试过{$smarty.block.parent},但它显示的是外部父母的内容,而不是块本身。 Smarty 版本 3.1.12

parent.tpl

{block name="block_parent"}
    <div class="parent">
        <h1>Parent Title</h1>

        {block name="block_child"}
            <div class="child">Child</div>
        {/block}
    </div>
{/block}

child.tpl

{extends file="parent.tpl"}
{block name="block_parent"}
    <div class="changed--parent">
        <h1>Parent Title changed</h1>

        {block name="block_child"}
            {* something like this for example *}
            {$smarty.block.self} 
        {/block}
    </div>
{/block}

期望的输出:

<div class="changed--parent">
    <h1>Parent Title changed</h1>
    <div class="child">Child</div>
</div>

回复@sofl:

使用时的输出 {$smarty.block.parent}

<div class="changed--parent">
    <h1>Parent Title changed</h1>
    <div class="parent">
        <h1>Parent Title</h1> <!-- this is not desired to happen -->
        <div class="child">Child</div>
    </div>
</div>

使用时的输出 {$smarty.block.child}

<div class="changed--parent">
    <h1>Parent Title changed</h1>
    <!-- child div missing -->
</div>

【问题讨论】:

  • {$smarty.block.parent} 怎么样?
  • @sofl 如前所述“显示外部父级内容,而不是块本身”。我已将输出添加到问题中。
  • 对不起,我错过了。从文档中,我认为 {$smarty.block.child} 可能会有所帮助 - smarty.net/docs/en/language.function.block.tpl
  • 其实之前也试过了,没有任何输出。我认为它不像我预期的那样工作。再次编辑我的帖子,你会明白我的意思。
  • 我也有同样的问题。你解决了吗?

标签: html nested smarty smarty3


【解决方案1】:

不幸的是,没有解决方案,或者目前这在 smarty 中是不可能的。但是有一种解决方法,至少在我的情况下有效:

{block name="parentBlockName"}
    {capture name="parentBlockName"}
        {$smarty.block.parent}
    {/capture}
    {$smarty.capture.parentBlockName|replace:'thing_to_change':'how_it_should_be'}
{/block}

这将替换您父级中的内容,同时保留子级。尝试尽可能具体,以确保没有错误被替换。我已使用此解决方法来更改父元素中的类。

【讨论】:

    猜你喜欢
    • 2012-02-01
    • 1970-01-01
    • 2010-12-13
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    相关资源
    最近更新 更多