2011-07-12 92 views
0

內.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 

回答

2

我不知道爲什麼$ smarty-> get_template_vars(「數據表')在這裏失敗了,但是你可以通過顯式傳遞參數並使用$ inParam []讀取來解決它:

your.tpl文件

{assign var="datasheet" value=$product->reference|escape:'htmlall':'UTF-8'} 
... ... 
{testsk datasheet=$datasheet} 

function.testsk.php

<?php 
function smarty_function_testsk($inParam, $inSmarty){ 
$ch = curl_init ("http://www.domain.com/path/to/".$inParam['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>'; 
} 
?> 

[未測試代碼]

http://www.smarty.net/docs/en/plugins.functions.tpl

(以上到單獨的文件內容編輯。下面是新的)

我假設smarty v3。應該爲v2.x類似地工作。在{php} ... {/ php}中的smarty.tpl文件中,您處於全局範圍並使用$ smarty-> get_template_vars('var_name');而不是$ this-> get_template_vars('var_name');.

第二次查看您的原始代碼,$ smarty-> get_template_vars()失敗,因爲未在函數作用域中定義$ smarty,因此您得到null(以及有關未定義變量的通知)。把「全球$聰明」;作爲插件函數體的第一行,或者更好地將函數的參數聲明「function smarty_function_testsk($ param,$ smarty)」定義爲$ smarty作爲當前模板對象的實例。

+0

請看看上面的代碼,也嘗試允許{php}標籤... – smepie

+0

@smepie,該函數的目的是在一個單獨的插件文件function.testsk.php,而不是模板文件。爲了清晰起見,我編輯了這些內容並解決{php} – Magicianeer