【问题标题】:Smarty modifier plugin with access to Smarty object可访问 Smarty 对象的 Smarty 修改器插件
【发布时间】:2015-02-11 14:25:44
【问题描述】:

PHP Smarty 的修饰符插件可以访问 Smarty 对象吗?

block 和 function 插件类型都将当前 Smarty 对象作为参数。即

function smarty_function_NAME($params, Smarty_Internal_Template $Smarty) {...}
function smarty_block_NAME($params, $content, Smarty_Internal_Template $Smarty, &$repeat) {...}

但是修改器插件没有。即

function smarty_modifier_NAME($arg1, $arg2, ...) {...}

有没有办法从修饰符中访问 Smarty 对象?

我想做一个修饰符来获取变量名字符串的值,如下所示:

{$colorblue = '#0317e9'}
{$index = 'blue'}
{$_color = "color`$index`"|smarty_variable_value}

【问题讨论】:

  • 为什么要使用 smarty 对象?如果你需要得到 $index 部分,你可以做一个{$_variable = "variable`$index`"|smarty_variable_value:$index}
  • 我认为我的例子不够清楚。想法是会有一个名为variable1 的变量。索引也不必是数字。我会相应地编辑问题。
  • 我仍然不确定你为什么需要 smarty 对象。您可以使用 : 运算符将任意数量的变量传递给修饰符:{$_color = "color`$index`"|smarty_variable_value:$colorblue:$index:$var1:$var2}

标签: php object plugins smarty modifier


【解决方案1】:

我最终创建了一个小的 get 扩展函数来处理这个问题:

function smarty_function_get($params, Smarty_Internal_Template $Smarty) {
    if (!isset($params['name'])) {
        return;
    }

    $value = $Smarty->getTemplateVars($params['name']);

    if (!isset($params['assign'])) {
        return $value;
    }

    $Smarty->assign($params['assign'], $value);
}

使用调用

{get name="color`$index`" assign="_color"}

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2012-01-01
    • 1970-01-01
    • 2012-06-24
    • 2013-08-05
    • 1970-01-01
    • 2014-09-20
    • 2011-09-25
    • 2011-07-12
    相关资源
    最近更新 更多