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

dynamic 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 -> Smarty 3
View previous topic :: View next topic  
Author Message
TimBroddin
Smarty n00b


Joined: 29 Nov 2010
Posts: 1

PostPosted: Mon Nov 29, 2010 2:21 pm    Post subject: dynamic extends Reply with quote

Hi there,

Thanks for the great work on Smarty. I love the new functionality in Smarty 3.

I'm working on a project where we have a number of parent templates and a number of child templates which we mix and match. I'm trying this:

{extends file=$extend_template}

But it does not seem to work because Smarty compiles the file dependencies and caches them in templates_c.

Does anyone know a workaround for this? At this moment I have turned force compile on but that's not the ideal solution.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Nov 29, 2010 2:28 pm    Post subject: Reply with quote

As block replacement in template inheritance is a compile time porcess you can't make dynamic extends by using assigned variables and the {extedns } tag.

Use the "extends" resource instead to define the parent/child template relation ships.
Back to top
View user's profile Send private message
starkj
Smarty n00b


Joined: 23 Nov 2015
Posts: 1

PostPosted: Mon Nov 23, 2015 9:17 am    Post subject: Reply with quote

hi,

just run into the same problem.

I want to set the template which is extended dynamically. Yes, this does not work in the first place since a template is complied once with the parent set at the first time. And disabling the cache is definitely a thing you don't want to do.

My template looks something like this:
Code:

{extends $parentTemplate}

{block name='content'}
foo
{/block


To make this work anyway, you can set a custom compileId for which I use the parent templates name:

Code:

$smarty->assign($parameters);
$smarty->setCompileId($parameters['parentTemplate']);
$xmarty->fetch('template.html.tpl');

or
Code:

$smarty->assign($parameters);
$smarty->fetch('template.html.tpl', null, $parameters['parentTemplate']);


This is meant to be a workaround and not best practice. The proper way to go is like already mentioned: Use the extends resource just to define parent/child relationship and then structure your templates in a way that you can do the rest by using blocks.

Regards
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 -> Smarty 3 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