Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用条件语句时获取空输出_Php_Smarty_Smarty3 - Fatal编程技术网

Php 使用条件语句时获取空输出

Php 使用条件语句时获取空输出,php,smarty,smarty3,Php,Smarty,Smarty3,我试图使用名为name的变量通过smarty运行条件语句,但在运行php文件时不会显示任何内容。我是新来的smarty。我可能犯了什么错误 Test.php <?php require_once('./libs/Smarty.class.php'); $smarty = new Smarty(); $smarty->assign('title','Title of the page'); $smarty->assign('hello','Te

我试图使用名为
name
的变量通过smarty运行条件语句,但在运行php文件时不会显示任何内容。我是新来的
smarty
。我可能犯了什么错误

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');

?>

在模板中分配变量实质上是将应用程序逻辑放入演示文稿中,这在PHP中可能会得到更好的处理

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

smarty对象中分配的变量只能进入模板

如果只给出$name='',则特定变量不会分配给smarty模板

所以,请在逻辑中分配变量,并在表示中获取值


还要确保{elseif}而不是{/elseif},并在.tpl文件的smarty模板页面中关闭{/if}语句

{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}
{if$name eq“smarty”}
名字是:smarty
{elseif$name eq“is smarty”}
名字是:IsSmarty
{else}
欢迎你,不管你是谁。
{/if}

要阅读有关Smarty中if-else的更多信息,请忘记关闭模板中的
{if}
块:

<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>

{$title}
{$hello}
{如果$name eq“smarty”}
名字是:smarty
{/elseif$name eq“is smarty”}
名字是:IsSmarty

{/if}{*
$smarty->assign('name','Your name');
?您没有分配$name。@Debflav已编辑。它仍然是一样的。@Debflav但是,如果我删除if else子句,它可以正常工作。
{elseif$name eq“is smarty”}…{/if}
使用isset($name)或$name |默认值:'',以逃避smarty的注意
<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>