【问题标题】:Call PHP variable in Smarty在 Smarty 中调用 PHP 变量
【发布时间】:2013-02-04 04:44:29
【问题描述】:

下面这个 PHP 代码在 Smarty 中的调用将不起作用

PHP 中的示例用法

$content=@file_get_contents("http://www.gempixel.com/short/api/?url=http://www.google.com&api=APIKEY");

$url=json_decode($content,TRUE);//Decodes json into an array

if(!$url["error"]){  // If there is no error
echo $url["short"]; //Outputs the short url
}else{ 
echo $url["msg"]; //Outputs the error message
}

对于 Smarty,我只需要将 php 替换为 {php} 即可。

{php}$shortenedurl = file_get_contents('http://www.gempixel.com/short/api/?api=fAaFjILdbigF&url=' . urlencode('http://' . $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI']));{/php}

{php}$url=json_decode($content,TRUE);if(!$url["error"]){echo $url["short"];}else{echo $url["msg"];}{/php}

但它不会调用字符串。我做错了什么?

【问题讨论】:

  • 我认为模板的全部目的是将代码与设计分开......
  • @OneTrickPony,朋友,谢谢你睁开眼睛;)

标签: php smarty


【解决方案1】:

首先你需要为 smarty 分配变量值

$iframe="hiii";
$smarty->assign( 'iframe', $iframe ); //$smarty is a global var to assign

然后获取您的 .tpl 文件

{$iframe}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    相关资源
    最近更新 更多