View previous topic :: View next topic |
Author |
Message |
misterm Smarty Rookie
Joined: 31 Mar 2009 Posts: 5
|
Posted: Tue Mar 31, 2009 10:07 am Post subject: smarty if file exists? |
|
|
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 |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Tue Mar 31, 2009 11:59 am Post subject: |
|
|
This would have been useful to me too at one point, so I'm curious about this one too. |
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Tue Mar 31, 2009 12:10 pm Post subject: |
|
|
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 |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Tue Mar 31, 2009 12:20 pm Post subject: |
|
|
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 |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Tue Mar 31, 2009 12:41 pm Post subject: Re: smarty if file exists? |
|
|
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 |
|
misterm Smarty Rookie
Joined: 31 Mar 2009 Posts: 5
|
Posted: Tue Mar 31, 2009 12:44 pm Post subject: |
|
|
Thanks for the reply elpmis
That prints the correct location to screen including the $product.ID :
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 |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Tue Mar 31, 2009 12:47 pm Post subject: |
|
|
misterm wrote: |
That prints the correct location to screen including the $product.ID :
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 ... what's inside player.tpl? |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Tue Mar 31, 2009 1:04 pm Post subject: |
|
|
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 :
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 ... 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 |
|
misterm Smarty Rookie
Joined: 31 Mar 2009 Posts: 5
|
Posted: Tue Mar 31, 2009 2:41 pm Post subject: |
|
|
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 |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 31, 2009 4:56 pm Post subject: |
|
|
Code: | {include file="soundbites/`$product.ID`/player.tpl"} |
|
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Tue Mar 31, 2009 4:59 pm Post subject: |
|
|
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 |
|
misterm Smarty Rookie
Joined: 31 Mar 2009 Posts: 5
|
Posted: Tue Mar 31, 2009 6:01 pm Post subject: |
|
|
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 |
|
misterm Smarty Rookie
Joined: 31 Mar 2009 Posts: 5
|
Posted: Tue Mar 31, 2009 6:10 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 31, 2009 6:33 pm Post subject: |
|
|
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 |
|
BbErSeRkK Smarty Rookie
Joined: 04 Jan 2008 Posts: 14
|
Posted: Wed Jun 03, 2009 5:41 am Post subject: |
|
|
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 |
|
|