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;

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