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

include file - path needed?

 
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
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Apr 28, 2009 3:24 pm    Post subject: include file - path needed? Reply with quote

I have an include similar to this one:

Code:

   <{include file='highlights_row.tpl' }>


However it can't find the file. Does it work similar to the PHP include where it looks in the current working directory? I was thinking that it looked in the directory that the template was in.

There is definitely a file of that name in the same directory as the including template.

If it looks in the current working directory, is there a way I can do something similar to this PHP code?

Code:

 include(dirname(__FILE__).'/test.php');
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Tue Apr 28, 2009 5:29 pm    Post subject: Case sensitive? Reply with quote

If you are on a Linux server, please check for the case of the filenames. It must match (upper case and lower case filenames are different, unlike Windows).

If youa re under Windows, and this script does not run, you may not have the vaild file name there, or your OS has specific limitation to disallow using/accessing this file.

In your case, you must put highlights_row.tpl in the same directory where the .tpl is running from. Normally, it is Smarty's template directory ( $smarty->template_dir ) in the config. So, do not get confused with php directory and template directory. Possibly, you put the .tpl file in php directory.

If you are in a php file, you can check the existence of the template by:

Code:
if($smarty->template_exists('highlights_row.tpl'))
{
  $smarty->display('highlights_row.tpl');
}
else
{
  // what if file was not found...
}

This is a demonstration only, and may not solve your exact problem.

And for general use; if you would like to access the file in php's working directory into Smarty's template, as if it is parsed with {php}..{/php}, the below code may help (for advanced users only, in a template file):
Quote:
... .tpl ...
{'highlights.php'|include_once}
... .tpl ...


The main thing is that include_once modifier function does not exist in Smarty. So, it tries to execute another callable function, that ultimately becomes a php function. So, it will search the file in php's working directory instead of Smarty's template directory.

Thats the difference there.
Does it help you undersand more? Please write me.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Apr 28, 2009 5:45 pm    Post subject: Reply with quote

Thanks, i understand how include works now.


My problem is, the way my templates are set up, I would like to be able to do something similar to this:

Code:

 include(dirname(__FILE__).'/test.php');


Translate that to Smarty, and it means: get the directory that this file is in, then include the file test.tpl which is in this directory.

Due to how my CMS works, template files are in many directories. For example:

1. Template A is including Template B which is in another directory (the path to Template B is passed in as a template variable).

2. Template B needs to include Template C which is in the same dir as itself. How can I put an include in Template B so that it can find Template C located in ITS OWN directory.
Back to top
View user's profile Send private message
alokin
Smarty Regular


Joined: 14 Apr 2009
Posts: 96
Location: Belgrade, Serbia

PostPosted: Tue Apr 28, 2009 8:44 pm    Post subject: Reply with quote

As far as I know, that dirname solution is not possible in Smarty... You should include that template C the same way you've included template B in template A, i.e.:
Code:

//template A
{include file = 'path/to/B.tpl'}

//template B
{include file = 'path/to/C.tpl'}

That path/to is relative to the path you set for Smarty template_dir.
_________________
www.nikolaposa.in.rs
Back to top
View user's profile Send private message
sray
Smarty n00b


Joined: 09 May 2009
Posts: 2

PostPosted: Sat May 09, 2009 4:12 am    Post subject: how to insert a php code in tpl? Reply with quote

Hi

i am just want to add a php code in tpl file.How can i call a php code in tpl??

Plz help me out asap.

Regards

Saurabh
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