2010-12-03 86 views
1

我知道我們應該避免任何模板引擎中的{php}標籤,但我們假設它是這種情況下唯一的方法。smarty代碼 - {php}標籤中的訪問變量

考慮到這一點,我想請求一些幫助,以抓住聰明的字符串爲PHP消費。

在這種情況下,我想訪問srtstr php函數中的$ result.domain。

我在做什麼錯?

{foreach key=period item=regoption from=$result.regoptions} 
{if $regoption.$domain} 
{if $domain eq "transfer"} 
{php} 
    $domainName = $result.domain; 
    $tld = strstr($domainName, '.'); 
{/php} 

{if $tld eq '.gt'} 
    <p>Something</p> 
{else} 
<option value="{$period}">{$period} {$LANG.orderyears} @ {$regoption.$domain}</option> 
{/if} 
{/if} 
{/if} 
{/foreach} 

由於提前,
MEM

+0

從外觀上來看,你正在試圖做可以在不`{} PHP來實現什麼`塊,通過簡單循環`$ results.regoptions`並在調用`$ smarty-> display()`之前進行字符串替換。 – eykanal 2010-12-03 19:13:52

回答

4

您可以使用get_template_vars,只要確保你使用$this代替$smarty

$this->get_template_vars('foo') 

這將是一個更好的主意來重寫你是什麼做爲smarty plugin though

編輯:

在您的例子,你可以做這樣的事情

$result = $this->get_template_vars('result'); 
$domainName = $result.domain; 
+0

以及這件事情會是什麼?我的意思是,我試過:「$ result.domain」和「result.domain」 - 沒有運氣。 – MEM 2010-12-03 18:07:50

0

不要{} PHP標籤參考PHP變量,而不是通過$smarty->assign設置的變量?

也許您試圖訪問通過$smarty->assign設置的{PHP}中的內容?

請嘗試訪問該對象,因爲它在調用$smarty->dispay()的.php文件中命名。

0

$this不爲我們工作,我們終於得到了使用它$GLOBALS變量是這樣的:

$variable= $GLOBALS['smarty']->getTemplateVars('variable');