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

Smarty 2 to 3: error with relative path

 
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
JoniJnm
Smarty Rookie


Joined: 17 Aug 2012
Posts: 6

PostPosted: Mon Aug 27, 2012 8:40 am    Post subject: Smarty 2 to 3: error with relative path Reply with quote

Hi,

With smarty 2, I can use relative path to fetch files:

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

But with the version 3, it can't load the template.

In smarty_resource.php

Code:
        // resolve relative path
        if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $file)) {
            // don't we all just love windows?
            $_path = str_replace('\\', '/', $file);
            $_was_relative_prefix = $file[0] == '.' ? substr($file, 0, strpos($_path, '/')) : null;
            $_path = DS . trim($file, '/');
            $_was_relative = true;
        }


$_path = DS . trim($file, '/');
converts '../mail/mail_begin.tpl' to '\../mail/mail_begin.tpl' and later, the normalizer replaces it to: '..\..\mail\mail_begin.tpl'

How can I use relative path with Smarty 3?

Thanks!
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 Aug 27, 2012 5:07 pm    Post subject: Reply with quote

Which version of Smarty are you using? PHP Version? Operating System?
(This works fine on my machine… Smarty 3.1.11, PHP 5.3.15, Mac)
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
JoniJnm
Smarty Rookie


Joined: 17 Aug 2012
Posts: 6

PostPosted: Mon Aug 27, 2012 7:15 pm    Post subject: Reply with quote

- Smarty 3.1.11
- Xampp 1.8.0 (php 5.4.4)
- Windows 7 x64

So, I don't know...

It works removing the lines
Code:
$_was_relative_prefix = $file[0] == '.' ? substr($file, 0, strpos($_path, '/')) : null;
$_path = DS . trim($file, '/');


But...

Thanks!


Last edited by JoniJnm on Tue Aug 28, 2012 8:25 am; edited 1 time in total
Back to top
View user's profile Send private message
JoniJnm
Smarty Rookie


Joined: 17 Aug 2012
Posts: 6

PostPosted: Tue Aug 28, 2012 8:24 am    Post subject: Reply with quote

My test

index.php
Code:
<?php

include('../../libs/smarty/SmartyBC.class.php');

$smarty = new SmartyBC;

$smarty->template_dir = __DIR__.'/tpls';
$smarty->compile_dir = __DIR__.'/compile';
$smarty->cache_dir = __DIR__.'/cache';

$smarty->assign("my_string", "Hi, this is a test: ".rand());
$smarty->display('../newfolder/test.tpl');


test.tpl
Code:
$my_string:<br />
{$my_string}



Folders & Files

/cache
/compile
/newfolder
/newfolder/test.tpl
/tpls
/index.php


The error

Code:
Fatal error: Uncaught exception 'SmartyException' with message 'Unable to load template file '../newfolder/test.tpl'' in Z:\blink\www\libs\smarty\sysplugins\smarty_internal_templatebase.php on line 127

SmartyException: Unable to load template file '../newfolder/test.tpl' in Z:\blink\www\libs\smarty\sysplugins\smarty_internal_templatebase.php on line 127



Quick Solution

In smarty_resources.php, change lines:
Code:
            $_was_relative_prefix = $file[0] == '.' ? substr($file, 0, strpos($_path, '/')) : null;
            $_path = DS . trim($file, '/');
            $_was_relative = true;

By
Code:
         if ($file[0] == '.' && $file[1] == '/') {
            $_was_relative_prefix = $file[0] == '.' ? substr($file, 0, strpos($_path, '/')) : null;
            $_path = DS . trim($file, '/');
            $_was_relative = true;
         }


Or using
Code:
$smarty->display('./../newfolder/test.tpl');

Instead
Code:
$smarty->display('../newfolder/test.tpl');


It works


Last edited by JoniJnm on Tue Aug 28, 2012 8:35 am; edited 1 time in total
Back to top
View user's profile Send private message
rodneyrehm
Administrator


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

PostPosted: Tue Aug 28, 2012 8:34 am    Post subject: Reply with quote

I think I see the problem.

In Smarty2 ../foo/bar.tpl was resolved against template_dir. This is not possible in Smarty3, as you can have multiple template_dir values. Smarty3 resolves relative paths against the current working directory.

Check with http://php.net/manual/en/function.getcwd.php
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
rodneyrehm
Administrator


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

PostPosted: Fri Aug 31, 2012 4:15 pm    Post subject: Reply with quote

Just linking to Issue 114
_________________
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 -> 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