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 if file exists?
Goto page 1, 2  Next
 
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
misterm
Smarty Rookie


Joined: 31 Mar 2009
Posts: 5

PostPosted: Tue Mar 31, 2009 10:07 am    Post subject: smarty if file exists? Reply with quote

Hi

I wonder if anyone can help?

I am setting up a smarty shopping cart I have bought and the following tweak falls outside of the products support.

I have a variable $product.ID which is set when any product page is displayed. In this case the product is a misic CD.

Most of these CDs have a small mp3 sample that I want to play on each product page.

What I would like to do is say: include a file exists for this product id, otherwise, do nothing.

In this example my $product.ID is 5, so I want to dynamically include a template file that contains my player from a directory with the name matching my $product.ID

I am trying this:

{if ($smarty->template_exists("soundbites/$product.ID/player.tpl")) {
$smarty->display("soundbites/$product.ID/player.tpl");
}
{/if}

but I can't seem to get it to work :/

I also tried a simpler option:

{include file="soundbites/$product.ID/player.tpl"}

Which again won't work.
I know the path is correct as this works if I hard code the productt ID:

{include file="soundbites/5/player.tpl"}

I just can't seem to simply get the variable to print its value in the path dynamically.




Any help would be much appreciated.

Mr M
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Mar 31, 2009 11:59 am    Post subject: Reply with quote

This would have been useful to me too at one point, so I'm curious about this one too. Question
Back to top
View user's profile Send private message
elpmis
Smarty Elite


Joined: 07 Jun 2007
Posts: 321

PostPosted: Tue Mar 31, 2009 12:10 pm    Post subject: Reply with quote

Have you tried something like this?

Code:
{capture assign='player'}soundbites/{$product.ID}/player.tpl{/capture}
{if $player|file_exists eq ''}
  {eval var=$player}
{/if}
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Mar 31, 2009 12:20 pm    Post subject: Reply with quote

elpmis wrote:
Have you tried something like this?

Code:
{capture assign='player'}soundbites/{$product.ID}/player.tpl{/capture}
{if $player|file_exists eq ''}
  {eval var=$player}
{/if}


I haven't tried it yet, but is it absolutely guaranteed that the current working directory will be the one that the current smarty template is in?
Back to top
View user's profile Send private message
elpmis
Smarty Elite


Joined: 07 Jun 2007
Posts: 321

PostPosted: Tue Mar 31, 2009 12:41 pm    Post subject: Re: smarty if file exists? Reply with quote

The request was

misterm wrote:
include a file exists for this product id, otherwise, do nothing.


The directory will be defined by {$product.ID}.
Back to top
View user's profile Send private message
misterm
Smarty Rookie


Joined: 31 Mar 2009
Posts: 5

PostPosted: Tue Mar 31, 2009 12:44 pm    Post subject: Reply with quote

Thanks for the reply elpmis

That prints the correct location to screen including the $product.ID Smile :

soundbites/5/player.tpl

But it's only printing the path to the file and not including the actual file.

I've had a play with it but can't figure this last bit out.

Many thanks

Mr M
Back to top
View user's profile Send private message
elpmis
Smarty Elite


Joined: 07 Jun 2007
Posts: 321

PostPosted: Tue Mar 31, 2009 12:47 pm    Post subject: Reply with quote

misterm wrote:

That prints the correct location to screen including the $product.ID Smile :

soundbites/5/player.tpl


{eval} should be parse the template.

Are you sure that soundbites/5/player.tpl exists?

Maybe you have to complete the path Rolling Eyes ... what's inside player.tpl?
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Mar 31, 2009 1:04 pm    Post subject: Reply with quote

elpmis wrote:
The request was

misterm wrote:
include a file exists for this product id, otherwise, do nothing.


The directory will be defined by {$product.ID}.


I understand that.




elpmis wrote:
misterm wrote:

That prints the correct location to screen including the $product.ID Smile :

soundbites/5/player.tpl


{eval} should be parse the template.

Are you sure that soundbites/5/player.tpl exists?

Maybe you have to complete the path Rolling Eyes ... what's inside player.tpl?


This is what I was referring to. The current working directory determines whether you have to give a complete path or not.
Back to top
View user's profile Send private message
misterm
Smarty Rookie


Joined: 31 Mar 2009
Posts: 5

PostPosted: Tue Mar 31, 2009 2:41 pm    Post subject: Reply with quote

The file definitely exists.

If I try:

{include file="soundbites/5/player.tpl"}

the file is brought in and rendered correctly.

Player.tpl is just a few lines of text at the moment, it will eventually hold a small flash mp3 player.

To be honest, i think all I need is something like:

{include file="soundbites/".{$product.ID}."/player.tpl"}

But I can't seem to get the syntax right to render the $product.ID Sad
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Mar 31, 2009 4:56 pm    Post subject: Reply with quote

Code:
{include file="soundbites/`$product.ID`/player.tpl"}
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 Mar 31, 2009 4:59 pm    Post subject: Reply with quote

mohrt wrote:
Code:
{include file="soundbites/`$product.ID`/player.tpl"}


Is there a good way to test to see if a template exists?
Back to top
View user's profile Send private message
misterm
Smarty Rookie


Joined: 31 Mar 2009
Posts: 5

PostPosted: Tue Mar 31, 2009 6:01 pm    Post subject: Reply with quote

Thanks for the suggestion, but it still doesn't work.

Damn

I guess to test that the file exists all i need to do is:

{include file="soundbites/5/player.tpl"}

which does bring in the content of tpl file in that folder.

Why wouldn't this work? :

{include file="soundbites/`$product.ID`/player.tpl"}

Do I have to write the full path as a variable?
That's how I got this working on a different site in plain php (no smarty):

$rightNavAd="content/ads/rightNav/"."$xsubcatid".".php";
if (file_exists($rightNavAd)) { include ($rightNavAd); }

Does anyone know the smarty equivalent?

many thaks again for the help
Back to top
View user's profile Send private message
misterm
Smarty Rookie


Joined: 31 Mar 2009
Posts: 5

PostPosted: Tue Mar 31, 2009 6:10 pm    Post subject: Reply with quote

Just realised that's what:

{capture assign='player'}soundbites/{$product.ID}/player.tpl{/capture}

is successfully doing.

So why won't this work:

{capture assign='player'}soundbites/{$product.ID}/player.tpl{/capture}
{if $player|file_exists eq ''}
{include file='$player'}
{eval var=$player}
{/if}
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Mar 31, 2009 6:33 pm    Post subject: Reply with quote

Smarty filepaths for {include} are always relative to the template_dir. PHP's function file_exists() is not going to respect the template_dir filepath.

Maybe try making a modifier:

Code:
{if $template|template_exists}
  {include file=$template}
{/if}


This modifier would basically execute:

Code:
return file_exists("{$smarty->template_dir}/{$template}");
Back to top
View user's profile Send private message Visit poster's website
BbErSeRkK
Smarty Rookie


Joined: 04 Jan 2008
Posts: 14

PostPosted: Wed Jun 03, 2009 5:41 am    Post subject: Reply with quote

I'm a bit slow,
tried to follow monty's example but had to do it this way:

Code:

php file:
$smarty->assign('template_dir',$smarty->template_base_dir);

tpl file:
{assign var="special_msg_tpl" value="./msgs/special.`$item.code`.tpl"}

{ if $special_msg_tpl|template_exists:$template_base_dir}
<li> {include file=$special_msg_tpl} </li>
{/if}


it works but seems a bit convoluted
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
Goto page 1, 2  Next
Page 1 of 2

 
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