Php/Smarty/smarty5

提供: 初心者エンジニアの簡易メモ
ナビゲーションに移動 検索に移動

smarty5のダウンロード

https://github.com/smarty-php/smarty/releases/tag/v5.8.0

smarty4から5へ

setterへ

修正前

$smarty->cache_dir = '/path/to/cache';
$smarty->template_dir = '/path/to/templates';
$smarty->compile_dir = '/path/to/templates_c';
$smarty->left_delimiter = '{{';
$smarty->right_delimiter = '}}';

修正後

$smarty->setCacheDir('/path/to/cache');
$smarty->setTemplateDir('/path/to/templates');
$smarty->setCompileDir('/path/to/templates_c');
$smarty->setLeftDelimiter('{{');
$smarty->setRightDelimiter('}}');

getterへ

修正前

$smarty->template_dir;
$smarty->compile_dir;
$smarty->cache_dir;
$smarty->left_delimiter;
$smarty->right_delimiter;

修正後

$smarty->getTemplateDir();
$smarty->getCompileDir();
$smarty->getCacheDir();
$smarty->getLeftDelimiter();
$smarty->getRightDelimiter();

use必須

new Smartyなどで、Smartyを使ってる箇所に以下を追加

use Smarty\Smarty;

pluginsの場所を変える

修正前 smarty-4.5.6/libs/plugins', 修正後 smarty-5.8.0/plugins',

templateで、escapeが使えない対応

エラー詳細

 Fatal error: Uncaught Smarty\CompilerException: Syntax error in template "<h2>{{$message|escape:'html'}}</h2>" unknown modifier 'escape' in /

対応

$this->setPluginsDir([
                    APPLICATION_PATH . '/../library/smarty-5.8.0/plugins'
                ]);

plugins/modifier.escape.php を追加

<?php
/**
 * Smarty escape modifier
 * Usage: {$var|escape:'html'}
 */
function smarty_modifier_escape($string, $type = 'html')
{
    switch ($type) {
        case 'html':
            return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
        case 'url':
            return rawurlencode($string);
        default:
            return $string;
    }
}

templateで、randが使えない対応

エラー詳細

Fatal error: Uncaught Smarty\CompilerException: Syntax error in template "file:ad.tpl" on line 8 "{{assign var="rand1000" value=1|rand:1000}}" unknown modifier 'rand' 

対応 plugins/modifier.rand.php を追加

<?php
function smarty_modifier_rand(...$args)
{
    if (count($args) == 1) {
        return rand(0, $args[0]);
    }

    if (count($args) >= 2) {
        return rand($args[0], $args[1]);
    }

    return rand();
}

templateで、mathが使えない対応

エラー詳細

Fatal error: Uncaught Smarty\CompilerException: Syntax error in template "file:ad.tpl" on line 1 "{{math equation="rand(1,1000)" assign="rand"}}" unknown tag 'math' in /

対応 plugins/modifier.rand.php を追加

<?php
function smarty_function_math($params, $smarty)
{
    if (!isset($params['equation'])) {
        return '';
    }

    $equation = $params['equation'];

    // 非常に簡易な処理(今回の rand 用)
    if (preg_match('/rand\((\d+),(\d+)\)/', $equation, $m)) {
        $result = rand((int)$m[1], (int)$m[2]);
    } else {
        return '';
    }

    if (isset($params['assign'])) {
        $smarty->assign($params['assign'], $result);
        return;
    }

    return $result;
}

templateで、get_classが使えない対応

修正前

// ErrorController.php
$this->view->exception = $errors->exception;
// error.tpl
{{$exception|get_class}}

修正後

// ErrorController.php
$this->view->exception = $errors->exception;
$this->view->exception_class = get_class($errors->exception);
// error.tpl
<b>Class:</b> {{$exception_class}}