Relative path for include template files

548 views
Skip to first unread message

Abalam

unread,
Apr 13, 2009, 6:05:05 PM4/13/09
to Smarty Developers
Hi,

I don't know if it was aborded previously, but what do you think about
having a relative include system ?

Correct me if i'm wrong, but as my experience (even in Smarty 3), when
i try :

Filesystem :
index.tpl
/mail/header.tpl
/mail/register.tpl

index.php :
$smarty->fetch("mail/header.tpl")

header.tpl :
{include file="register.tpl"}

I've an error, because /register.tpl doesn't exists...
Don't you think it can be useful to have an access mode wth relative
path ?

Instead of calling only with absolute path, i think we can use "../
index.tpl" if we need to call parent folders, but not to put "/mail/
register.tpl" if the current template is already in the folder "/
mail/".

Thanks for reading,
Have a nice week !

Yann

Jason Morriss

unread,
Apr 13, 2009, 6:13:55 PM4/13/09
to smarty-d...@googlegroups.com
The paths within the template are relative from the template_dir you have configured in your smarty object, at least that's what I've experienced. So you should be able to do:

index.php :
$smarty->template_dir = '/full/path/to/templates/';

$smarty->fetch("mail/header.tpl");

header.tpl :
{include file="mail/register.tpl"}

note, no leading slash in the include.

However, It might be nice for the file paths within a template to be relative to the current template file instead of the template_dir. But that might break a lot of existing templates.

-- Jason
--
Jason Morriss
http://www.psychostats.com/

Abalam

unread,
Apr 13, 2009, 6:32:07 PM4/13/09
to Smarty Developers
I vote for an option anywhere, because i work myself with multi-schema
templates (aspect oriented).

And there are lots of dimensions (sub-directories), with skin
priorities...


To fit my previous example, skin 1 and 4 have a custom register body
for emailing, not skin 2 and 3 :

Default :
/mail/register.tpl
/mail/header.tpl

Facultative skin templates :
/skin1/mail/header.tpl
/skin1/mail/register.tpl
/skin4/mail/header.tpl
/skin4/mail/register.tpl

A $path parameter is very contraignant in my case...

Abalam

unread,
Apr 13, 2009, 6:39:15 PM4/13/09
to Smarty Developers
Uwe, Monte,

Maybe it's hard to do, but the ultimate solution for me can be useful
for everyone :

- Is it possible to have a priority system array for the $smarty-
>template_dir as we have for $smarty->plugins_dir ?

This way, all website with skins and default template files could use
easily Smarty 3 without copying skins generci template files (Magento,
phpbb, coppermine, etc...).

Thanks for reading,
Yann

Monte Ohrt

unread,
Apr 13, 2009, 6:43:34 PM4/13/09
to smarty-d...@googlegroups.com
You can use an array for the template_dir, even in Smarty 2. Is that
what you are referring to?

Abalam

unread,
Apr 13, 2009, 6:59:42 PM4/13/09
to Smarty Developers
I didn't know that, as i found in the sourcecode :

// set default dirs
$this->template_dir = '.' . DIRECTORY_SEPARATOR .
'templates' . DIRECTORY_SEPARATOR;
$this->compile_dir = '.' . DIRECTORY_SEPARATOR .
'templates_c' . DIRECTORY_SEPARATOR;
$this->plugins_dir = array(dirname(__FILE__) .
DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR);

I thought only plugins could be an array...
Thanks for the tip, but i have questions :

- are they like plugins, look up in each folders in the same order
then stop when the first file with the name is found ?
- is it possible to call relatively another file into {include }
function ?

Thanks for all.
Yann

Monte Ohrt

unread,
Apr 13, 2009, 7:19:58 PM4/13/09
to smarty-d...@googlegroups.com
Abalam wrote:
> I didn't know that, as i found in the sourcecode :
>
> // set default dirs
> $this->template_dir = '.' . DIRECTORY_SEPARATOR .
> 'templates' . DIRECTORY_SEPARATOR;
> $this->compile_dir = '.' . DIRECTORY_SEPARATOR .
> 'templates_c' . DIRECTORY_SEPARATOR;
> $this->plugins_dir = array(dirname(__FILE__) .
> DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR);
>
> I thought only plugins could be an array...
> Thanks for the tip, but i have questions :
>
> - are they like plugins, look up in each folders in the same order
> then stop when the first file with the name is found ?
>
Yes

> - is it possible to call relatively another file into {include }
> function ?
>

All template paths are relative to the root of the template_dir

Jason Morriss

unread,
Apr 13, 2009, 8:20:48 PM4/13/09
to smarty-d...@googlegroups.com
One thing I'm planning on doing in my project is to allow multiple themes (as is usual in any large web based project, IMO) and its easily accomplished with having a single template_dir. For example my structure might be like this:

templates/
templates/theme1
templates/theme2

in templates/main.html I could do:
{include file='theme1/header.html'}
{include file='theme2/footer.html'}

It's very easy to include files from each theme directory and you don't need multiple $template_dir's. I even use variables to allow the inclusions to be dynamic in some cases:

{include file="$theme/header.html"}

I never once use an absolute path so I don't understand how the current system wouldn't work in your case.


On Mon, Apr 13, 2009 at 6:59 PM, Abalam <ygaut...@gmail.com> wrote:
- is it possible to call relatively another file into {include }
function ?



Abalam

unread,
Apr 14, 2009, 3:08:23 PM4/14/09
to Smarty Developers


>
> I never once use an absolute path so I don't understand how the current
> system wouldn't work in your case.

It's because i want to have priorities, and i don't want to copy
similar files for all themes si i need some files to be exactly the
same as the default theme.

In your example, you must copy files, even if it is the same as
theme1...
Reply all
Reply to author
Forward
0 new messages