Search code examples
phpsmartyextends

Smarty extends not works


My inheritance in Smarty doesn't work,

These are the template files:

./views/parent.tpl ./views/modules/child.tpl

So this is my child.tpl =

/* child.tpl */
{extends file='../parent.tpl'}
{block name='contents_accueil'}

<article>
<p>Something here</p>
</article>

{/block}

And my parent.tpl :

<div>
<p>Something else</p>
   {block name='contents_accueil'}{/block}
</div>

Why does it not work? It does not include my child.tpl file.

Thanks

The file php which calls the parent.tpl

require_once('application/librairies/tpl/Smarty.class.php');
require_once('config.inc.php');

$data=array();
$smarty=new Smarty();

if(isset($_GET['page'])) {
    $current_page=$_GET['page'];
} 

$data = (isset($current_page)) ? $_PAGES[$current_page] : $data=HOME_PAGE;

$smarty->assign('data', $data);
$smarty->display('./application/views/parent.tpl');

Solution

  • As sofl said, you got smarty template inheritance wrong. You have to display the child.tpl, not the parent, because the parent can be used for multiple childs, i.e. child2.tpl will look like:

    {extends file='../parent.tpl'}
    {block name='contents_accueil'}
    
    <article>
    <p>Something completely different here</p>
    </article>
    
    {/block}
    

    As you see, childs are the only ones that have all the information. If you just display parent.tpl, smarty doesn't have any clue about what file use as child. Think of {extends} as a container include