2012-10-27 23 views
2

我知道我們應該避免任何模板引擎內的{php}標籤,但是,讓我們假設它是這種情況下唯一的方法。如何抓住smarty的字符串爲php消費

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

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

我在做什麼錯?

如何抓取聰明的字符串{$ video.yt_id}的PHP消費?

{php} 
$youtube = new YouTube('I-WANT-THE-YOUTUBE-ID-HERE'); 

$meta  = $youtube->get_meta(); 
$author  = $youtube->get_author(); 
$comments = $youtube->get_comments(); 
{/php} 

<p> 
Duration: {php} echo sprintf("%0.2f", $meta['duration']/60); {/php}mins | 
Rated: {php} echo $meta['rating']; {/php} out of 5 | 
Posted by {php} echo $author['username']; {/php} ({php} echo $author['age']; {/php} y/o from {php} echo $author['location']; {/php}) 
{php} echo nl2br($meta['description']); {/php} 
</p> 
+0

不,不要假設。順便說一下,這是充滿'{php}'標籤,我不相信你使用smarty。 – Alexander

回答

1

這應該有助於

{php} 
    $video = $this->get_template_vars('video'); 

    $youtube = new YouTube($video[ 'yt_id' ]); 

    $meta  = $youtube->get_meta(); 
    $author  = $youtube->get_author(); 
    $comments = $youtube->get_comments(); 

    $this->assign('meta', $meta); 
    $this->assign('author', $author); 
    $this->assign('comments', $comments); 
{/php} 

<p> 
{assign var="format" value="%0.2f"} 
{assign var="meta_duration60" value=$meta.duration/60} 
Duration: {$format|sprintf:$duration}mins | 
Rated: {$meta.rating} out of 5 | 
Posted by {$author.username} ({$author.age} y/o from {$author.location}) 
{$meta.description|nl2br} 
</p> 

你可以訪問Smarty的變量內使用$this->get_template_vars($varname){php}標籤,讓變量從可用的{php}到模板的其餘部分內側$this->assign('varname', $varname)

的變化輸出是未經測試的,我希望它能工作

+1

非常感謝你!!!!!!!!! –