Class yii\smarty\Extension

Inheritanceyii\smarty\Extension
Source Code https://github.com/yiisoft/yii2-smarty/blob/master/src/Extension.php

Extension provides Yii-specific syntax for Smarty templates.

Protected Properties

Hide inherited properties

Property Type Description Defined By

Public Methods

Hide inherited methods

Method Description Defined By
__construct() yii\smarty\Extension
blockCss() Smarty block function plugin Usage is the following: yii\smarty\Extension
blockDescription() Smarty block function plugin Usage is the following: yii\smarty\Extension
blockJavaScript() Smarty block function plugin Usage is the following: yii\smarty\Extension
blockTitle() Smarty block function plugin Usage is the following: yii\smarty\Extension
compilerUse() Smarty compiler function plugin Usage is the following: yii\smarty\Extension
functionJs() Smarty template function to instantiate JsExpression yii\smarty\Extension
functionMeta() Smarty function plugin Usage is the following: yii\smarty\Extension
functionPath() Smarty template function to get relative URL for using in links yii\smarty\Extension
functionRegisterCssFile() Smarty function plugin Usage is the following: yii\smarty\Extension
functionRegisterJsFile() Smarty function plugin Usage is the following: yii\smarty\Extension
functionSet() Smarty function plugin Usage is the following: yii\smarty\Extension
functionUrl() Smarty template function to get absolute URL for using in links yii\smarty\Extension
modifierVoid() Smarty modifier plugin Converts any output to void yii\smarty\Extension

Protected Methods

Hide inherited methods

Method Description Defined By
getViewConstVal() Helper function to convert a textual constant identifier to a View class integer constant value. yii\smarty\Extension

Property Details

Hide inherited properties

$smarty protected property
protected \Smarty $smarty null
$viewRenderer protected property

Method Details

Hide inherited methods

__construct() public method

public void __construct ( $viewRenderer, $smarty )
$viewRenderer yii\smarty\ViewRenderer
$smarty \Smarty

                public function __construct($viewRenderer, $smarty)
{
    $this->viewRenderer = $viewRenderer;
    $smarty = $this->smarty = $smarty;
    $smarty->registerPlugin('function', 'path', [$this, 'functionPath']);
    $smarty->registerPlugin('function', 'url', [$this, 'functionUrl']);
    $smarty->registerPlugin('function', 'set', [$this, 'functionSet']);
    $smarty->registerPlugin('function', 'meta', [$this, 'functionMeta']);
    $smarty->registerPlugin('function', 'js', [$this, 'functionJs']);
    $smarty->registerPlugin('function', 'registerJsFile', [$this, 'functionRegisterJsFile']);
    $smarty->registerPlugin('function', 'registerCssFile', [$this, 'functionRegisterCssFile']);
    $smarty->registerPlugin('block', 'title', [$this, 'blockTitle']);
    $smarty->registerPlugin('block', 'description', [$this, 'blockDescription']);
    $smarty->registerPlugin('block', 'registerJs', [$this, 'blockJavaScript']);
    $smarty->registerPlugin('block', 'registerCss', [$this, 'blockCss']);
    $smarty->registerPlugin('compiler', 'use', [$this, 'compilerUse']);
    $smarty->registerPlugin('modifier', 'void', [$this, 'modifierVoid']);
}

            
blockCss() public method

Smarty block function plugin Usage is the following:

{registerCss} div.header {

background-color: #3366bd;
color: white;

} {/registerCss}

Supported attributes: key and valid HTML attributes for the style tag. Refer to Yii documentation for details.

public void blockCss ( $params, $content, $template, &$repeat )
$params
$content
$template \Smarty_Internal_Template
$repeat

                public function blockCss($params, $content, $template, &$repeat)
{
    if ($content !== null) {
        $key = isset($params['key']) ? $params['key'] : null;
        Yii::$app->getView()->registerCss($content, $params, $key);
    }
}

            
blockDescription() public method

Smarty block function plugin Usage is the following:

{description}

The text between the opening and closing tags is added as
meta description tag to the page output.

{/description}

Supported attributes: none.

public void blockDescription ( $params, $content, $template, &$repeat )
$params
$content
$template \Smarty_Internal_Template
$repeat

                public function blockDescription($params, $content, $template, &$repeat)
{
    if ($content !== null) {
        // Clean-up whitespace and newlines
        $content = preg_replace('/\s+/', ' ', trim($content));
        Yii::$app->getView()->registerMetaTag([
            'name' => 'description',
            'content' => $content
        ], 'description');
    }
}

            
blockJavaScript() public method

Smarty block function plugin Usage is the following:

{registerJs key='show' position='POS_LOAD'}

$("span.show").replaceWith('<div class="show">');

{/registerJs}

Supported attributes: key, position. Refer to Yii documentation for details. The position attribute is passed as text without the class prefix. Default is 'POS_READY'.

public void blockJavaScript ( $params, $content, $template, &$repeat )
$params
$content
$template \Smarty_Internal_Template
$repeat

                public function blockJavaScript($params, $content, $template, &$repeat)
{
    if ($content !== null) {
        $key = isset($params['key']) ? $params['key'] : null;
        $position = isset($params['position']) ? $params['position'] : null;
        Yii::$app->getView()->registerJs(
            $content,
            $this->getViewConstVal($position, View::POS_READY),
            $key
        );
    }
}

            
blockTitle() public method

Smarty block function plugin Usage is the following:

{title} Web Site Login {/title}

Supported attributes: none.

public void blockTitle ( $params, $content, $template, &$repeat )
$params
$content
$template \Smarty_Internal_Template
$repeat

                public function blockTitle($params, $content, $template, &$repeat)
{
    if ($content !== null) {
        Yii::$app->getView()->title = $content;
    }
}

            
compilerUse() public method

Smarty compiler function plugin Usage is the following:

{use class="app\assets\AppAsset"} {use class="yii\helpers\Html"} {use class='yii\widgets\ActiveForm' type='block'} {use class='@app\widgets\MyWidget' as='my_widget' type='function'}

Supported attributes: class, as, type. Type defaults to 'static'.

public string compilerUse ( $params, $template )
$params
$template \Smarty_Internal_Template

                public function compilerUse($params, $template)
{
    if (!isset($params['class'])) {
        trigger_error("use: missing 'class' parameter");
    }
    // Compiler plugin parameters may include quotes, so remove them
    foreach ($params as $key => $value) {
        $params[$key] = trim($value, '\'""');
    }
    $class = $params['class'];
    $alias = ArrayHelper::getValue($params, 'as', StringHelper::basename($params['class']));
    $type = ArrayHelper::getValue($params, 'type', 'static');
    if (!isset($this->smarty->registered_plugins[$type][$alias])) {
        // Register the class during compile time
        $this->smarty->registerClass($alias, $class);
        // Skip already registered block and function
        if ($type === 'block') {
            // Register widget tag during compile time
            $this->viewRenderer->widgets['blocks'][$alias] = $class;
            // Register widget tag during compile time
            $this->smarty->registerPlugin('block', $alias, [$this->viewRenderer, '_widget_block__' . $alias]);
        } elseif ($type === 'function') {
            // Register widget tag during compile time
            $this->viewRenderer->widgets['functions'][$alias] = $class;
            // Register widget tag during compile time
            $this->smarty->registerPlugin('function', $alias, [$this->viewRenderer, '_widget_function__' . $alias]);
        }
    }
    if ($type === 'block') {
        // Inject code to re-register widget tag during run-time
        return <<<PHP
p
\$viewRenderer=\$_smarty_tpl->default_template_handler_func[0];
\$viewRenderer->widgets['blocks']['$alias'] = '$class';
try {
    \$_smarty_tpl->registerPlugin('block', '$alias', [\$viewRenderer, '_widget_block__$alias']);
}
catch (SmartyException \$e) {
    /* Ignore already registered exception during first execution after compilation */
}

    } elseif ($type === 'function') {
        // Inject code to re-register widget tag during run-time
        return <<<PHP
p
\$viewRenderer=\$_smarty_tpl->default_template_handler_func[0];
\$viewRenderer->widgets['functions']['$alias'] = '$class';
try {
    \$_smarty_tpl->registerPlugin('function', '$alias', [\$viewRenderer, '_widget_function__$alias']);
}
catch (SmartyException \$e) {
    /* Ignore already registered exception during first execution after compilation */
}

    }
    return '';
}

            
functionJs() public method (available since version 2.0.9)

Smarty template function to instantiate JsExpression

Usage is the following:

{js assign='expr' expression='function(){alert('expression');}}'}

public void functionJs ( $params, \Smarty_Internal_Template $template )
$params array
$template \Smarty_Internal_Template

                public function functionJs($params, \Smarty_Internal_Template $template)
{
    if (!isset($params['expression'])) {
        trigger_error("expression: missing 'js' parameter");
    }
    if (!isset($params['assign'])) {
        trigger_error("assign: missing 'js' parameter");
    }
    $template->assign($params['assign'], new JsExpression($params['expression']));
}

            
functionMeta() public method

Smarty function plugin Usage is the following:

{meta keywords="Yii,PHP,Smarty,framework"}

Supported attributes: any; all attributes are passed as parameter array to Yii's registerMetaTag function.

public void functionMeta ( $params, $template )
$params
$template \Smarty_Internal_Template

                public function functionMeta($params, $template)
{
    $key = isset($params['name']) ? $params['name'] : null;
    Yii::$app->getView()->registerMetaTag($params, $key);
}

            
functionPath() public method

Smarty template function to get relative URL for using in links

Usage is the following:

{path route='blog/view' alias=$post.alias user=$user.id}

where route is Yii route and the rest of parameters are passed as is.

public string functionPath ( $params, \Smarty_Internal_Template $template )
$params array
$template \Smarty_Internal_Template

                public function functionPath($params, \Smarty_Internal_Template $template)
{
    if (!isset($params['route'])) {
        trigger_error("path: missing 'route' parameter");
    }
    array_unshift($params, $params['route']);
    unset($params['route']);
    return Url::to($params);
}

            
functionRegisterCssFile() public method

Smarty function plugin Usage is the following:

{registerCssFile url='@assets/css/normalizer.css'}

Supported attributes: url, key, depends and valid HTML attributes for the link tag. Refer to Yii documentation for details.

public void functionRegisterCssFile ( $params, $template )
$params
$template \Smarty_Internal_Template

                public function functionRegisterCssFile($params, $template)
{
    if (!isset($params['url'])) {
        trigger_error("registerCssFile: missing 'url' parameter");
    }
    $url = ArrayHelper::remove($params, 'url');
    $key = ArrayHelper::remove($params, 'key', null);
    Yii::$app->getView()->registerCssFile($url, $params, $key);
}

            
functionRegisterJsFile() public method

Smarty function plugin Usage is the following:

{registerJsFile url='http://maps.google.com/maps/api/js?sensor=false' position='POS_END'}

Supported attributes: url, key, depends, position and valid HTML attributes for the script tag. Refer to Yii documentation for details. The position attribute is passed as text without the class prefix. Default is 'POS_END'.

public void functionRegisterJsFile ( $params, $template )
$params
$template \Smarty_Internal_Template

                public function functionRegisterJsFile($params, $template)
{
    if (!isset($params['url'])) {
        trigger_error("registerJsFile: missing 'url' parameter");
    }
    $url = ArrayHelper::remove($params, 'url');
    $key = ArrayHelper::remove($params, 'key', null);
    if (isset($params['position'])) {
        $params['position'] = $this->getViewConstVal($params['position'], View::POS_END);
    }
    Yii::$app->getView()->registerJsFile($url, $params, $key);
}

            
functionSet() public method

Smarty function plugin Usage is the following:

{set title="My Page"} {set theme="frontend"} {set layout="main.tpl"}

Supported attributes: title, theme, layout

public void functionSet ( $params, $template )
$params
$template \Smarty_Internal_Template

                public function functionSet($params, $template)
{
    if (isset($params['title'])) {
        $template->tpl_vars['this']->value->title = Yii::$app->getView()->title = ArrayHelper::remove($params, 'title');
    }
    if (isset($params['theme'])) {
        $template->tpl_vars['this']->value->theme = Yii::$app->getView()->theme = ArrayHelper::remove($params, 'theme');
    }
    if (isset($params['layout'])) {
        Yii::$app->controller->layout = ArrayHelper::remove($params, 'layout');
    }
    // We must have consumed all allowed parameters now, otherwise raise error
    if (!empty($params)) {
        trigger_error('set: Unsupported parameter attribute');
    }
}

            
functionUrl() public method

Smarty template function to get absolute URL for using in links

Usage is the following:

{url route='blog/view' alias=$post.alias user=$user.id}

where route is Yii route and the rest of parameters are passed as is.

public string functionUrl ( $params, \Smarty_Internal_Template $template )
$params array
$template \Smarty_Internal_Template

                public function functionUrl($params, \Smarty_Internal_Template $template)
{
    if (!isset($params['route'])) {
        trigger_error("path: missing 'route' parameter");
    }
    array_unshift($params, $params['route']);
    unset($params['route']);
    return Url::to($params, true);
}

            
getViewConstVal() protected method

Helper function to convert a textual constant identifier to a View class integer constant value.

protected mixed getViewConstVal ( $string, $default )
$string string

Constant identifier name

$default integer

Default value

                protected function getViewConstVal($string, $default)
{
    try {
        $val = @constant('\yii\web\View::' . $string);
    } catch (\Exception $e) {
    } catch (\Throwable $e) {
    }
    return isset($val) ? $val : $default;
}

            
modifierVoid() public method

Smarty modifier plugin Converts any output to void

public string modifierVoid ( $arg )
$arg mixed

                public function modifierVoid($arg)
{
    return;
}