Files
smarty/docs/ja/programmers/advanced-features/template-resources.xml

259 lines
8.7 KiB
XML
Raw Normal View History

2007-04-21 12:13:34 +00:00
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 2675 Maintainer: takagi Status: ready -->
2007-04-21 12:13:34 +00:00
<!-- CREDITS: mat-sh,daichi,joe -->
<sect1 id="template.resources">
<title>テンプレートリソース</title>
<para>
テンプレートは様々なリソースから呼び出して使用できます。テンプレートを
<link linkend="api.display"><varname>display()</varname></link>
<link linkend="api.fetch"><varname>fetch()</varname></link>
したり別のテンプレートからインクルードしたりする際には、
リソースの種類に続けて適切なパスとテンプレート名を指定します。
リソースを明示的に指定しない場合は <link
linkend="variable.default.resource.type">
<parameter>$default_resource_type</parameter></link> の値であるとみなします。
</para>
<sect2 id="templates.from.template.dir">
<title>$template_dir からのテンプレート</title>
<para>
<link linkend="variable.template.dir">
<parameter>$template_dir</parameter></link> からのテンプレートを使用する場合は、
テンプレートリソースの指定は必要ありません。しかし、一貫性を保つために
<literal>file:</literal> リソースを使用してもかまいません。使用したいテンプレートへのパスを、
<link linkend="variable.template.dir">
<parameter>$template_dir</parameter></link>
のルートディレクトリからの相対パスで指定します。
</para>
<example>
<title>$template_dir のテンプレートを使用する</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty->display('index.tpl');
$smarty->display('admin/menu.tpl');
$smarty->display('file:admin/menu.tpl'); // 上と同じ
?>
]]>
</programlisting>
<para>Smarty のテンプレート</para>
<programlisting>
<![CDATA[
{include file='index.tpl'}
{* 以下は、上と同じです *}
{include file='file:index.tpl'}
]]>
</programlisting>
</example>
</sect2>
<sect2 id="templates.from.any.dir">
<title>任意のディレクトリからのテンプレート</title>
<para>
<link linkend="variable.template.dir">
<parameter>$template_dir</parameter></link>
の外に置かれたテンプレートを使うには、リソースの種類
<literal>file:</literal> を指定しなければなりません。
その後にテンプレートへの絶対パスを続けます。
</para>
<example>
<title>任意のディレクトリからのテンプレートを使用する</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty->display('file:/export/templates/index.tpl');
$smarty->display('file:/path/to/my/templates/menu.tpl');
?>
]]>
</programlisting>
<para>
Smarty のテンプレート
</para>
<programlisting>
<![CDATA[
{include file='file:/usr/local/share/templates/navigation.tpl'}
]]>
</programlisting>
</example>
<sect3 id="templates.windows.filepath">
<title>Windows のファイルパス</title>
<para>
通常、Windows 環境の場合はファイルパスの先頭にドライブレター (C:)
が含まれます。ネームスペースの衝突を回避して期待通りの結果を得るために、
必ず <literal>file:</literal> を使用して下さい。
</para>
<example>
<title>Windows ファイルパスからテンプレートを使用する</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty->display('file:C:/export/templates/index.tpl');
$smarty->display('file:F:/path/to/my/templates/menu.tpl');
?>
]]>
</programlisting>
<para>
Smarty テンプレート
</para>
<programlisting>
<![CDATA[
{include file='file:D:/usr/local/share/templates/navigation.tpl'}
]]>
</programlisting>
</example>
</sect3>
</sect2>
<sect2 id="templates.from.elsewhere">
<title>その他のリソース内のテンプレート</title>
<para>
データベース・ソケット・LDAP 等の
PHPによってアクセス可能なリソースからテンプレートを取得する事ができます。
そのためにはリソースプラグイン関数を記述し、それを登録する必要があります。
</para>
<para>
リソースプラグイン関数についての詳細な情報は
<link linkend="plugins.resources">リソースプラグイン</link>
の項を参照してください。
</para>
<note>
<para>
元から存在する <literal>file:</literal> リソースは上書きできないことに注意しましょう。
しかし、ファイルシステム上のテンプレートを別の方法で取得するテンプレートを作成することはできます。
それを別のリソース名で登録すればよいのです。
</para>
</note>
<example>
<title>カスタムリソースを使用する</title>
<programlisting role="php">
<![CDATA[
<?php
// これらの関数をアプリケーションに追加します
function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
{
// ここでデータベースを呼び出し、取得したテンプレートを
// $tpl_source に代入します
$sql = new SQL;
$sql->query("select tpl_source
from my_table
where tpl_name='$tpl_name'");
if ($sql->num_rows) {
$tpl_source = $sql->record['tpl_source'];
return true;
} else {
return false;
}
}
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
// $tpl_timestampに代入するためにデータベースを呼び出します
$sql = new SQL;
$sql->query("select tpl_timestamp
from my_table
where tpl_name='$tpl_name'");
if ($sql->num_rows) {
$tpl_timestamp = $sql->record['tpl_timestamp'];
return true;
} else {
return false;
}
}
function db_get_secure($tpl_name, &$smarty_obj)
{
// 全てのテンプレートがセキュアであると仮定します
return true;
}
function db_get_trusted($tpl_name, &$smarty_obj)
{
// テンプレートから使用しません
}
// テンプレートリソース名"db"を登録します
$smarty->register_resource("db", array("db_get_template",
"db_get_timestamp",
"db_get_secure",
"db_get_trusted"));
// phpスクリプトからテンプレートリソースを使用します
$smarty->display("db:index.tpl");
?>
]]>
</programlisting>
<para>
Smarty テンプレート
</para>
<programlisting>
<![CDATA[
{include file='db:/extras/navigation.tpl'}
]]>
</programlisting>
</example>
</sect2>
<sect2 id="default.template.handler.function">
<title>デフォルトのテンプレートハンドラ関数</title>
<para>
テンプレートリソースからテンプレートの取得に失敗した際に、
テンプレートのコンテンツを取り戻すために呼び出されるユーザ定義関数を指定します。
この関数の使用方法の1つとして、その場限りのテンプレートを作成する処理を行います。
</para>
<example>
<title>デフォルトのテンプレートハンドラ関数を使用する</title>
<programlisting role="php">
<![CDATA[
<?php
// アプリケーション内のどこかでこの関数を呼び出します
function make_template ($resource_type, $resource_name, &$template_source, &$template_timestamp,
&$smarty_obj)
{
if( $resource_type == 'file' ) {
if ( ! is_readable ( $resource_name )) {
// テンプレートファイルを生成し、コンテンツを返します
$template_source = "This is a new template.";
require_once SMARTY_CORE_DIR . 'core.write_file.php';
smarty_core_write_file( array( 'filename'=>$smarty_obj->template_dir . DIRECTORY_SEPARATOR . $resource_name, 'contents'=>$template_source ), $smarty_obj );
return true;
}
} else {
// ファイルではない場合
return false;
}
}
// デフォルトのハンドラをセット
$smarty->default_template_handler_func = 'make_template';
?>
]]>
</programlisting>
</example>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->