View previous topic :: View next topic |
Author |
Message |
omarv_r Smarty n00b
Joined: 08 Dec 2015 Posts: 4
|
Posted: Tue Dec 08, 2015 9:41 pm Post subject: Getting template's path and filename |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Dec 09, 2015 12:04 pm Post subject: |
|
|
How would that help you, if template storage is a database? |
|
Back to top |
|
omarv_r Smarty n00b
Joined: 08 Dec 2015 Posts: 4
|
Posted: Wed Dec 09, 2015 2:06 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Dec 10, 2015 4:43 am Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Dec 10, 2015 1:54 pm Post subject: |
|
|
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 |
|
omarv_r Smarty n00b
Joined: 08 Dec 2015 Posts: 4
|
Posted: Thu Dec 10, 2015 7:56 pm Post subject: |
|
|
AnrDaemon, thanks for your answer.
You have reason, I should not. But believe me: this data must be in the presentation tier
Thank you again. |
|
Back to top |
|
omarv_r Smarty n00b
Joined: 08 Dec 2015 Posts: 4
|
Posted: Thu Dec 10, 2015 8:43 pm Post subject: |
|
|
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 ) |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Dec 10, 2015 9:17 pm Post subject: |
|
|
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 |
|
|