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

Absolute path doesn't work

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


Joined: 28 Sep 2011
Posts: 2

PostPosted: Wed Sep 28, 2011 5:53 pm    Post subject: Absolute path doesn't work Reply with quote

Hi,

I updated Smarty to version 3.1.1 and suddenly all of my pages don't work.

Previously the following syntax worked:

$smarty->fetch('/page.tpl');

Now it doesn't. I've noticed that removing leading / from the path makes it works.

Also paths used in {include} and {extends} have the same issue.

Can anybody knows how to turn on backward compatibility?
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Wed Sep 28, 2011 6:16 pm    Post subject: Reply with quote

As of Smarty 3.1 /foo.tpl means /foo.tpl - / being root, being the root of your system. Absolute paths are, as the name says, absolute. thus they're not relative to a template_dir. if you want that, ./foo.tpl is your friend (as of Smarty 3.1.1).

As of Smarty 3.1.1 you've got the following options:

Code:
$smarty->fetch("foo.tpl"); // translates to $template_dir/foo.tpl
$smarty->fetch("./foo.tpl"); // translates to $template_dir/foo.tpl
$smarty->fetch("../foo.tpl"); // translates to $template_dir/../foo.tpl
$smarty->fetch("/foo.tpl"); // translates to /foo.tpl


This applies to fetch() and include() likewise. From within the template, {include} and {extends} allow:

Code:
{include "foo.tpl"} {* translates to $template_dir/foo.tpl *}
{include "./foo.tpl"} {* translates to $current_template_path/foo.tpl *}
{include "../foo.tpl"} {* translates to $current_template_path/../foo.tpl *}
{include "/foo.tpl"}  {* translates to /foo.tpl *}


There is no backward compatibility flag for this.

a simple regular expression should help you find the spots you need to adjust:
Code:
\{(include|extends).+?["']/
and
Code:
->(fetch|display)\(\s*["']/

should find most if not all occurrences.
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Sep 28, 2011 7:41 pm    Post subject: Re: Absolute path doesn't work Reply with quote

yew wrote:
Hi,

I updated Smarty to version 3.1.1 and suddenly all of my pages don't work.

Previously the following syntax worked:

$smarty->fetch('/page.tpl');

Now it doesn't. I've noticed that removing leading / from the path makes it works.

Also paths used in {include} and {extends} have the same issue.

Can anybody knows how to turn on backward compatibility?


I think you may have uncovered an unfortunate bug present in Smarty 3.0, which was corrected in 3.1.

Code:
$smarty->fetch('/page.tpl');


This should indeed load the template from the system root. Smarty 2 behaved this way, and Smarty 3.1 also behaves this way. (I believe) Smarty 3.0 incorrectly loaded the template from the template_dir root. This behavior would carry over to {include ...} and {extends ...}. You must use one of the following:

Code:
$smarty->fetch('page.tpl');

Code:
$smarty->fetch('./page.tpl');




Smarty 2 also handled the ./ syntax OK within display() and fetch(), as does 3.1. The ../ syntax is new and does not make much sense to use with fetch() or display(), but comes in handy with {include ...}
Back to top
View user's profile Send private message Visit poster's website
yew
Smarty n00b


Joined: 28 Sep 2011
Posts: 2

PostPosted: Wed Sep 28, 2011 9:37 pm    Post subject: Reply with quote

Thanks for the answers. When I swap
Code:
$smarty->fetch('/page.tpl')

to
Code:
$smarty->fetch('page.tpl')

it works.
But this one doesn't:
Code:
$smarty->fetch('./page.tpl')


Any idea, why? (./ works in {include} and {extends} but not in fetch method).
Back to top
View user's profile Send private message
oschonrock
Smarty Rookie


Joined: 08 Aug 2003
Posts: 13

PostPosted: Mon Oct 03, 2011 2:57 pm    Post subject: using ../subdir/template.tpl from fetch Reply with quote

smarty->fetch('../subdir/template.tpl from fetch');

we use this a lot to mean:

"find a template relative to the current template_dir..but 'up and across' into a another subdir"

upgrading to 3.1.x breaks this. What do you suggest as an alternative syntax? This?

smarty->fetch('./../subdir/template.tpl from fetch');

will that work?
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Mon Oct 03, 2011 3:03 pm    Post subject: Reply with quote

I'm not so sure I see the necessity for "../subdir". From where I stand this looks like an ugly hack, considering you _are_ able to register multiple directories with $template_dir. Since 3.1 you're even able to access them specifically:
Code:
$smarty->setTemplateDir(array(
 'group1' => 'templates/group1',
 'group2' => 'templates/group2',
 'base' => 'templates/base',
));

$smarty->fetch('foo.tpl'); // checks templates/group1/foo.tpl then templates/group2/foo.tpl then templates/base/foo.tpl
$smarty->fetch('[group2]foo.tpl'); // checks templates/group2/foo.tpl only


Anyways, the current SVN trunk has a fix for your issue. Use the trunk, or wait for the (soon to come?!) Smarty 3.1.2
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
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 -> Bugs 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