「Php/Smarty/smarty5」の版間の差分
| (同じ利用者による、間の26版が非表示) | |||
| 4行目: | 4行目: | ||
==smarty4から5へ== | ==smarty4から5へ== | ||
===setterへ=== | ===setterへ=== | ||
$smarty-> | 修正前 | ||
<pre> | |||
$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 = '}}'; | |||
</pre> | |||
修正後 | |||
<pre> | <pre> | ||
$ | $smarty->setCacheDir('/path/to/cache'); | ||
$ | $smarty->setTemplateDir('/path/to/templates'); | ||
$ | $smarty->setCompileDir('/path/to/templates_c'); | ||
$smarty->setLeftDelimiter('{{'); | |||
$smarty->setRightDelimiter('}}'); | |||
</pre> | </pre> | ||
===getterへ=== | ===getterへ=== | ||
修正前 | |||
<pre> | |||
$smarty->template_dir; | |||
$smarty->compile_dir; | |||
$smarty->cache_dir; | |||
$smarty->left_delimiter; | |||
$smarty->right_delimiter; | |||
</pre> | |||
修正後 | |||
<pre> | <pre> | ||
$smarty-> | $smarty->getTemplateDir(); | ||
$smarty-> | $smarty->getCompileDir(); | ||
$smarty-> | $smarty->getCacheDir(); | ||
$smarty->getLeftDelimiter(); | |||
$smarty->getRightDelimiter(); | |||
</pre> | </pre> | ||
| 23行目: | 43行目: | ||
use Smarty\Smarty; | use Smarty\Smarty; | ||
===pluginsの場所を変える=== | |||
修正前 | |||
smarty-4.5.6/libs/plugins', | |||
修正後 | |||
smarty-5.8.0/plugins', | |||
===templateで、modifierのescapeが使えない対応=== | |||
エラー詳細 | |||
<pre> | |||
Fatal error: Uncaught Smarty\CompilerException: Syntax error in template "<h2>{{$message|escape:'html'}}</h2>" unknown modifier 'escape' in / | |||
</pre> | |||
対応方法 | |||
<pre> | |||
$this->setPluginsDir([ | |||
APPLICATION_PATH . '/../library/smarty-5.8.0/plugins' | |||
]); | |||
</pre> | |||
plugins/modifier.escape.php を追加 | |||
<pre> | |||
<?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; | |||
} | |||
} | |||
</pre> | |||
===templateで、modifierのrandが使えない対応=== | |||
エラー詳細 | |||
<pre> | |||
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> | |||
対応方法 | |||
plugins/modifier.rand.php を追加 | |||
<pre> | |||
<?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(); | |||
} | |||
</pre> | |||
===templateで、modifierのdefaultが使えない対応=== | |||
エラー詳細 | |||
<pre> | |||
Message: Syntax error in template "{{include file="parts/hoge.tpl" request=$request piyo=$piyo|default:''}}" | |||
Smarty\CompilerException | |||
</pre> | |||
対応方法 | |||
plugins/modifier.default.php を追加 | |||
<pre> | |||
<?php | |||
function smarty_modifier_default($value, $default = '') | |||
{ | |||
if ($value === null || $value === '' || $value === false) { | |||
return $default; | |||
} | |||
return $value; | |||
} | |||
</pre> | |||
===templateで、modifierのdate_formatが使えない対応=== | |||
エラー詳細 | |||
<pre> | |||
Syntax error in template "{{if $smarty.now|date_format:'%Y-%m-%d'<='2014-11-30'}}" unknown modifier 'date_format' | |||
</pre> | |||
対応方法 | |||
plugins/modifier.date_format.php を追加 | |||
<pre> | |||
<?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); | |||
} | |||
</pre> | |||
===templateで、modifierのreplaceが使えない対応=== | |||
エラー詳細 | |||
<pre> | |||
Syntax error in template "" unknown modifier 'replace' | |||
</pre> | |||
対応方法 | |||
plugins/modifier.replace.php を追加 | |||
<pre> | |||
<?php | |||
function smarty_modifier_replace($string, $search, $replace = '') | |||
{ | |||
return str_replace($search, $replace, $string); | |||
} | |||
</pre> | |||
===templateで、modifierのurlencodeが使えない対応=== | |||
エラー詳細 | |||
<pre> | |||
Syntax error in template "" unknown modifier 'urlencode' | |||
</pre> | |||
対応 | |||
plugins/modifier.urlencodephp を追加 | |||
<pre> | |||
<?php | |||
function smarty_modifier_urlencode($string) | |||
{ | |||
return urlencode($string); | |||
} | |||
</pre> | |||
===templateで、modifierのmb_strimwidthが使えない対応=== | |||
エラー詳細 | |||
<pre> | |||
Syntax error in template "{{$surename|mb_strimwidth:0:40}" unknown modifier 'mb_strimwidth' | |||
</pre> | |||
対応 | |||
plugins/modifier.mb_strimwidth.php を追加 | |||
<pre> | |||
<?php | |||
function smarty_modifier_mb_strimwidth($string, $start, $width, $trimmarker = '', $encoding = 'UTF-8') | |||
{ | |||
return mb_strimwidth($string, $start, $width, $trimmarker, $encoding); | |||
} | |||
</pre> | |||
===templateで、functionのmathが使えない対応=== | |||
エラー詳細 | |||
<pre> | |||
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> | |||
対応方法 | |||
plugins/function.math.php を追加 | |||
<pre> | |||
<?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; | |||
} | |||
</pre> | |||
===templateで、functionのinsertが使えない対応=== | |||
エラー詳細 | |||
<pre> | |||
Syntax error in template "{{insert name="getHoge"}}" unknown tag 'insert' | |||
Smarty\CompilerException | |||
</pre> | |||
対応方法 | |||
plugins/function.insert.php を追加 | |||
<pre> | |||
<?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 ''; | |||
} | |||
</pre> | |||
そのまま作ったinsert関数とテンプレが使える | |||
<pre> | |||
function smarty_insert_getHoge($params, &$smarty) | |||
{ | |||
return "hogehoge"; | |||
} | |||
</pre> | |||
tpl | |||
<pre> | |||
{{insert name="getHoge"}} | |||
</pre> | |||
tplが、ループ処理する不具合にあたった・・ | |||
===templateで、if preg_matchが使えない対応=== | |||
エラー詳細 | |||
<pre> | |||
Syntax error in template "{{if preg_match("/_fuga$/", $request.code)}}" unknown modifier 'preg_match' | |||
Smarty\CompilerException | |||
</pre> | |||
対応方法 | |||
plugins/modifier.preg_match.php を追加。ちなみに、smarty3,4,5で使える。 | |||
<pre> | |||
<?php | |||
function smarty_modifier_preg_match($string, $pattern) | |||
{ | |||
return preg_match($pattern, $string); | |||
} | |||
</pre> | |||
修正前 | |||
<pre> | |||
{{if preg_match("/_fuga$/", $request.code)}} | |||
{{elseif preg_match("/\.hoge/", $itaurl)}} | |||
{{elseif preg_match("/\.piyo/", $itaurl)}} | |||
{{/if}} | |||
</pre> | |||
修正後 | |||
<pre> | |||
{{if $request.code|preg_match:"/_fuga$/"}} | |||
{{elseif $itaurl|preg_match:"/\.hoge/"}} | |||
{{elseif $itaurl|preg_match:"/\.piyo/"}} | |||
{{else}} | |||
{{/if}} | |||
</pre> | |||
===templateで、countが使えない対応=== | |||
エラー詳細 | |||
<pre> | |||
Syntax error in template "{{if count($genres) > 0}}" unknown modifier 'count' | |||
Smarty\CompilerException | |||
</pre> | |||
対応方法 | |||
plugins/modifier.count.php を追加。 | |||
<pre> | |||
<?php | |||
function smarty_modifier_count($value) | |||
{ | |||
if (is_array($value) || $value instanceof Countable) { | |||
return count($value); | |||
} | |||
return 0; | |||
} | |||
</pre> | |||
修正前 | |||
<pre> | |||
{{if count($genres) > 0}} | |||
</pre> | |||
修正後 | |||
<pre> | |||
{{if $genres|count > 0}} | |||
</pre> | |||
===templateで、substrが使えない対応=== | |||
エラー詳細 | |||
<pre> | |||
Syntax error in template "{{$genre.created|substr:0:16}}" unknown modifier 'substr' | |||
Smarty\CompilerException | |||
</pre> | |||
対応方法 | |||
plugins/modifier.substr.php を追加。 | |||
<pre> | |||
<?php | |||
function smarty_modifier_substr($string, $start, $length = null) | |||
{ | |||
if ($length === null) { | |||
return substr($string, $start); | |||
} | |||
return substr($string, $start, $length); | |||
} | |||
</pre> | |||
===templateで、get_classが使えない対応=== | |||
修正前 | |||
<pre> | |||
// ErrorController.php | |||
$this->view->exception = $errors->exception; | |||
// error.tpl | |||
{{$exception|get_class}} | |||
</pre> | |||
修正後 | |||
<pre> | |||
// ErrorController.php | |||
$this->view->exception = $errors->exception; | |||
$this->view->exception_class = get_class($errors->exception); | |||
// error.tpl | |||
<b>Class:</b> {{$exception_class}} | |||
</pre> | |||
===getPluginsDir()やsetPluginsDir()について=== | |||
smarty5では、getPluginsDirがdeprecatedで、[]の空配列が、強制になってって使えなくなってる。 | |||
===getTemplateDirなど=== | |||
getSmarty()を経由する必要がある。 | |||
修正前 | |||
$smarty->getTemplateDir(); | |||
修正後 | |||
$smarty->getSmarty()->getTemplateDir(); | |||
===smarty4の警告はsmarty5にすることで消えた=== | |||
php8で、smarty4で、出てた警告は消えた。 | |||
<pre> | |||
Warning: Undefined array key "start_time" in smarty-4.5.6/libs/sysplugins/smarty_internal_debug.php on line 146 | |||
Warning: Undefined array key "start_template_time" insmarty-4.5.6/libs/sysplugins/smarty_internal_debug.php on line 73 | |||
</pre> | |||
2026年4月7日 (火) 07:12時点における最新版
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で、modifierのmb_strimwidthが使えない対応
エラー詳細
Syntax error in template "{{$surename|mb_strimwidth:0:40}" unknown modifier 'mb_strimwidth'
対応 plugins/modifier.mb_strimwidth.php を追加
<?php
function smarty_modifier_mb_strimwidth($string, $start, $width, $trimmarker = '', $encoding = 'UTF-8')
{
return mb_strimwidth($string, $start, $width, $trimmarker, $encoding);
}
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"}}
tplが、ループ処理する不具合にあたった・・
templateで、if preg_matchが使えない対応
エラー詳細
Syntax error in template "{{if preg_match("/_fuga$/", $request.code)}}" unknown modifier 'preg_match'
Smarty\CompilerException
対応方法
plugins/modifier.preg_match.php を追加。ちなみに、smarty3,4,5で使える。
<?php
function smarty_modifier_preg_match($string, $pattern)
{
return preg_match($pattern, $string);
}
修正前
{{if preg_match("/_fuga$/", $request.code)}}
{{elseif preg_match("/\.hoge/", $itaurl)}}
{{elseif preg_match("/\.piyo/", $itaurl)}}
{{/if}}
修正後
{{if $request.code|preg_match:"/_fuga$/"}}
{{elseif $itaurl|preg_match:"/\.hoge/"}}
{{elseif $itaurl|preg_match:"/\.piyo/"}}
{{else}}
{{/if}}
templateで、countが使えない対応
エラー詳細
Syntax error in template "{{if count($genres) > 0}}" unknown modifier 'count'
Smarty\CompilerException
対応方法
plugins/modifier.count.php を追加。
<?php
function smarty_modifier_count($value)
{
if (is_array($value) || $value instanceof Countable) {
return count($value);
}
return 0;
}
修正前
{{if count($genres) > 0}}
修正後
{{if $genres|count > 0}}
templateで、substrが使えない対応
エラー詳細
Syntax error in template "{{$genre.created|substr:0:16}}" unknown modifier 'substr'
Smarty\CompilerException
対応方法
plugins/modifier.substr.php を追加。
<?php
function smarty_modifier_substr($string, $start, $length = null)
{
if ($length === null) {
return substr($string, $start);
}
return substr($string, $start, $length);
}
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}}
getPluginsDir()やsetPluginsDir()について
smarty5では、getPluginsDirがdeprecatedで、[]の空配列が、強制になってって使えなくなってる。
getTemplateDirなど
getSmarty()を経由する必要がある。 修正前
$smarty->getTemplateDir();
修正後
$smarty->getSmarty()->getTemplateDir();
smarty4の警告はsmarty5にすることで消えた
php8で、smarty4で、出てた警告は消えた。
Warning: Undefined array key "start_time" in smarty-4.5.6/libs/sysplugins/smarty_internal_debug.php on line 146 Warning: Undefined array key "start_template_time" insmarty-4.5.6/libs/sysplugins/smarty_internal_debug.php on line 73