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

Getting parent template path in {extends} context

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
roadster
Smarty n00b


Joined: 18 Nov 2013
Posts: 4
Location: Toulouse, France

PostPosted: Mon Nov 18, 2013 2:01 pm    Post subject: Getting parent template path in {extends} context Reply with quote

Hello,

I'm currently working on a Smarty plugin which processes assets (css, js, etc.). I'm currently using $template->source->filepath in my plugin to get the current template path, and get the absolute template path.

However, when this template extends another template using the {extends} function, I'd like to get the *parent* template path. For example :

if child.tpl extends main/father.tpl, I'd like to get the absolute path of main/father.tpl

Is there a way to get this parent template path ?

Thanks in advance for your replies.
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 18, 2013 8:10 pm    Post subject: Reply with quote

Template inheritance is processed at compile time and the content of childs and parent is merged in one compiled file. During this process the parent file path is not stored.

At $template->properties['file_dependency'] you can access an array which does contain information about all loaded templates. I don't now if this does help you.

Note also that this in an internal structure which may change in future releases.
Back to top
View user's profile Send private message
roadster
Smarty n00b


Joined: 18 Nov 2013
Posts: 4
Location: Toulouse, France

PostPosted: Mon Nov 18, 2013 10:13 pm    Post subject: Reply with quote

Thanks for this reply.

I noticed in the template PHP source the file_dependency array, and found the parent template path, which seems talways appear in second position in the file dependency list. But could I rely on this position to code my plugin ? I'm not sure.

I've looked at the code of the internal Smarty_Internal_Compile_Extends class, and it seems possible to store the parent template in a template property, such as:

Code:
$compiler->template->source->template = $_template


It's working without further changes in Smarty code, as Smarty_Internal_Resource_Extends already uses the template attribute.

Could it be a safer way to access the parent template from a plugin ?
Back to top
View user's profile Send private message
roadster
Smarty n00b


Joined: 18 Nov 2013
Posts: 4
Location: Toulouse, France

PostPosted: Wed Nov 20, 2013 6:54 pm    Post subject: Reply with quote

Any advice about the suggestion above ?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Nov 20, 2013 7:12 pm    Post subject: Reply with quote

Quote:
$compiler->template->source->template = $_template


Will not work because this structure is only existing during compilation.

But I'm quite sure that the parent will always be the second entry in the 'file_dependency' array.
Back to top
View user's profile Send private message
roadster
Smarty n00b


Joined: 18 Nov 2013
Posts: 4
Location: Toulouse, France

PostPosted: Tue Nov 26, 2013 3:57 pm    Post subject: Reply with quote

Thanks for this reply Smile
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 -> Plugins 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