mirror of
https://github.com/smarty-php/smarty.git
synced 2025-11-01 21:01:37 +01:00
471 lines
16 KiB
XML
471 lines
16 KiB
XML
<?xml version="1.0" encoding="windows-1251"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: 1.4 $ -->
|
||
<part id="getting.started">
|
||
<title>Приступая к работе</title>
|
||
|
||
<chapter id="what.is.smarty">
|
||
<title>Что такое Smarty?</title>
|
||
<para>
|
||
Smarty - это компилирующий обработчик шаблонов для PHP и на PHP.
|
||
Говоря более четко, он предоставляет один из инструментов, которые
|
||
позволяет добиться разделения прикладной логики и от
|
||
представления. Это очень удобно в ситуациях, когда программист и
|
||
верстальщик шаблона - различные люди. Например, скажем, вы создаете
|
||
страницу, которая показывает газетную статью. Название статьи, автор
|
||
и сама статья - элементы, которые не содержат никакой информации о том, как они
|
||
будут представлены. Их передают в Smarty из приложения, а верстальщик
|
||
шаблона редактирует шаблоны и использует комбинацию тэгов HTML и
|
||
тэгов шаблона, чтобы отформатировать представление этих элементов
|
||
(таблицы HTML, фоновые цвета, размеры шрифта, стиля и т.д.). Однажды
|
||
программист захочет изменить способ хранения статьи (сделать изменения
|
||
в логике приложения.) Это изменение не вызовет изменений в шаблонах.
|
||
Содержание будет все еще передаваться в шаблон таким же самым способом.
|
||
Аналогично, если верстальщик захочет полностью перепроектировать
|
||
шаблоны, это не потребует никаких изменений к прикладной логике.
|
||
</para>
|
||
<para>
|
||
Теперь коротко о том, чего не может Smarty. Он не пытается полностью
|
||
разделить логику от шаблонов. Нет никакой проблемы с логикой в ваших
|
||
шаблонах при условии, что это строго логика представления. Совет:
|
||
держите прикладную логику вне шаблонов, а логику представления вне
|
||
приложения. Так проще всего сохранить проект управляемым и расширяемым
|
||
в течение длительного времени.
|
||
</para>
|
||
<para>
|
||
Одна из уникальных возможностей Smarty - компилирование шаблонов. Это
|
||
означает, что Smarty читает файлы шаблонов и создает PHP-код на их основе.
|
||
Код создаётся один раз и потом только выполняется. Поэтому нет
|
||
необходимости обрабатывать файл шаблона для каждого запроса и каждый
|
||
шаблон может пользоваться всеми преимуществами таких кэшируюших решений,
|
||
как Zend Accelerator
|
||
(<ulink url="&url.zend;">&url.zend;</ulink>) или PHP Accelerator
|
||
(<ulink url="&url.php-accelerator;">&url.php-accelerator;</ulink>).
|
||
</para>
|
||
<para>
|
||
Некоторые особенности Smarty:
|
||
</para>
|
||
<itemizedlist>
|
||
<listitem>
|
||
<para>
|
||
Он очень быстр
|
||
</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>
|
||
Он эффективен, так как PHP обработчик делает за него грязную работу
|
||
</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>
|
||
Никакой лишней обработки шаблонов, они компилируются только один раз
|
||
</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>
|
||
Перекомпилируются только те шаблоны, которые именились
|
||
</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>
|
||
Вы можете создавать пользовательские
|
||
<link linkend="language.custom.functions">функции</link> и
|
||
<link linkend="language.modifiers">модификаторы</link>, что делает язык
|
||
шаблонов чрезвычайно расширяемым
|
||
</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>
|
||
Настраиваемые разделители тэгов шаблона, то есть вы можете
|
||
использовать {}, {{}}, <!--{}-->, и т.д..
|
||
</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>
|
||
If/elseif/else/endif конструкции передаются PHP обработчику,
|
||
так что синтаксис {if ...} выражения может быть настолько простым или
|
||
комплексным, как вам нравится
|
||
</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>
|
||
Допустимо неограниченное вложение секций, условий и т.д.
|
||
</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>
|
||
Существует возможность включения PHP-кода прямо в ваш шаблон, однако это
|
||
не рекомендуется
|
||
</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>
|
||
Встроеное кэширование
|
||
</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>
|
||
Произвольные источники шаблона
|
||
</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>
|
||
Произвольные функции обработки кэширования
|
||
</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>
|
||
Поддержка плагинов
|
||
</para>
|
||
</listitem>
|
||
</itemizedlist>
|
||
</chapter>
|
||
|
||
<chapter id="installation">
|
||
<title>Установка</title>
|
||
|
||
<sect1 id="installation.requirements">
|
||
<title>Требования</title>
|
||
<para>
|
||
Для установки и работы Smarty необходим веб-сервер с установленным PHP версии 4.0.6 или выше.
|
||
</para>
|
||
</sect1>
|
||
<sect1 id="installing.smarty.basic">
|
||
<title>Базовая инсталяция</title>
|
||
<para>
|
||
Скопируйте файлы Smarty, которые находятся в директории /libs/ дистрибутива.
|
||
НЕ МЕНЯЙТЕ эти файлы. Использйте возможности внешней настройки вместо
|
||
этого.
|
||
</para>
|
||
<example>
|
||
<title>Файлы Smarty</title>
|
||
<screen>
|
||
<![CDATA[
|
||
Smarty.class.php
|
||
Smarty_Compiler.class.php
|
||
Config_File.class.php
|
||
debug.tpl
|
||
/plugins/*.php (все файлы!)
|
||
/core/*.php (все файлы!)
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
<para>
|
||
Smarty использует PHP константу <link
|
||
linkend="constant.smarty.dir">SMARTY_DIR</link>, которая указывает путь к
|
||
файлам Smarty. Обычно, если приложение может найти файл
|
||
<emphasis>Smarty.class.php</emphasis>, то нет необходимости устанавливать
|
||
SMARTY_DIR. Однако, если <emphasis>Smarty.class.php</emphasis> не может
|
||
быть найден в вашем include_path или вы не указывали абсолютный путь к нему в
|
||
приложении, то вы должны определить SMARTY_DIR вручную.
|
||
SMARTY_DIR <emphasis>должен</emphasis> включать завершающий слэш.
|
||
</para>
|
||
<para>
|
||
Как следует инстанциировать объект Smarty в ваших PHP-скриптах:
|
||
</para>
|
||
<example>
|
||
<title>Создание обекта Smarty</title>
|
||
<screen>
|
||
<![CDATA[
|
||
require('Smarty.class.php');
|
||
$smarty = new Smarty;
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
<para>
|
||
Попробуйте выполнить вышеуказанный код. Если Вы получаете ошибку о том,
|
||
что <emphasis>Smarty.class.php</emphasis> не найден, попробуйте следующие
|
||
варианты:
|
||
</para>
|
||
|
||
<example>
|
||
<title>Укажите абсолютный путь к директории Smarty</title>
|
||
<screen>
|
||
<![CDATA[
|
||
require('/usr/local/lib/php/Smarty/Smarty.class.php');
|
||
$smarty = new Smarty;
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<example>
|
||
<title>Добавьте директорию Smarty к include_path</title>
|
||
<screen>
|
||
<![CDATA[
|
||
// Отредактируйте ваш php.ini файл, добавьте путь к Smarty
|
||
// к include_path и перезапустите веб сервер.
|
||
// Следующий код должен работать без ошибок:
|
||
require('Smarty.class.php');
|
||
$smarty = new Smarty;
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<example>
|
||
<title>Установите значение константы SMARTY_DIR</title>
|
||
<screen>
|
||
<![CDATA[
|
||
define('SMARTY_DIR','/usr/local/lib/php/Smarty/');
|
||
require(SMARTY_DIR.'Smarty.class.php');
|
||
$smarty = new Smarty;
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
|
||
<para>
|
||
Теперь, когда все файлы находятся на своих местах, пришло время
|
||
установки директорий Smarty в вашем приложении. Smarty требует
|
||
четыре директории, которые по умолчанию называются
|
||
<emphasis>templates</emphasis>, <emphasis>templates_c</emphasis>,
|
||
<emphasis>configs</emphasis> и <emphasis>cache</emphasis>. Каждая из
|
||
них определяется свойствами класса Smarty: <emphasis>$template_dir</emphasis>,
|
||
<emphasis>$compile_dir</emphasis>, <emphasis>$config_dir</emphasis>, и
|
||
<emphasis>$cache_dir</emphasis> соответственно. Настойчиво рекомендуется
|
||
использовать разные наборы этих директорий для каждого приложения,
|
||
использующего Smarty.
|
||
</para>
|
||
<para>
|
||
Убедитесь, что вы знаете значение DOCUMENT_ROOT вашего веб-сервера.
|
||
В нашем примере это "/web/www.mydomain.com/docs/".
|
||
Так как к файлам Smarty будут обращаться только скрипты, то вам
|
||
рекомендуется вынести директории Smarty <emphasis>за пределы</emphasis>
|
||
DOCUMENT_ROOT, чтобы избежать лишнего беспокойства.
|
||
</para>
|
||
<para>
|
||
В нашем примере мы будем устанавливать Smarty для некоторой гостевой
|
||
книги. Приложение было выбрано только для того, чтобы использовать его
|
||
имя в именах директорий. Вы можете использовать те же настройки с любым
|
||
другим приложением, просто меняя "guestbook" на имя вашего приложения.
|
||
Мы же выбрали путь к директориям Smarty такой:
|
||
"/web/www.mydomain.com/smarty/guestbook/".
|
||
</para>
|
||
|
||
<!-- TO BE TRANSLATED ASAP! -->
|
||
|
||
<para>
|
||
You will need as least one file under your document root, and that is the
|
||
script accessed by the web browser. We will call our script "index.php",
|
||
and place it in a subdirectory under the document root called
|
||
"/guestbook/". It is convenient to setup the web server so that "index.php"
|
||
can be identified as the default directory index, so if you access
|
||
"http://www.mydomain.com/guestbook/", the index.php script will be executed
|
||
without "index.php" in the URL. In Apache you can set this up by adding
|
||
"index.php" onto the end of your DirectoryIndex setting (separate each
|
||
entry with a space.)
|
||
</para>
|
||
<para>
|
||
Lets take a look at the file structure so far:
|
||
</para>
|
||
<example>
|
||
<title>Example file structure</title>
|
||
<screen>
|
||
<![CDATA[
|
||
/usr/local/lib/php/Smarty/Smarty.class.php
|
||
/usr/local/lib/php/Smarty/Smarty_Compiler.class.php
|
||
/usr/local/lib/php/Smarty/Config_File.class.php
|
||
/usr/local/lib/php/Smarty/debug.tpl
|
||
/usr/local/lib/php/Smarty/plugins/*.php
|
||
|
||
/web/www.mydomain.com/smarty/guestbook/templates/
|
||
/web/www.mydomain.com/smarty/guestbook/templates_c/
|
||
/web/www.mydomain.com/smarty/guestbook/configs/
|
||
/web/www.mydomain.com/smarty/guestbook/cache/
|
||
|
||
/web/www.mydomain.com/docs/guestbook/index.php
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
<para>
|
||
Smarty will need write access to the <emphasis>$compile_dir</emphasis> and
|
||
<emphasis>$cache_dir</emphasis>, so be sure the web server user can write
|
||
to them. This is usually user "nobody" and group "nobody". For OS X users,
|
||
the default is user "www" and group "www". If you are using Apache, you can
|
||
look in your httpd.conf file (usually in "/usr/local/apache/conf/") to see
|
||
what user and group are being used.
|
||
</para>
|
||
<example>
|
||
<title>Setting file permissions</title>
|
||
<screen>
|
||
<![CDATA[
|
||
chown nobody:nobody /web/www.mydomain.com/smarty/guestbook/templates_c/
|
||
chmod 770 /web/www.mydomain.com/smarty/guestbook/templates_c/
|
||
|
||
chown nobody:nobody /web/www.mydomain.com/smarty/guestbook/cache/
|
||
chmod 770 /web/www.mydomain.com/smarty/guestbook/cache/
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
<note>
|
||
<title>Technical Note</title>
|
||
<para>
|
||
chmod 770 will be fairly tight security, it only allows user "nobody" and
|
||
group "nobody" read/write access to the directories. If you would like to
|
||
open up read access to anyone (mostly for your own convenience of viewing
|
||
these files), you can use 775 instead.
|
||
</para>
|
||
</note>
|
||
<para>
|
||
We need to create the index.tpl file that Smarty will load. This will be
|
||
located in your $template_dir.
|
||
</para>
|
||
<example>
|
||
<title>Editing /web/www.mydomain.com/smarty/guestbook/templates/index.tpl</title>
|
||
<screen>
|
||
<![CDATA[
|
||
{* Smarty *}
|
||
|
||
Hello, {$name}!
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
<note>
|
||
<title>Technical Note</title>
|
||
<para>
|
||
{* Smarty *} is a template comment. It is not required, but it is good
|
||
practice to start all your template files with this comment. It makes
|
||
the file easy to recognize regardless of the file extension. For
|
||
example, text editors could recognize the file and turn on special
|
||
syntax highlighting.
|
||
</para>
|
||
</note>
|
||
<para>
|
||
Now lets edit index.php. We'll create an instance of Smarty, assign a
|
||
template variable and display the index.tpl file. In our example
|
||
environment, "/usr/local/lib/php/Smarty" is in our include_path. Be sure you
|
||
do the same, or use absolute paths.
|
||
</para>
|
||
<example>
|
||
<title>Editing /web/www.mydomain.com/docs/guestbook/index.php</title>
|
||
<screen>
|
||
<![CDATA[
|
||
// load Smarty library
|
||
require('Smarty.class.php');
|
||
|
||
$smarty = new Smarty;
|
||
|
||
$smarty->template_dir = '/web/www.mydomain.com/smarty/guestbook/templates/';
|
||
$smarty->compile_dir = '/web/www.mydomain.com/smarty/guestbook/templates_c/';
|
||
$smarty->config_dir = '/web/www.mydomain.com/smarty/guestbook/configs/';
|
||
$smarty->cache_dir = '/web/www.mydomain.com/smarty/guestbook/cache/';
|
||
|
||
$smarty->assign('name','Ned');
|
||
|
||
$smarty->display('index.tpl');
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
<note>
|
||
<title>Technical Note</title>
|
||
<para>
|
||
In our example, we are setting absolute paths to all of the Smarty
|
||
directories. If '/web/www.mydomain.com/smarty/guestbook/' is within your
|
||
PHP include_path, then these settings are not necessary. However, it is
|
||
more efficient and (from experience) less error-prone to set them to
|
||
absolute paths. This ensures that Smarty is getting files from the
|
||
directories you intended.
|
||
</para>
|
||
</note>
|
||
<para>
|
||
Now load the index.php file from your web browser. You should see "Hello,
|
||
Ned!"
|
||
</para>
|
||
<para>
|
||
You have completed the basic setup for Smarty!
|
||
</para>
|
||
</sect1>
|
||
<sect1 id="installing.smarty.extended">
|
||
<title>Extended Setup</title>
|
||
|
||
<para>
|
||
This is a continuation of the <link
|
||
linkend="installing.smarty.basic">basic installation</link>, please read
|
||
that first!
|
||
</para>
|
||
<para>
|
||
A slightly more flexible way to setup Smarty is to extend the class and
|
||
initialize your Smarty environment. So instead of repeatedly setting
|
||
directory paths, assigning the same vars, etc., we can do that in one place.
|
||
Lets create a new directory "/php/includes/guestbook/" and make a new file
|
||
called "setup.php". In our example environment, "/php/includes" is in our
|
||
include_path. Be sure you set this up too, or use absolute file paths.
|
||
</para>
|
||
|
||
<example>
|
||
<title>Editing /php/includes/guestbook/setup.php</title>
|
||
<screen>
|
||
<![CDATA[
|
||
// load Smarty library
|
||
require('Smarty.class.php');
|
||
|
||
// The setup.php file is a good place to load
|
||
// required application library files, and you
|
||
// can do that right here. An example:
|
||
// require('guestbook/guestbook.lib.php');
|
||
|
||
class Smarty_GuestBook extends Smarty {
|
||
|
||
function Smarty_GuestBook() {
|
||
|
||
// Class Constructor. These automatically get set with each new instance.
|
||
|
||
$this->Smarty();
|
||
|
||
$this->template_dir = '/web/www.mydomain.com/smarty/guestbook/templates/';
|
||
$this->compile_dir = '/web/www.mydomain.com/smarty/guestbook/templates_c/';
|
||
$this->config_dir = '/web/www.mydomain.com/smarty/guestbook/configs/';
|
||
$this->cache_dir = '/web/www.mydomain.com/smarty/guestbook/cache/';
|
||
|
||
$this->caching = true;
|
||
$this->assign('app_name','Guest Book');
|
||
}
|
||
|
||
}
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
<para>
|
||
Now lets alter the index.php file to use setup.php:
|
||
</para>
|
||
<example>
|
||
<title>Editing /web/www.mydomain.com/docs/guestbook/index.php</title>
|
||
<screen>
|
||
<![CDATA[
|
||
require('guestbook/setup.php');
|
||
|
||
$smarty = new Smarty_GuestBook;
|
||
|
||
$smarty->assign('name','Ned');
|
||
|
||
$smarty->display('index.tpl');
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
<para>
|
||
Now you see it is quite simple to bring up an instance of Smarty, just use
|
||
Smarty_GuestBook which automatically initializes everything for our application.
|
||
</para>
|
||
</sect1>
|
||
</chapter>
|
||
</part>
|
||
|
||
<!-- 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
|
||
-->
|