Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

smarty if empty else smarty value

I have smarty value if it empty need another result

{get_video_meta video_id=$video_data.id key='test'}

This value print "test" If this value is empty else how can I use?

 {if !empty({get_video_meta video_id=$video_data.id key='test'})} 
  No value
 {else}
 {get_video_meta video_id=$video_data.id key='test'}
{/if}

This code not work

like image 595
Raj Mohammed Avatar asked Jun 09 '15 09:06

Raj Mohammed


People also ask

How do you write if condition in Smarty?

{if} statements in Smarty have much the same flexibility as PHP if statements, with a few added features for the template engine. Every {if} must be paired with a matching {/if} . {else} and {elseif} are also permitted. All PHP conditionals and functions are recognized, such as ||, or, &&, and, is_array(), etc.

How check array is empty or not in Smarty?

PHP function smarty_modifier_is_empty($input) { return empty($input); } ?> This will output Array is empty . Show activity on this post. You can put the variable directly in an if-statement as long as you're sure it'll be always set (empty or not) like so {if !$


2 Answers

You could use the {capture} function to capture the output into a variable, then choose what to do with it:

{capture name="video_meta"}{get_video_meta video_id=$video_data.id key='test'}{/capture}
{if empty($smarty.capture.video_meta)}
 No value
 {else}
 {$smarty.capture.video_meta}
{/if}
like image 142
IMSoP Avatar answered Sep 30 '22 20:09

IMSoP


You can assign value to variable:

{if empty($code)}
    {assign var='code' value='404'}
{/if}

but i suppose that you need something like:

{if empty($video_data.id)}
    No value
{else}
    {get_video_meta video_id=$video_data.id key='test'}
{/if}

because get_video_meta - it's smarty plugin, and you can't use it like you try to use...

In foreach:

{foreach from=$video_data key=k item=v}
    {if empty($k)}
        No value
    {else}
        {get_video_meta video_id=$v.id key=$k}
    {/if}
{/foreach}
like image 45
cn007b Avatar answered Oct 04 '22 20:10

cn007b