开发者

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

开发者 https://www.devze.com 2023-03-20 01:57 出处:网络
inside .tpl: {assign var=\"datasheet\" value=$product->reference|escape:\'htmlall\':\'UTF-8\'} ... ...

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

obviously it doesn't work but with a given known variable function is good and tested i also tried to allow php tag into smarty class, it allows but i'm not able to access smarty variable.

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}

IT DOESN'T WORK:

{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}

ERROR:

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


I don't know why $smarty->get_template_vars('datasheet') fails here, but you can work around it by explicitly passing the parameter and reading with $inParam[]:

your.tpl file

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

[code not tested]

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

(Edited above to separate file contents. Below here is new)

I assumed smarty v3. Should work the similarly for v2.x.

In a smarty .tpl file inside {php} ... {/php} you are at global scope and use $smarty->get_template_vars('var_name'); instead of $this->get_template_vars('var_name');.

On second look at your original code, $smarty->get_template_vars() fails because $smarty is not defined in the function scope, so you get null (and a notice about undefined variable). Put "global $smarty;" as first line of your plugin function body or, better change the function's parameter declaration "function smarty_function_testsk($param, $smarty)" which defines $smarty as the instance of the current template object.

0

精彩评论

暂无评论...
验证码 换一张
取 消