內.tpl:Smarty 3 tpl:如何執行調用smarty變量到.tpl文件的php插件函數?
{assign var="datasheet" value=$product->reference|escape:'htmlall':'UTF-8'}
... ...
{testsk}
function.testsk.php:
<?php
function smarty_function_testsk(){
$ch = curl_init ("http://www.domain.com/path/to/".$smarty->get_template_vars('datasheet')."/name.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
preg_match("/\s<div id=\"divname\">(.*)<\/div>/siU", $page, $matches);
foreach ($matches as &$match) {
$match = $match;
}
echo '<table>';
echo $matches[1];
echo '</table>';
}
?>
顯然不工作,但與給定的已知變量功能良好和測試 我也試圖讓PHP標籤到smarty類,它允許但我不能訪問smarty變量。
IT WORKS:
{php}
$ch = curl_init ("http://www.domain.com/path/to/3345674/name.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
preg_match("/\s<div id=\"divname\">(.*)<\/div>/siU", $page, $matches);
foreach ($matches as &$match) {
$match = $match;
}
echo '<table>';
echo $matches[1];
echo '</table>';
{/php}
它不工作:
{assign var="datasheet" value=$product->reference|escape:'htmlall':'UTF-8'}
{php}
$v1 = $this->get_template_vars('datasheet');
$ch = curl_init ("http://www.domain.com/path/to/".$v1."/name.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
preg_match("/\s<div id=\"divname\">(.*)<\/div>/siU", $page, $matches);
foreach ($matches as &$match) {
$match = $match;
}
echo '<table>';
echo $matches[1];
echo '</table>';
{/php}
錯誤:
Fatal error: Using $this when not in object context in /var/www/vhosts/domain.com/httpdocs/folder/tools/smarty/plugins/block.php.php(23) : eval()'d code on line 2
請看看上面的代碼,也嘗試允許{php}標籤... – smepie
@smepie,該函數的目的是在一個單獨的插件文件function.testsk.php,而不是模板文件。爲了清晰起見,我編輯了這些內容並解決{php} – Magicianeer