「Php/Smarty/smarty5」の版間の差分
提供: 初心者エンジニアの簡易メモ
| 行50: | 行50: | ||
smarty-5.8.0/plugins', | smarty-5.8.0/plugins', | ||
| − | === | + | ===templateで、modifierのescapeが使えない対応=== |
エラー詳細 | エラー詳細 | ||
<pre> | <pre> | ||
| 行82: | 行82: | ||
</pre> | </pre> | ||
| − | === | + | ===templateで、modifierのrandが使えない対応=== |
エラー詳細 | エラー詳細 | ||
<pre> | <pre> | ||
| 行105: | 行105: | ||
</pre> | </pre> | ||
| − | === | + | ===templateで、functionのmathが使えない対応=== |
エラー詳細 | エラー詳細 | ||
<pre> | <pre> | ||
| 行137: | 行137: | ||
} | } | ||
</pre> | </pre> | ||
| − | === | + | ===templateで、functionのinsertが使えない対応=== |
エラー詳細 | エラー詳細 | ||
<pre> | <pre> | ||
2026年4月5日 (日) 14:58時点における版
目次
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で、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で、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}}
