View previous topic :: View next topic |
Author |
Message |
1234ru Smarty Rookie
Joined: 27 Jun 2019 Posts: 7
|
Posted: Sat Jun 29, 2019 5:13 am Post subject: Relative paths in {include file="..."} |
|
|
Is it possible to instruct {include} directive to interpret the path relative to the current template (where it was invoked)?
Let's say I have a module with certain structure of templates.
It's main template always includes another template from the same folder. When trying to invoke it without path like:
Code: | {include file="subtemplate.tpl"} |
I get an error "Unable to load template". I have to specify the full path like
Code: | {include file="/path/to/module/subtemplate.tpl"} |
How is this done in a proper way?
Last edited by 1234ru on Sat Jun 29, 2019 6:05 am; edited 1 time in total |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sat Jun 29, 2019 5:52 am Post subject: |
|
|
Maybe Smarty's $template_dir array does not include where the main template is located. An element of $template_dir may actually be that location's parent folder.
See setTemplateDir() and addTemplateDir(), then read about Resources. |
|
Back to top |
|
1234ru Smarty Rookie
Joined: 27 Jun 2019 Posts: 7
|
Posted: Sat Jun 29, 2019 7:02 am Post subject: |
|
|
The module's main template isn't main for Smarty instance. It is included as a subtemplate itself, like:
Code: | {include file="module/main.tpl"} |
And in module/main.tpl I want to use paths relative to it, not to global $template_dir.
Is it somehow possible? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sat Jun 29, 2019 10:05 pm Post subject: |
|
|
We can learn the path of the directory of the template Smarty is currently working on, and use it as the prefix to where we want to go.
/templates/index.tpl:
Code: | <html>
<head>
<title>Smarty</title>
</head>
<body>
<p>Hello,
{include file="{$smarty.current_dir}/data/data.tpl"}
</p>
</body>
</html> |
/templates/data/data.tpl:
Code: | {$name}!<br/>
{include file="{$smarty.current_dir}/subdata/subdata.tpl"} |
/templates/data/subdata/subdata.tpl:
Code: | {$smarty.current_dir}/{$smarty.template}: {"Boo!"} |
Output:
Code: | Hello, Ned!
E:\Inetpub\wwwroot\smarty\templates\data\subdata/subdata.tpl: Boo! |
I also experimented with the shortcut for current directory (the period):
Code: | {include file="./data/data.tpl"}
{include file="./subdata/subdata.tpl"} | which worked for me.
Last edited by bsmither on Sun Jun 30, 2019 2:37 pm; edited 1 time in total |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Jun 30, 2019 8:14 am Post subject: |
|
|
And I've read that article twice. >.> Thanks bsmither. |
|
Back to top |
|
1234ru Smarty Rookie
Joined: 27 Jun 2019 Posts: 7
|
Posted: Mon Jul 01, 2019 12:50 pm Post subject: |
|
|
bsmither, excellent! That is exactly what I was looking for!
{$smarty.current_dir}/{$smarty.template} even allows me to call a template from itself (useful when working with nested structures like submenus) which I was going to ask about. Thank you!
AnrDaemon wrote: | And I've read that article twice |
What article are you talking about? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Jul 01, 2019 4:27 pm Post subject: |
|
|
You shouldn't be using "{$smarty.template}". Why don't you just use template name straight away?
And article - the one he mentioned - I missed the reference to current_dir. My bad.
And of course the use of "./tempate" was creative. |
|
Back to top |
|
1234ru Smarty Rookie
Joined: 27 Jun 2019 Posts: 7
|
Posted: Mon Jul 01, 2019 4:47 pm Post subject: |
|
|
Quote: | You shouldn't be using "{$smarty.template}". Why don't you just use template name straight away? |
Because I need to call a template from itself to create a nested menu. Like:
Code: |
<div class="menu-item">
<a href="{$item.url*}">{$item.title}</a>
{if $item.submenu}
{foreach from=$item.submenu item=subitem}
{include file="{$smarty.current_dir}/{$smarty.template}" item=$subitem}
{/foreach}
{/if}
</div>
|
|
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Mon Jul 01, 2019 5:02 pm Post subject: |
|
|
I think that argument says that we cannot not know the filename of this template, so use it.
But, the way I see it, if the variable is made available, it is either a crucial property that has enormous value, or it is a mere convenience. Both of which is more than being stupidly superfluous or possibly some other reason to not use it.
When this code gets refactored/re-purposed into another file, one does not need to go through it changing the value of the file attribute of the {include} tag to suit its placement. The code will just work anywhere. |
|
Back to top |
|
|