phpif-statementsmartyline-breakssmarty2

How to check whether the variable is empty or not and depending on it insert <br> in <td> and print the value in it?


I'm using Smarty 2 in my project.

I've one HTML table in which I'm printing the date received from PHP. For your reference I'm just putting below only the necessary code part from smarty template:

<td>{$street1}, {$street2}, {$city}, {$state_code}, {$zip_code}</td>

Now in above code I want to check whether the variable $street2 has some value in it or not. If the variable $street2 contains some value then I want to add <br> after $street1 and print the value contained in $street2 on the new line within that same <td>.

How should I achieve this in smarty?

Same is the thing with PHP code as follows :

$data['user_address'] = $value['street1']."".$value['street2']."".$value['city']."".$value['state_code']."".$value['zip_code'];

In above code also I have to check $value['street2'] and insert a break line and insert the value.

Can someone please help me in this?


Solution

  • 1:

    <td>{$street1}, {if $street2}<br>{$street2}, {/if}{$city}, {$state_code}, {$zip_code}</td>
    

    2:

    $data['user_address'] = $value['street1'].""
        .($value['street2'] ? "\n".$value['street2']."" : '')
        .$value['city']."".$value['state_code']."".$value['zip_code'];