デリミタの表示 {$smarty.ldelim}, {$smarty.rdelim}

広告

テンプレートファイル内では静的コンテンツとSmartyで使用している変数などを区別するために、Smartyで使用する語句はデリミタで囲っています。デリミタはデフォルトでは「{」と「}」ですが、デリミタではなく静的コンテンツの一部として「{」と「}」を記述したい場合に {$smarty.ldelim} と {$smarty.rdelim} を使用できます。

{$smarty.ldelim}
{$smarty.rdelim}

テンプレートファイル内での記述例は次のようになります。

Smartyでのデリミタは {$smarty.ldelim} と {$smarty.rdelim} です。

{$smarty.ldelim}が「{」に該当し、{$smarty.rdelim}が「}」に該当します(デリミタがデフォルトのままの場合)。

テンプレートファイル内に「{」と「}」が文字として両方記述されていた場合や、「{」が記述された後にデリミタを使用したSmartyの変数などが現れた場合はエラーとなります。

なお同じ目的で利用されるものとして組み込み関数の{ldelim},{rdelim}や{literal}{/literal}などを使用することも可能です。こちらは別のページで詳しく見ていきます。

サンプルプログラム

では簡単なサンプルプログラムを作成して試してみます。

sample4-1.php

<?php

require_once('Smarty.class.php');

$smarty = new Smarty();

$smarty->template_dir = 'd:/smartysample/reserve/templates/';
$smarty->compile_dir  = 'd:/smartysample/reserve/templates_c/';
$smarty->config_dir   = 'd:/smartysample/reserve/configs/';
$smarty->cache_dir    = 'd:/smartysample/reserve/cache/';

$smarty->assign('title', '予約変数のテスト');

$smarty->display('sample4-1.tpl');

?>

上記を「sample4-1.php」の名前で「(Apacheドキュメントルート)¥smarty¥reserve」に保存します。

sample4-1.tpl

{* Smarty reserve/sample4-1.tpl *}
<html>
<head>
<title>Smarty Test</title>
</head>
<body>

<h1>{$title}</h1>
<p>Smartyでのデリミタは {$smarty.ldelim} と {$smarty.rdelim} です。</p>

</body>
</html>

上記を「sample4-1.tpl」の名前で「D:¥smartysample¥reserve¥templates」に保存します。

そしてブラウザから「http://localhost/smarty/reserve/sample4-1.php」へアクセスして下さい。

デリミタの表示 {$smarty.ldelim}, {$smarty.rdelim}

( Written by Tatsuo Ikura )