Smarty field value in If statement - smarty

I am trying to include a database field value in a Smarty if file exists statement like this.
{if file_exists('docs/owner_comments/{$property.id}.shtml')}
{include file="{$incownercomments}/{$property.id}.shtml"}
{else}
....
{/if}
However it does not work. The problem is with the first line. How can I include the field value in the {if file exists...} part?
I appreciate your help.

You should change ' in first line into " and of course make sure path is valid
I've created test script:
index.php:
$smarty->assign('property', array('id' => 2));
$smarty->display('testme.tpl');
testme.tpl
{$property.id}
{if file_exists("templates/testme{$property.id}.tpl")}
{include file="templates/testme{$property.id}.tpl"}
{else}
....
{/if}
testme2.tpl
testme2
Output is
2 testme2
as expected. If I change in index.php line
$smarty->assign('property', array('id' => 2));
into
$smarty->assign('property', array('id' => 3));
and file testme2.tpl doesn't exists, I get output
3 ....
as expected

Related

Nested if-else in smarty

Hi I am a beginner and I was trying to write some nested if-else statements in smarty php. It doesn't recognize them and gives an error. I have posted my code snippet but it would be best if someone gave me an example. Thank you :D
{if $my_value[1]==1}
{if}{/if} ======>here I get the error
{elseif $my_value[1]==2}
{else}
{/if}
Try this:
{if $my_value[1] == 1}
1
{elseif $my_value[1] == 2}
2
{else}
0
{/if}

How to remove duplicate values on array using Smarty

I have an array that have duplicated values, I want to print the value only once no matter how many time it exist on the array.
This is the code that I have:
{foreach item="item" from=$root.page.news.item }
{foreach from=$item.Tags item=tagitem key=kex}
{$tagitem}
{/foreach}
{/foreach}
This is what it prints right:
kim000kardashian
kim000kardashian
miley000cyrus
miley000cyrus
kim000kardashian
irina000shayk
and this is what I am looking to print
kim000kardashian
miley000cyrus
irina000shayk
Is there a way to achieve this only using Smarty Templates? or any way that I can use on the .tpl files?
Thanks in advance
{foreach item="item" from=$root.page.news.item }
{foreach from=$item.Tags item=tagitem key=kex}
{if !$done.$tagitem}
{$done.$tagitem = 1}
{$tagitem}
{/if}
{/foreach}
{/foreach}
I am not sure it works with all the versions.
Maybe it would be a bit cleaner to call an array_unique() in the php.

How to assign string PHP variable (function) in smarty?

I need to change from section by $type. In from section we have an PHP static functions:
{foreach from=dbPay::getAll(null) item=dbPay}
{foreach from=dbPay::getSomething(null) item=dbPay}
etc.
Let's see, we make an variable for that:
{foreach from={$static_php_function} item=dbPay}
Usage:
{if ($type == 1)}
// need to assign: dbPay::getAll(null)
// ?? {assign var="static_php_function" value="dbPay::getAll(null)"}
{elseif ($type == 2)}
// need to assign: dbPay::getSomething(null)
// ?? {assign var="static_php_function" value="dbPay::getSomething(null)"}
{/if}
{foreach from={$static_php_function} item=dbPay}
...
{/foreach}
Just drop double quotes around function call, and curly braces around variable name in foreach loop:
{if ($type == 1)}
{assign var="static_php_function_result" value=dbPay::getAll(null)}
{elseif ($type == 2)}
{assign var="static_php_function_result" value=dbPay::getSomething(null)}
{/if}
{foreach from=$static_php_function_result item=dbPay}
...
{/foreach}
Remember that it does not assign function for later call, but result to operate on.
simple
add the output of you function depending what type it is in the handler to a var.
add that var to you template.
And fixed.

Concatenate smarty variable with foreach

In a .tpl file, I'm using a smarty foreach to concatenate values from an array, separated by pipes "|" :
{foreach from=$attachments item=attachment}{$attachment.file}|{/foreach}
This writes : test1.mp3|test2.mp3|test3.mp3|
Now... I need to pass this result as a variable in an href link.
The problem is I can't include my foreach inside the a href tag.
Also I tried to assign this result to a new smarty variable but can't figure how to do it.
Any help would be great.
Thanks.
You can do it this way:
{assign var=result value=''}
{foreach from=$attachments item=attachment}
{assign var=temp value=$attachment.file}
{assign var=result value=$result$temp|}
{/foreach}

Smarty: cannot recognize continue tag

all im trying to do is to use the simple continue tag but it keeps giving me error like this:
string(145) "Smarty error: [in module_db_tpl:onlyimage4;image_detail line 26]: syntax error: unrecognized tag 'continue' (Smarty_Compiler.class.php, line 590)"
my code is as follow:
{foreach from=$itemlist item="item"}
< .. SOME CODE ..>
{if $maxCol == $colm}
</div>
{assign var ='colm' value = 0}
{$row++}
{continue} **<- THIS IS THE PROBLEM**
{/if}
<.. SOME CODE ..>
{/foreach}
does anyone have any idea whats wrong, I've been googling and see no comments of such sort everyone seem to suggest that this should work.. any ideas guys...
Old question, but you need to use: {$continue} (including the $)
For smarty 2:
I don't think the tag exists. if you read this thread you can see that there are people that want it, and a suggestion to fix it like so. (have not tried)
compiler.continue­.php
<?php
function smarty_compiler_con­tinue($contents, &$smarty)
{
return 'continue;';
}
?>
(Bold part my addition)
Create these two files (in this case just one) and put them into your plugins directory
(notice the naming convention compiler.xxx.php).
The good news is, for smarty 3 there is such a tag! see the manual, with example:
{$data = [1,2,3,4,5]}
{foreach $data as $value}
{if $value == 3}
{* skip this iteration *}
{continue}
{/if}
{$value}
{/foreach}
{*
prints: 1 2 4 5
*}

Resources