0

I am attempting to create a template using Smarty for php. I would like the template to be reusable so I inheriting it and substituting values using blocks. This works fine except when I need to access these block values in sub-templates. I am calling these sub-templates using smarty's {include} function.

Example:

Template-Parent (template.tpl):

<html>
     <head>
         {include file=sub_template.tpl}
     </head>

     <body>
         {block name=title}No Title Provided{/block}
     </body>
</html>

Sub-Template (sub_template.tpl)

     <title>{block name=title}No Title Provided{/block}</title>

Template-Child (template_child.tpl)

     {extends file="template.tpl"}
     {block name=title}My Website!{/block}

When I view the site, the output is:

<html>
     <head>
         <title>No Title Provided</title>
     </head>

     <body>
         My Website!
     </body>
</html>

After doing some reaserch I did see a note on smarty's website about enclosing {include} functions in dummy {block} tags but have had varied levels of success getting this to work.

Note: If you have a subtemplate which is included with {include} and it contains {block} areas it works only if the {include} itself is called from within a surrounding {block}. In the final parent template you may need a dummy {block} for it.(http://www.smarty.net/docs/en/advanced.features.template.inheritance.tpl)

Due to this, I have tried:

<html>
     <head>
         {block name=dummy}{include file=sub_template.tpl}{/block}
     </head>

     <body>
         {block name=title}No Title Provided{/block}
     </body>
</html>

This seems to work until I make any change to sub-template. As soon as a change to the sub-template is made, it once again stops responding to the block values set in the child.

Have I misunderstood what the notice was referring to about placing the {include} in dummy block tags or is this a bug with smarty? I am not currently using caching but my other thought was that maybe this is a sub-template caching problem.

Any insight would be greatly appreciated, thanks!

Krejko
  • 901
  • 1
  • 9
  • 23
  • I am thinking it may in fact be a caching problem. I cleared the 'templates_c' folder in the smarty directory and this causes the block values to work as expected in the sub_template file. However, once the sub_template is changed, it once again stops finding values in the blocks. Is there any elegant solution that does not involve clearing the cache manually after each change? – Krejko Sep 10 '12 at 09:04

1 Answers1

0

It is true that these templates are compiled and cached into the templates directory. This is why clearing the templates directory temporarily fixed the issue. The template caching is different than the explicit caching that smarty also supports. I have found that the template caching can be overridden with:

$smarty->compile_check = true;
$smarty->force_compile = true;

This allows changes to the template to be made with out needing to delete the cache after change while in development.

Krejko
  • 901
  • 1
  • 9
  • 23