Просмотр файла manual-ru/language.function.include.html

Размер файла: 8.82Kb
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{include}</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.1">
<link rel="home" href="index.html" title="Руководство по Smarty">
<link rel="up" href="language.builtin.functions.html" title="Chapter 7. Встроенные функции">
<link rel="prev" href="language.function.if.html" title="{if},{elseif},{else}">
<link rel="next" href="language.function.include.php.html" title="{include_php}">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">{include}</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.function.if.html">Prev</a> </td>
<th width="60%" align="center">Chapter 7. Встроенные функции</th>
<td width="20%" align="right"> <a accesskey="n" href="language.function.include.php.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="{include}">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.function.include"></a>{include}</h2></div></div></div>
<p>
  Тэги {include} используются для включения других шаблонов в текущий.
  Любые переменные, доступные в текущем шаблоне, доступны и во
  включаемом. Тэг {include} должен иметь атрибут <span class="emphasis"><em>'file'</em></span>,
  который указывает путь к ресурсу шаблона.
 </p>
<p>
  Опциональный атрибут <span class="emphasis"><em>assign</em></span> указывает, что
  результат выполнения {include} будет присвоен переменной вместо отображения.
 </p>
<p>
  Все значения присвоенных переменных восстанавливаются после того,
  как подключаемый шаблон отработал. Это значит, что вы можете использовать
  все переменные из подключающего шаблона в подключаемом, но изменения
  переменных внутри подключаемого шаблона не будут видны внутри подключающего
  шаблона после команды {include}.
 </p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col align="center">
<col align="center">
<col align="center">
<col>
</colgroup>
<thead><tr>
<th align="center">Имя атрибута</th>
<th align="center">Тип</th>
<th align="center">Обязателен</th>
<th align="center">По умолчанию</th>
<th>Описание</th>
</tr></thead>
<tbody>
<tr>
<td align="center">file</td>
<td align="center">string</td>
<td align="center">Да</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>Имя файла шаблона для включения</td>
</tr>
<tr>
<td align="center">assign</td>
<td align="center">string</td>
<td align="center">Нет</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>Имя переменной, которой присвоится вывод
    шаблона</td>
</tr>
<tr>
<td align="center">[var ...]</td>
<td align="center">[var type]</td>
<td align="center">Нет</td>
<td align="center"><span class="emphasis"><em>n/a</em></span></td>
<td>Переменные, переданные в локальную область
    включаемого шаблона</td>
</tr>
</tbody>
</table></div>
<div class="example">
<a name="id2672100"></a><p class="title"><b>Example 7.17. Функция {include}</b></p>
<div class="example-contents"><pre class="programlisting">

&lt;html&gt;
 &lt;head&gt;
  &lt;title&gt;{$title}&lt;/title&gt;
 &lt;/head&gt;
 &lt;body&gt;
  {include file='page_header.tpl'}
  {* тут идёт тело шаблона *}
  {include file="$tpl_name.tpl"} &lt;-- заменит $tpl_name его значением
  {include file='page_footer.tpl'}
 &lt;/body&gt;
&lt;/html&gt;

  </pre></div>
</div>
<br class="example-break"><p>
  Вы также можете передать переменные в подключаемый шаблон в
  виде <a class="link" href="language.syntax.attributes.html" title="Параметры">атрибутов</a>.
  Любая переменная, переданная в подключаемый
  шаблон, доступны только в области видимости подключаемого
  файла. Переданные переменные имеют преимущество перед
  существующими переменными с аналогичными именами.
 </p>
<div class="example">
<a name="id2672148"></a><p class="title"><b>Example 7.18. передача переменных в {include}</b></p>
<div class="example-contents">
<pre class="programlisting">

{include file='header.tpl' title='Main Menu' table_bgcolor='#c0c0c0'}

{* тут идёт тело шаблона *}

{include file='footer.tpl' logo='http://my.example.com/logo.gif'}

  </pre>
<p>где header.tpl может быть</p>
<pre class="programlisting">

&lt;table border='1' width='100%' bgcolor='{$table_bgcolor|default:"#0000FF"}'&gt;
 &lt;tr&gt;
  &lt;td&gt;
   &lt;h1&gt;{$title}&lt;/h1&gt;
  &lt;/td&gt;
 &lt;/tr&gt;
&lt;/table&gt;

  </pre>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id2672178"></a><p class="title"><b>Example 7.19. {include} и присвоение переменной</b></p>
<div class="example-contents">
<p>
   Этот пример присвоит содержимое nav.tpl переменной $navbar,
   которая затем выводится сверху и снизу страницы.
  </p>
<pre class="programlisting">

&lt;body&gt;
{include file='nav.tpl' assign=navbar}
{include file='header.tpl' title='Main Menu' table_bgcolor='#effeef'}
{$navbar}

{* тут идёт тело шаблона *}

{include file='footer.tpl' logo='http://my.example.com/logo.gif'}
{$navbar}
&lt;/body&gt;

  </pre>
</div>
</div>
<br class="example-break"><p>
  Для подключения файлов вне папки
  <a class="link" href="api.variables.html#variable.template.dir" title="$template_dir">$template_dir</a>
  можно указывать файл с помощью
  <a class="link" href="template.resources.html" title="Ресурсы">ресурсов</a>.
 </p>
<div class="example">
<a name="id2672237"></a><p class="title"><b>Example 7.20. Примеры ресурсов шаблонов в {include}</b></p>
<div class="example-contents"><pre class="programlisting">

{* абсолютные пути *}
{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'}


  </pre></div>
</div>
<br class="example-break"><p>
  См. также
  <a class="link" href="language.function.include.php.html" title="{include_php}">{include_php}</a>,
  <a class="link" href="language.function.insert.html" title="{insert}">{insert}</a>,
  <a class="link" href="language.function.php.html" title="{php}">{php}</a>,
  <a class="link" href="template.resources.html" title="Ресурсы">Ресурсы</a> and
  <a class="link" href="tips.componentized.templates.html" title="Составные шаблоны">Составные шаблоны</a>.
 </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.function.if.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.builtin.functions.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.function.include.php.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">{if},{elseif},{else} </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> {include_php}</td>
</tr>
</table>
</div>
</body>
</html>