Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Block functions in nested included templates behavior

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
scuzzy
Smarty Regular


Joined: 31 Aug 2003
Posts: 84

PostPosted: Thu May 10, 2018 11:54 pm    Post subject: Block functions in nested included templates behavior Reply with quote

Hello, sorry for all the questions but I'm migrating a old version of smarty to the current version and I'm having problems with a behavior Confused

I am trying to leverage {block name="header" append}...

child.tpl
Code:
{extends file="parent.tpl"}
{block name="header" append}
  <strong style="color:#00C000">[ child append to include #{$fooCounter} ]</strong>
{/block}

parent.tpl
Code:
{include file="include.tpl"}
<hr>
[ I am the body ]
{*block name="header" append}<strong style="color:#C00000">[ parent append to include #{$fooCounter} ]</strong>{/block*}

include.tpl
Code:
{extends file="include.parent.tpl"}
{block name="header" append}
  [ I am the include block #{$fooCounter} ]
{/block}

include.parent.tpl
Code:
{block name="header"}
  [ I am the include block parent #{$fooCounter} ]
{/block}

smarty.php
Code:
<?php

require 'vendor/autoload.php';

$smarty = new Smarty();
$smarty->assign( 'fooCounter', new FooCounter() );
$smarty->force_compile = true;
$smarty->display( 'child.tpl' );

class FooCounter
{
  public function __toString()
  {
    static $counter = 0;
    $counter++;
    return (string) $counter;
  }
}


Ok that's the setup, what I'm trying to do, is allow the child.tpl to append to the include.parent.tpl {block name="header"} block which works



However, as soon as I un-comment the {*block name="header" append}...{/block*} that is inside of parent.tpl, instead of appending to the include like child.tpl was, its now appended in the parent.tpl content.



What can I do to get both parent.tpl and child.tpl to append to the block in include.tpl/include.parent.tpl consistently?

child.tpl extends parent.tpl includes include.tpl extends include.parent.tpl

With thanks.
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1701

PostPosted: Fri May 11, 2018 12:43 pm    Post subject: Reply with quote

-{include file="include.tpl"}
+{extends file="include.tpl"}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1701

PostPosted: Fri May 11, 2018 2:28 pm    Post subject: Reply with quote

To clarify: Do not interleave {extends} and {include}. The results are hardly predictable.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP