mirror of
https://github.com/smarty-php/smarty.git
synced 2025-11-08 16:21:39 +01:00
218 lines
7.1 KiB
XML
218 lines
7.1 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 2583 Maintainer: takagi Status: ready -->
|
|
<!-- CREDITS: mat-sh,daichi,joe -->
|
|
<sect1 id="language.function.include">
|
|
<title>{include}</title>
|
|
<para>
|
|
<varname>{include}</varname> タグを使用して、
|
|
現在のテンプレートに他のテンプレートをインクルードします。
|
|
現在のテンプレートにて利用可能なあらゆる変数は、
|
|
インクルードされたテンプレートでも同じく利用可能です。
|
|
</para>
|
|
|
|
<itemizedlist>
|
|
<listitem><para>
|
|
<varname>{include}</varname> タグには、テンプレートリソースのパスを含んだ
|
|
<parameter>file</parameter> 属性を必ず指定する必要があります。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
<varname>{include}</varname> の出力をブラウザに表示する代わりに変数に格納したい場合は、
|
|
オプションの <parameter>assign</parameter> 属性にその変数名を定義します。
|
|
<link linkend="language.function.assign"><varname>{assign}</varname></link>
|
|
と同等です。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
インクルードされたテンプレートに変数を渡すには、
|
|
<link linkend="language.syntax.attributes">attributes</link>
|
|
を使用します。インクルードされたテンプレートに明示的に渡された変数は、
|
|
インクルードされたファイルのスコープでのみ有効となります。
|
|
そのテンプレートに同じ名前の変数が存在する場合は、
|
|
渡された変数がそれをオーバーライドします。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
全ての割り当て変数の値は、インクルードされたテンプレートのスコープが閉じた後に元に戻ります。
|
|
これは、インクルードされたテンプレート内で全ての変数を使用可能であるということです。
|
|
しかし、インクルードされたテンプレート内での変数の変更は
|
|
<varname>{include}</varname>
|
|
の後でインクルードしている側のテンプレート内では見ることはできません。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
<link linkend="variable.template.dir"><parameter>$template_dir</parameter></link>
|
|
ディレクトリ外にあるファイルを <varname>{include}</varname> するには、
|
|
<link linkend="template.resources">テンプレートリソース</link> を指定します。
|
|
</para></listitem>
|
|
</itemizedlist>
|
|
|
|
<informaltable frame="all">
|
|
<tgroup cols="5">
|
|
<colspec colname="param" align="center" />
|
|
<colspec colname="type" align="center" />
|
|
<colspec colname="required" align="center" />
|
|
<colspec colname="default" align="center" />
|
|
<colspec colname="desc" />
|
|
<thead>
|
|
<row>
|
|
<entry>属性名</entry>
|
|
<entry>型</entry>
|
|
<entry>必須</entry>
|
|
<entry>デフォルト</entry>
|
|
<entry>概要</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>file</entry>
|
|
<entry>string</entry>
|
|
<entry>Yes</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>インクルードするテンプレートファイル名</entry>
|
|
</row>
|
|
<row>
|
|
<entry>assign</entry>
|
|
<entry>string</entry>
|
|
<entry>No</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>インクルードしたコンテンツの出力を格納する変数名</entry>
|
|
</row>
|
|
<row>
|
|
<entry>[var ...]</entry>
|
|
<entry>[var type]</entry>
|
|
<entry>No</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>ローカルからテンプレートに渡す変数</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
|
|
<example>
|
|
<title>シンプルな {include} の例</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
<html>
|
|
<head>
|
|
<title>{$title}</title>
|
|
</head>
|
|
<body>
|
|
{include file='page_header.tpl'}
|
|
|
|
{* ここにテンプレートの本体を記述します。変数 $tpl_name
|
|
はたとえば 'contact.tpl' などに置き換えられます。
|
|
*}
|
|
{include file="$tpl_name.tpl"}
|
|
|
|
{include file='page_footer.tpl'}
|
|
</body>
|
|
</html>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
|
|
<example>
|
|
<title>{include} に変数を渡す</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{include file='links.tpl' title='Newest links' links=$link_array}
|
|
{* ここにテンプレートの本体を記述します *}
|
|
{include file='footer.tpl' foo='bar'}
|
|
]]>
|
|
</programlisting>
|
|
<para>このテンプレートは、以下のような <filename>links.tpl</filename> をインクルードします。</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
<div id="box">
|
|
<h3>{$title}{/h3>
|
|
<ul>
|
|
{foreach from=$links item=l}
|
|
.. 何かを行います ...
|
|
</foreach}
|
|
</ul>
|
|
</div>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
|
|
|
|
<example>
|
|
<title>{include} と変数への割り当て</title>
|
|
<para>この例は、<filename>nav.tpl</filename>
|
|
の内容を変数 <varname>$navbar</varname> に割り当て、
|
|
ページの最初と最後に出力させるものです。
|
|
</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
<body>
|
|
{include file='nav.tpl' assign=navbar}
|
|
{include file='header.tpl' title='Smarty is cool'}
|
|
{$navbar}
|
|
{* テンプレートの本体をここへ記述します *}
|
|
{$navbar}
|
|
{include file='footer.tpl'}
|
|
</body>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
|
|
<example>
|
|
<title>さまざまな {include} リソースの例</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{* ファイルの絶対パス *}
|
|
{include file='/usr/local/include/templates/header.tpl'}
|
|
|
|
{* ファイルの絶対パス(結果は上と同じ) *}
|
|
{include file='file:/usr/local/include/templates/header.tpl'}
|
|
|
|
{* Windows環境のファイルの絶対パス(接頭辞の"file:"は必須) *}
|
|
{include file='file:C:/www/pub/templates/header.tpl'}
|
|
|
|
{* "db"と名付けられたテンプレートリソースからインクルード *}
|
|
{include file='db:header.tpl'}
|
|
|
|
{* 変数名に格納された名前のテンプレートをインクルード - 例 $module = 'contacts' *}
|
|
{include file="$module.tpl"}
|
|
|
|
{* この例は、シングルクォートでは変数が展開されないため、動作しません *}
|
|
{include file='$module.tpl'}
|
|
|
|
{* 複数の可変テンプレートをインクルード - 例 amber/links.view.tpl *}
|
|
{include file="$style_dir/$module.$view.tpl"}
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<para>
|
|
<link linkend="language.function.include.php"><varname>{include_php}</varname></link>、
|
|
<link linkend="language.function.insert"><varname>{insert}</varname></link>、
|
|
<link linkend="language.function.php"><varname>{php}</varname></link>、
|
|
<link linkend="template.resources">テンプレートリソース</link> および
|
|
<link linkend="tips.componentized.templates">コンポーネント化したテンプレート</link>
|
|
も参照してください。
|
|
</para>
|
|
</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
|
|
-->
|
|
|