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

Using a variable directory to {include

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


Joined: 24 Sep 2003
Posts: 9

PostPosted: Wed Sep 24, 2003 2:51 pm    Post subject: Using a variable directory to {include Reply with quote

Hi!

First of all, sorry for my english, my main language is portuguese. Also, this is my first post here, I've searched the manual, Wiki and FAQ, but couldn't find an answer.

I have a question which can be formulated and answered in many ways, I suppose. How to use a variable to specify a file to include, or how can I nest smarty code (looks like I can't, right?), and how can I include a file using relative path, and not only absolute?

I develop on Linux and several of my clients run Windows, so I had to create a function to specify what directory the templates resides.

On my computer:
/var/www/html/virto/templates/template_name/

On one of my clients:
c:/virto/www/templates/template_name/

So I made a class with two methods to tell me what's the template address:
class virtoConfig()
{
function templateDir()
{
return 'virto';
}
// AKA template name

function templateDirCompleto()
{
'/var/www/html/virto/templates/' . $this->templateDir();
}
}

Assign it to smarty:
$virto_config = new virtoConfig();
$smarty->assign('virto_config', $virto_config);

And now the file inclusion, per se... I am writing this way for now, in a file called template.tpl, which resides on /var/www/html/virto/templates/template_name/:

{include file="/var/www/html/virto/templates/virto/menu_corporativo.tpl"}

But what I really need to is to abstract the directory, so I don't have to make a different template version for my clients.

So, I've tried using a relative path:

{include file="menu_corporativo.tpl"}

menu_corporativo.tpl and template.tpl both reside on the same directory.

I've also tried to "nest" Smarty block (I think the manual says I can't, right?, anyway I had to try Wink)

{include file="{$virto_config->templateDirCompleto()}/menu_corporativo.tpl"}

And / or:

{include file="$virto_config->templateDirCompleto()/menu_corporativo.tpl"}

I am sure there are lots of scripts and people which did what I need to, so I came here for help Smile

The most strange thing I've noticed is why the relative include didn't work... the manual says it should Sad

Here's some specs:

RedHat 9
Apache/2.0.40
PHP 4.2.2
Smarty 2.5.0

Any help is appreciated, thanks!
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Sep 24, 2003 4:57 pm    Post subject: Reply with quote

Hi inerte.

As for the problem with directories being different with your client and development setup:

- Why not use different templates_dir settings for each computer? As long as the contents of the templates_dir are mapped the same way on both machines, the path to those directories should not matter as far as Smarty is concerned. In other words, you shouldn't need to use absolute paths when refering to template files--they are expected to be relative to templates_dir.

- You also have the option to use a custom resource. This allows you to have far greater control of access to templates. The manual and the forum has far more on that topic.

As for nesting tags, it is not possible, but many workarounds exist. In your case, you will need a temporary variable:

{assign var=path value=$virto_config->templateDirCompleto()}
{include file="$path/menu_corporativo.tpl"}

You may also want to look at embedded backticks. Still, you shouldn't need to do something like this and even if you did, you should use a resource to hide the implementation details from the template. eg. using a hypothetical resource named "base", this could look like: {include file="base:menu_corpativo.tpl"}.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Sep 24, 2003 4:59 pm    Post subject: Reply with quote

consider you have:
/path/to/templates/dir1/foo.tpl
/path/to/templates/dir1/bar.tpl


$smarty->template_dir = "/path/to/templates"; /* this and only this has to be configurable in your case */
$smarty->display('dir1/foo.tpl);

and in foo.tpl:
{include file="dir1/bar.tpl"} (note it's relative to $smarty->template_dir not relative to the template itself )


also working:
$smarty->assign('dir', 'dir1');

and in foo.tpl:
{include file="$dir/bar.tpl"}


if you really really need full paths to your templates in your templates (you don't need that in your examples) you have to say

$smarty->display('file:/path/to/templates/dir1/foo.tpl');
or {include file="file:/path/to/templates/dir1/foo.tpl"}

(note the resource_name "file:" before the full path)

HTH
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
inerte
Smarty Rookie


Joined: 24 Sep 2003
Posts: 9

PostPosted: Wed Oct 08, 2003 11:50 am    Post subject: Reply with quote

Hi Smile

Sorry for the late reply...

My template_dir was set wrong, and some of the .tpl files weren't loading.

Also, I have learned new tricks from both replies, which I am glad.

Anyway, it's working now, thanks!
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