facebook twitter hatena line email

「Php/Smarty/smarty5」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(use必須)
行43: 行43:
  
 
  use Smarty\Smarty;
 
  use Smarty\Smarty;
 +
 +
===pluginsの場所を変える===
 +
修正前
 +
smarty-4.5.6/libs/plugins',
 +
修正後
 +
smarty-5.8.0/plugins',
  
 
==templateで、escapeが使えない対応==
 
==templateで、escapeが使えない対応==

2026年4月5日 (日) 13:32時点における版

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で、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}}