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

Relative paths in {include file="..."}

 
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
1234ru
Smarty Rookie


Joined: 27 Jun 2019
Posts: 7

PostPosted: Sat Jun 29, 2019 5:13 am    Post subject: Relative paths in {include file="..."} Reply with quote

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
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sat Jun 29, 2019 5:52 am    Post subject: Reply with quote

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
View user's profile Send private message
1234ru
Smarty Rookie


Joined: 27 Jun 2019
Posts: 7

PostPosted: Sat Jun 29, 2019 7:02 am    Post subject: Reply with quote

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
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sat Jun 29, 2019 10:05 pm    Post subject: Reply with quote

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:
&nbsp;{$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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Jun 30, 2019 8:14 am    Post subject: Reply with quote

And I've read that article twice. >.> Thanks bsmither.
Back to top
View user's profile Send private message
1234ru
Smarty Rookie


Joined: 27 Jun 2019
Posts: 7

PostPosted: Mon Jul 01, 2019 12:50 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Jul 01, 2019 4:27 pm    Post subject: Reply with quote

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
View user's profile Send private message
1234ru
Smarty Rookie


Joined: 27 Jun 2019
Posts: 7

PostPosted: Mon Jul 01, 2019 4:47 pm    Post subject: Reply with quote

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
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Mon Jul 01, 2019 5:02 pm    Post subject: Reply with quote

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
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