Suhail Gupta
Suhail Gupta

Reputation: 23226

Getting a blank output when using conditional statement

I am trying to run conditional statement through smarty using a variable named name, but nothing gets displayed when the php file is run. I am new to smarty. What could be the mistake I am making?

Test.php

<?php
    require_once('./libs/Smarty.class.php');

    $smarty = new Smarty();

    $smarty->assign('title','Title of the page');
    $smarty->assign('hello','Text displayed from smarty!!');
    $smarty->assign('name','is smarty');
    $smarty->display('./template/template.tpl');

?>

Template.tpl

<html>

<head> 
    <title> {$title} </title>
 </head>

<body>
    {$hello}

    {if $name eq "smarty"}
        <span> The name is : smarty</span>
    {/elseif $name eq "is smarty"}
        <span> The name is : is smarty</span>
</body>

Upvotes: 0

Views: 72

Answers (3)

axiac
axiac

Reputation: 72226

You forgot to close the {if} block in the template:

<html>

<head> 
    <title> {$title} </title>
 </head>

<body>
    {$hello}

    {if $name eq "smarty"}
        <span> The name is : smarty</span>
    {/elseif $name eq "is smarty"}
        <span> The name is : is smarty</span>
    {/if}   {* <------ here ---- *}
</body>

When nothing is displayed you can debug the templates by setting $smarty->error_reporting to E_ALL & ~E_NOTICE before you call display().

Upvotes: 2

Yatin Khullar
Yatin Khullar

Reputation: 1590

In your .tpl file use if condition something like this

{if $name eq "smarty"}
        <span> The name is : smarty</span>
    {elseif $name eq "is smarty"}
        <span> The name is : is smarty</span>
    {else}
    Welcome, whatever you are.
    {/if}

To read more about if else in Smarty click here

Upvotes: 0

pmr
pmr

Reputation: 14

Assignment of variables in-template is essentially placing application logic into the presentation that may be better handled in PHP.

$smarty->assign('name', 'is smarty');

assigned variable in smarty object only can possible to get in template.

If you give simply $name = '' then particular variable not assign to smarty template.

so please assign variable in logic and get the value in presentation

And also ensure {elseif} instead of {/elseif} and also closing {/if} statement in smarty template page

Upvotes: 0

Related Questions