Smarty通用块代码继承

Smarty general block code inheritance

从parent文件中获取块内容到child是真的吗? 例如,我有 3 个文件 parent.tpl & first-child.tpl & second-child.tpl

Parent 包含该代码:

<div>
    {block name='greetings'}{/block}
</div>

First-Child 包含:

{extends file="parent.tpl"}
{block name='greetings'}
    <h1>Hello! I'm child</h1>
{/block}

Second-Child 包含与第一个问候语块相同的内容(仅相同的文本),但 second-child 使用 h4 标签而不是 h1 标签:

{extends file="parent.tpl"}
{block name='greetings'}
    <h4>Hello! I'm child</h4>
{/block}

我不想在每个 child 中复制该文本 Hello! I'm child,而是将其放入自己的块中。 可以这样做:创建新文件 greeting_text.tpl 并将它们包含到每个 child 中,但我不想分配给自己的文件。我试图将该文本声明为 parent.tpl:

...
{block name='greetings_text'}Hello! I'm child{/block}

然后从 children 得到:

{extends file="parent.tpl"}
{block name='greetings'}
    <h1>{block name='greetings_text'}{/block}</h1>
{/block}

和second-child:

{extends file="parent.tpl"}
{block name='greetings'}
    <h4>{block name='greetings_text'}{/block}</h4>
{/block}

但我知道不可能将 block 内容从 parent 提取到 child。或者我错了。 请给我建议,除了将 greeting_text 放入他们自己的文件并将它们包含在 children 之外,我该怎么做?谢谢!


已编辑:

抱歉,之前的问题文本有点不正确(没有完全描述我正在尝试做的事情)

如何写sofl,它与append结构一起工作。但问题是,我有 3 个文件,其中两个是可扩展的(layout.tpl、default.tpl 和 page.tpl)

layout.tpl

<div class="layout">
  {block name="content"}{/block}
</div>

default.tpl

{extends file="layout.tpl"}

{block name='menu'}
  <ul>
      <li>Link 1</li>
      <li>Link 2</li>
      <li>Link 3</li>
  </ul>
{/block}

page.tpl

{extends file="default.tpl"}

{block name="content"}
  <div class="first-page-example">
      {block name='menu' append}{/block}

      <span>I'm text only for first page....</span>
  </div>
{/block}

我启动了 page.tpl ($smarty->display('page.tpl')),我想从 default.tpl 到每个 child (page.tpl, p.s。可以有很多 child page1.tpl, page2.tpl,...但是对于所有这些,需要从 default.tpl) 中放入默认菜单并放入这一切 layout.tpl 就像 content。那么我如何使用 block 构造来做到这一点?请任何建议。

使用prependappend保留parent块的内容。 http://www.smarty.net/docs/en/language.function.block.tpl

parent

<div>
    {block name='greetings'}<h4>Hello! I'm child</h4>{/block}
</div>

child

{extends file="parent.tpl"}
{block name='greetings' append}
    <h5>...here I am</h5>
{/block}

更新的答案

这似乎有点棘手。您可能会使用多个块 contentdefault.tpl 中的块 menu 没有位置。

一些可能性

layout.tpl

<div class='layout'>
  {block name='content'}{/block}
</div>

default.tpl

{extends file='layout.tpl'}

{block name='content'}
    {block name='menu'}
      <ul>
          <li>Link 1</li>
          <li>Link 2</li>
          <li>Link 3</li>
      </ul>
    {/block}
    {block name='actualContent'}{/block}
{/block}

page.tpl

{extends file='default.tpl'}

{block name='menu'}
    <div class="first-page-example">
        {$smarty.block.parent}
    </div>
{/block}
{block name='actualContent'}
    <span>I'm text only for first page....</span>
{/block}

我不太了解你的项目背景,但我个人不会在 page.tpl 中添加太多结构。所以我的替代品看起来像这样

layout.tpl

<div class='layout'>
  {block name='content'}{/block}
</div>

default.tpl

{extends file='layout.tpl'}
{block 'content'}
    <header>
        <nav>
        {block 'menu'}
            <ul>
                <li>Link 1</li>
                <li>Link 2</li>
                <li>Link 3</li>
            </ul>
        {/block}
        </nav>
    </header>
    <main>
        {block 'actualContent'}comming soon...{/block}
    </main>
{/block}

page.tpl

{extends file='default.tpl'}
{block 'actualContent'}
    <article>I'm text only for first page....</article>
{/block}

这并不能完全回答您的问题,但我认为可能有所帮助。