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 template's path and filename

 
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
omarv_r
Smarty n00b


Joined: 08 Dec 2015
Posts: 4

PostPosted: Tue Dec 08, 2015 9:41 pm    Post subject: Getting template's path and filename Reply with quote

Hello, I am new in this forum.

I want to know if is possible to get the path and filename of the template that is being processed at a gave moment.

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


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Dec 09, 2015 12:04 pm    Post subject: Reply with quote

How would that help you, if template storage is a database?
Back to top
View user's profile Send private message
omarv_r
Smarty n00b


Joined: 08 Dec 2015
Posts: 4

PostPosted: Wed Dec 09, 2015 2:06 pm    Post subject: Reply with quote

Hello again.

I have a template that includes a subtemplate. In that subtemplate I call a custom Smarty function passing a filename as parameter (something like: {foo filename="../myfile.dat"} ) to be processed by the custom function.

My problem is that the filename could be relative to subtemplate's location. In that case, into the custom function, I can't read the file (ex. using get_file_content) because I only know which is the template's folder (not subtemplate).

Obviously, if I know where the subtemplate is located, then I could locate the file passed as a parameter even if it's path is relative.

Thanks.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Thu Dec 10, 2015 4:43 am    Post subject: Reply with quote

In the function plugin the template object is passed as 2nd parameter.
$template->parent->source->filepath will give you the filepath of the calling template.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Dec 10, 2015 1:54 pm    Post subject: Reply with quote

omarv_r wrote:
Hello again.

I have a template that includes a subtemplate. In that subtemplate I call a custom Smarty function passing a filename as parameter (something like: {foo filename="../myfile.dat"} ) to be processed by the custom function.

My problem is that the filename could be relative to subtemplate's location. In that case, into the custom function, I can't read the file (ex. using get_file_content) because I only know which is the template's folder (not subtemplate).

Obviously, if I know where the subtemplate is located, then I could locate the file passed as a parameter even if it's path is relative.

Thanks.

You should not mix business logic with presentation details.
Back to top
View user's profile Send private message
omarv_r
Smarty n00b


Joined: 08 Dec 2015
Posts: 4

PostPosted: Thu Dec 10, 2015 7:56 pm    Post subject: Reply with quote

AnrDaemon, thanks for your answer.

You have reason, I should not. But believe me: this data must be in the presentation tier Cool

Thank you again.
Back to top
View user's profile Send private message
omarv_r
Smarty n00b


Joined: 08 Dec 2015
Posts: 4

PostPosted: Thu Dec 10, 2015 8:43 pm    Post subject: Reply with quote

Hello U.Tews.

The correct answer for my case is: $template->source->filepath

Lets say that we have:

(1) c:\folder1\template.tpl.php

that includes:

(2) c:\folder1\folder2\sub_template.tpl.php (this last one calls the function)

Into the function:

$template->parent->source->filepath

is returning (1), whereas:

$template->source->filepath

returns (2)... what I need!

Thank you because without your answer I had not found the solution.

Thank to AnrDaemon too for his interest.

(And I apologize for my english, but I speak spanish Wink )
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Dec 10, 2015 9:17 pm    Post subject: Reply with quote

If this data should be in presentation tier, then pass the data (or path to the data) explicitly.
You are solving a nonexistent problem, I'm afraid.

Besides, string binary data in the templates directory does not sound right to me. Or if the data is not binary, may be just {include} it? Or rewrite it as a Smarty config file?
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