Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Issues with {include} and {extends}

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
elDekyfin
Smarty n00b


Joined: 31 Dec 2015
Posts: 2

PostPosted: Thu Dec 31, 2015 10:49 am    Post subject: Issues with {include} and {extends} Reply with quote

I'm having issues when different template files inherit from a file which uses {include}. Below is the summary of my code

header.tpl
Code:

<html>
<head>
<title>{$title}</title>
...
{block name="scripts"}{/block}
</head>
<body>
...


index.tpl
Code:

{include file="header.tpl"}
{block name="content"} ... {/block}
{include file="footer.tpl"}


page.tpl
Code:

{extends file="index.tpl"}
{block name="content"} ... {/block}


shop.tpl
Code:

{extends file="index.tpl"}
{block name="scripts"} /* custom css */ {/block}
{block name="content"} ... {/block}


Now the problem is that when a page uses "page.tpl", the following compiled files are created
***.file.page.tpl.php
***.file.header.tpl.php
***.file.footer.tpl.php

when another page later loads "shop.tpl", the /*custom css*/ in its scripts block does not show.

If "shop.tpl" is rather loaded before "page.tpl", /*custom css*/ which is unique to "shop.tpl" appears in the rendered "page.tpl".


How do I solve this?

Thank you
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Dec 31, 2015 5:11 pm    Post subject: Reply with quote

Short version is that templates that extend other templates should not contain directives that generate contents other than {block}.

index.tpl
Code:
<!DOCTYPE html>
<html>
<head>
<title>{$title}</title>
...
{block name="scripts"}{/block}
</head>
<body>
{include "page_header.tpl"}
{block name="content"} ... {/block}
{include "page_footer.tpl"}
</body></html>
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Jan 01, 2016 4:54 pm    Post subject: Reply with quote

Template inheritance was in Smarty versions < 3.1.28 a compile time process.
So {block name="scripts"}{/block} in header.tpl was replaced by child template {block} when it got compiled when shop.tpl or page.tpl what ever was first.

If you set
Code:
$smarty->merge_compiled_includes = true;
the subtemplate code for header.tpl and footer.tpl is compiled into page.tpl and shop.tpl compiled template and {block name="scripts"}{/block} is resolved correctly. See also the INHERITANCE_RELEASE_NOTES

Starting with 3.1.28 inheritance is a run time process which does remove this requirement and other restrictions.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    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