Reputation: 1457
Is it real to get block content from parent file to child?
For example, I have 3 files parent.tpl
& first-child.tpl
& second-child.tpl
Parent contains that code:
<div>
{block name='greetings'}{/block}
</div>
First-Child contains:
{extends file="parent.tpl"}
{block name='greetings'}
<h1>Hello! I'm child</h1>
{/block}
Second-Child contains same as first greetings block (same text only), but instead of h1
tag, second-child use h4
tag:
{extends file="parent.tpl"}
{block name='greetings'}
<h4>Hello! I'm child</h4>
{/block}
I don't want to duplicate that text Hello! I'm child
in each child, but put this into own block.
It can be done that way: Create new file greeting_text.tpl
and include them into each child, but I don't want to allocate for that own file. I tried to declare that text into parent.tpl
:
...
{block name='greetings_text'}Hello! I'm child{/block}
And get that from children:
{extends file="parent.tpl"}
{block name='greetings'}
<h1>{block name='greetings_text'}{/block}</h1>
{/block}
and second-child:
{extends file="parent.tpl"}
{block name='greetings'}
<h4>{block name='greetings_text'}{/block}</h4>
{/block}
But I understood it's impossible to take block
content from parent to child. Or maybe i'm wrong.
Please give me advice, how I can do that except put greeting_text
into their own file and include them in children? Thanks!
EDITED:
Sorry, previous question text was a little bit incorrect (not fully describe what I'm trying to do)
How wrote sofl, it's work with append
construction. But problem is, that i have 3 files, and two of them extendable (layout.tpl, default.tpl and 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}
I launch page.tpl
($smarty->display('page.tpl')
), and i wanted to get menu
from default.tpl
to each child (page.tpl
, p.s. there can be many child page1.tpl, page2.tpl,... But for all of them, needed to put default menu from default.tpl
) And put that all to layout.tpl
like a content
. So how i can do that with block
construction? Please, any advices.
Upvotes: 1
Views: 1864
Reputation: 1024
Use prepend
or append
to keep the content of the parent block. 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}
UPDATED ANSWER
It seems abit tricky. You might use more than one block content
. The block menu
in default.tpl
has no placement.
Some possibilities
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}
I don't know much about the background of your project but personally I would not add too much structure to page.tpl
. So my alternate would look like so
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}
This doesnt exactly answers your question but may help I think.
Upvotes: 2