用smarty 3语法重写插件

时间:2012-03-27 14:53:11

标签: smarty smarty3

我需要重写修饰符'truncate'的方法,因为它会导致错误。 版本3中不存在 _getfilepath

$this->_engine = new Smarty();
$this->_engine->template_dir = $config->paths->templates;
$this->_engine->compile_dir = sprintf('%s/tmp/templates_c',
                                      $config->paths->data);

 $this->_engine->plugins_dir = array($config->paths->base .
                           '/woonbel/include/Templater/plugins','plugins');



function smarty_function_breadcrumbs($params, $smarty)
{
    $defaultParams = array('trail'     => array(),
                           'separator' => ' > ',
                           'truncate'  => 40);

    // initialize the parameters
    foreach ($defaultParams as $k => $v) {
        if (!isset($params[$k]))
            $params[$k] = $v;
    }

    // load the truncate modifier
    if ($params['truncate'] > 0)

require_once $ smarty-> getPluginsDir('modifier','truncate');

 $links = array();
        $numSteps = count($params['trail']);
        for ($i = 0; $i < $numSteps; $i++) {
            $step = $params['trail'][$i];

            // truncate the title if required
            if ($params['truncate'] > 0)
                $step['title'] = smarty_modifier_truncate($step['title'],
                                                          $params['truncate']);

            // build the link if it's set and isn't the last step
            if (strlen($step['link']) > 0 && $i < $numSteps - 1) {
                $links[] = sprintf('<a href="%s" title="%s">%s</a>',
                                   htmlSpecialChars($step['link']),
                                   htmlSpecialChars($step['title']),
                                   htmlSpecialChars($step['title']));
            }
            else {
                // either the link isn't set, or it's the last step
                $links[] = htmlSpecialChars($step['title']);
            }
        }

        // join the links using the specified separator
        return join($params['separator'], $links);
    }

1 个答案:

答案 0 :(得分:2)

您正在寻找(未公开记录的)loadPlugin()函数:

$smarty->loadPlugin('smarty_modifier_truncate', true);
$string = smarty_modifier_truncate($string);

更新测试用例:

<?php
error_reporting(E_ALL);
require_once 'path-to/Smarty.class.php';

$smarty = new Smarty();
$smarty->loadPlugin('smarty_modifier_truncate', true);
$string = 'hello world I should be truncated to some smaller string';
$string = smarty_modifier_truncate($string, 10);
var_dump($string);