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

提供: 初心者エンジニアの簡易メモ
ナビゲーションに移動 検索に移動
編集の要約なし
編集の要約なし
55行目: 55行目:
  Fatal error: Uncaught Smarty\CompilerException: Syntax error in template "<h2>{{$message|escape:'html'}}</h2>" unknown modifier 'escape' in /
  Fatal error: Uncaught Smarty\CompilerException: Syntax error in template "<h2>{{$message|escape:'html'}}</h2>" unknown modifier 'escape' in /
</pre>
</pre>
対応
対応方法
 
<pre>
<pre>
$this->setPluginsDir([
$this->setPluginsDir([
87行目: 88行目:
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'  
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'  
</pre>
</pre>
対応
対応方法
 
plugins/modifier.rand.php を追加
plugins/modifier.rand.php を追加
<pre>
<pre>
111行目: 113行目:
Smarty\CompilerException  
Smarty\CompilerException  
</pre>
</pre>
対応
対応方法
 
plugins/modifier.default.php を追加
plugins/modifier.default.php を追加
<pre>
<pre>
129行目: 132行目:
Syntax error in template  "{{if $smarty.now|date_format:'%Y-%m-%d'<='2014-11-30'}}" unknown modifier 'date_format'
Syntax error in template  "{{if $smarty.now|date_format:'%Y-%m-%d'<='2014-11-30'}}" unknown modifier 'date_format'
</pre>
</pre>
対応
対応方法
 
plugins/modifier.date_format.php を追加
plugins/modifier.date_format.php を追加
<pre>
<pre>
159行目: 163行目:
Syntax error in template "" unknown modifier 'replace'
Syntax error in template "" unknown modifier 'replace'
</pre>
</pre>
対応
対応方法
 
plugins/modifier.replace.php を追加
plugins/modifier.replace.php を追加
<pre>
<pre>
187行目: 192行目:
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 /
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 /
</pre>
</pre>
対応
対応方法
 
plugins/function.math.php を追加
plugins/function.math.php を追加
<pre>
<pre>
220行目: 226行目:
Smarty\CompilerException  
Smarty\CompilerException  
</pre>
</pre>
対応
対応方法
 
plugins/function.insert.php を追加
plugins/function.insert.php を追加
<pre>
<pre>
251行目: 258行目:
<pre>
<pre>
{{insert name="getHoge"}}
{{insert name="getHoge"}}
</pre>
===templateで、functionのif preg_matchが使えない対応===
エラー詳細
<pre>
Syntax error in template "{{if preg_match("/_hoge$/", $request.url)}}" unknown modifier 'preg_match'
Smarty\CompilerException
</pre>
対応方法
plugins/block.if_preg_match.php を追加すれば、smarty3,4,5で使える。
<pre>
<?php
function smarty_block_if_preg_match($params, $content, $smarty, &$repeat)
{
    if ($repeat) {
        return;
    }
    if (!isset($params['pattern']) || !isset($params['subject'])) {
        return '';
    }
    if (preg_match($params['pattern'], $params['subject'])) {
        return $content;
    }
    return '';
}
</pre>
修正後
<pre>
{{if preg_match("/_open$/", $request.code)}}
    OPEN
{{/if}}
</pre>
修正前
<pre>
{{if_preg_match pattern="/_open$/" subject=$request.code}}
    OPEN
{{/if_preg_match}}
</pre>
</pre>



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

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で、modifierの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で、modifierの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で、modifierのdefaultが使えない対応

エラー詳細

 Message: Syntax error in template "{{include file="parts/hoge.tpl" request=$request piyo=$piyo|default:''}}" 
Smarty\CompilerException 

対応方法

plugins/modifier.default.php を追加

<?php
function smarty_modifier_default($value, $default = '')
{
    if ($value === null || $value === '' || $value === false) {
        return $default;
    }
    return $value;
}

templateで、modifierのdate_formatが使えない対応

エラー詳細

Syntax error in template  "{{if $smarty.now|date_format:'%Y-%m-%d'<='2014-11-30'}}" unknown modifier 'date_format'

対応方法

plugins/modifier.date_format.php を追加

<?php
function smarty_modifier_date_format($timestamp, $format = '%Y-%m-%d')
{
    if (!$timestamp) {
        return '';
    }

    // strftime互換変換
    $format = str_replace(
        ['%Y','%m','%d','%H','%M','%S'],
        ['Y','m','d','H','i','s'],
        $format
    );

    if (!is_numeric($timestamp)) {
        $timestamp = strtotime($timestamp);
    }

    return date($format, $timestamp);
}

templateで、modifierのreplaceが使えない対応

エラー詳細

Syntax error in template "" unknown modifier 'replace'

対応方法

plugins/modifier.replace.php を追加

<?php
function smarty_modifier_replace($string, $search, $replace = '')
{
    return str_replace($search, $replace, $string);
}

templateで、modifierのurlencodeが使えない対応

エラー詳細

Syntax error in template "" unknown modifier 'urlencode'

対応 plugins/modifier.urlencodephp を追加

<?php
function smarty_modifier_urlencode($string)
{
    return urlencode($string);
}

templateで、functionの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/function.math.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で、functionのinsertが使えない対応

エラー詳細

Syntax error in template "{{insert name="getHoge"}}" unknown tag 'insert'
Smarty\CompilerException 

対応方法

plugins/function.insert.php を追加

<?php

function smarty_function_insert($params, $smarty)
{
    if (!isset($params['name'])) {
        return '';
    }

    $func = 'smarty_insert_' . $params['name'];

    if (function_exists($func)) {
        return $func($params, $smarty);
    }

    return '';
}

そのまま作ったinsert関数とテンプレが使える

function smarty_insert_getHoge($params, &$smarty)
{
    return "hogehoge";
}

tpl

{{insert name="getHoge"}}

templateで、functionのif preg_matchが使えない対応

エラー詳細

Syntax error in template "{{if preg_match("/_hoge$/", $request.url)}}" unknown modifier 'preg_match' 
Smarty\CompilerException 

対応方法

plugins/block.if_preg_match.php を追加すれば、smarty3,4,5で使える。

<?php

function smarty_block_if_preg_match($params, $content, $smarty, &$repeat)
{
    if ($repeat) {
        return;
    }

    if (!isset($params['pattern']) || !isset($params['subject'])) {
        return '';
    }

    if (preg_match($params['pattern'], $params['subject'])) {
        return $content;
    }

    return '';
}

修正後

{{if preg_match("/_open$/", $request.code)}}
    OPEN
{{/if}}

修正前

{{if_preg_match pattern="/_open$/" subject=$request.code}}
    OPEN
{{/if_preg_match}}

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