2014-02-10 25 views
0

你好我使用Smarty的,并试图获得的if/else在TPL其中显示工作{$代码}Smarty的 - PHP的if/else

php文件

$this->_Smarty->assign("License_Key", TRUE); 

$Value = ' 
License Key : 
{if $License_Key} 
Valid 
{else} 
Not Valid 
{/if}'; 

$this->_Smarty->assign("Code", $Value); 

在TPL文件:

{$Code} 

输出为

License Key : 
{if $License_Key} 
Valid 
{else} 
Not Valid 
{/if} 

出认沽应该是:

License Key : Valid 
+0

您正在为任何其他Smarty变量分配'$ Value'。你没有告诉Smarty它应该将该片段解释为Smarty模板。 – deceze

回答

1

你必须把的$Value内容为.tpl文件,而不是到PHP。所以.tpl内容将是:

License Key : 
{if $License_Key} 
    Valid 
{else} 
    Not Valid 
{/if} 

PHP,只分配$License_Key

$this->_Smarty->assign("License_Key", TRUE); 

编辑:但是,如果你需要做的是你究竟是如何写的,你可以使用{eval},试试这样:

{eval var=$Code} 

但我仍建议你在Smarty模板中使用Smarty标记,而不是在PHP文件中使用它。

+0

是的,但我正在寻找其他的东西,所以我必须用这种方式,有没有解决方案?所以我可以使用if/else在分配 –

+0

我刚刚更新了我的文章 –

+0

好吧它工作正常,非常感谢你,你最好:) –

0

我认为这是你在找什么:

$Value = 'License Key : '.($License_Key) ? 'Valid' : 'Not Valid'; 

?基本上是如果和:基本上是别的。

+0

这比在PHP中使用smarty标签更有意义。这不是它的目的。只要在PHP中这样做,并输出结果smarty。 –

+0

谢谢,这是非常真实的。 – Anonymous