簡體   English   中英

Smarty 3 tpl:如何執行 php 插件 function 將 smarty 變量調用到 .tpl 文件中?

[英]Smarty 3 tpl: how to execute php plugin function that calls a smarty variable into the .tpl file?

內部.tpl:

{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>';
}
?>

顯然它不起作用,但是對於給定的已知變量 function 很好並且經過測試,我還嘗試允許 php 標記進入 smarty class,它允許但我不能訪問 smarty 變量。

有用:

{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

我不知道為什么 $smarty->get_template_vars('datasheet') 在這里失敗,但是您可以通過顯式傳遞參數並使用 $inParam[] 讀取來解決它:

你的.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 文件中,您位於全局 scope 並使用 $smarty->get_template_vars('var_name'); 而不是 $this->get_template_vars('var_name');。

在第二次查看您的原始代碼時,$smarty->get_template_vars() 失敗,因為 $smarty 未在 function scope 中定義,因此您得到 Z37A6259CC0C1DAE29BD9A7866489D 的通知(和未定義變量的通知)。 輸入“global $smarty;” 作為插件 function 主體的第一行,或者,最好更改函數的參數聲明“function smarty_function_testsk($param, $smarty)”,它將 $smarty 定義為當前模板 object 的實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM