mirror of
https://github.com/smarty-php/smarty.git
synced 2026-02-05 06:45:07 +01:00
367 lines
9.4 KiB
XML
367 lines
9.4 KiB
XML
<?xml version="1.0" encoding="windows-1251"?>
|
|
<!-- $Revision$ -->
|
|
<chapter id="tips">
|
|
<title>Ñîâåòû</title>
|
|
<sect1 id="tips.blank.var.handling">
|
|
<title>Îáðàáîòêà ïóñòûõ ïåðåìåííûõ</title>
|
|
<para>
|
|
Èíîãäà, íàïðèìåð, äëÿ òîãî ÷òîáû ôîí òàáëèöû ðàáîòàë êîððåêòíî,
|
|
íåîáõîäèìî âûâåñòè âìåñòî ïóñòîãî çíà÷åíèÿ ïåðåìåííîé, çíà÷åíèå
|
|
ïî óìîë÷àíèþ "&nbsp;". Ìíîãèå áû èñïîëüçîâàëè êîíñòðóêöèþ {if},
|
|
íî åñòü áîëåå êîðîòêèé ïóòü â Smatry èñïîëüçóÿ <emphasis>default</emphasis>
|
|
ìîäèôèêàòîð ïåðåìåííîé.
|
|
</para>
|
|
<example>
|
|
<title>Âûâîä &nbsp;, åñëè ïåðåìåííàÿ ïóñòà</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{* äëèííûé ïóòü *}
|
|
|
|
{if $title eq ""}
|
|
|
|
{else}
|
|
{$title}
|
|
{/if}
|
|
|
|
|
|
{* êîðîòêèé ïóòü *}
|
|
|
|
{$title|default:" "}
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</sect1>
|
|
<sect1 id="tips.default.var.handling">
|
|
<title>Îáðàáîòêà ïåðåìåííûõ ïî óìëî÷àíèþ</title>
|
|
<para>
|
|
Åñëè ïåðåìåííàÿ âñòðå÷àåòñÿ ÷àñòî, òî èñïîëüçîâàíèå ìîäèôèêàòîðà
|
|
default êàæäûé ðàç ìîæíî èçáåæàòü, èñïîëüçóÿ ôóíêöèþ
|
|
<link linkend="language.function.assign">assign</link>.
|
|
</para>
|
|
<example>
|
|
<title>Íàçíà÷åíèå ïåðåìåííîé øàáëîíà çíà÷åíèÿ ïî óìîë÷àíèþ</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{* ââåðõó øàáëîíà *}
|
|
{assign var="title" value=$title|default:"no title"}
|
|
|
|
{* åñëè ïåðåìåííàÿ $title áûëà ïóñòîé, òî ñåé÷àñ îíà ñîäåðæèò "no title" *}
|
|
{$title}
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</sect1>
|
|
<sect1 id="tips.passing.vars">
|
|
<title>Ïðèñâîåíèå ïåðåìåííîé çàãîëîâêà (title) çàãîëîâêó øàáëîíà.</title>
|
|
<para>
|
|
Åñëè áîëüøèíñòâî âàøèõ øàáëîíîâ èìåþò ïîõîæèå âåðõíèå è íèæíèå
|
|
÷àñòè, òî èìååò ñìûñë âûíåñòè èõ â îòäåëüíûå ôàéëû. Íî åñëè
|
|
øàïêà äîëæíà èìåòü ðàçëè÷íûå çàãîëîâêè íà ðàçëè÷íûõ ñòðàíèöàõ?
|
|
Ñìîòðèòå ïðèìåð íèæå.
|
|
</para>
|
|
<example>
|
|
<title>Ïðèñâîåíèå ïåðåìåííîé çàãîëîâêà (title) çàãîëîâêó øàáëîíà.</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
mainpage.tpl
|
|
------------
|
|
|
|
{include file="header.tpl" title="Main Page"}
|
|
{* òåëî øàáëîíà *}
|
|
{include file="footer.tpl"}
|
|
|
|
|
|
archives.tpl
|
|
------------
|
|
|
|
{config_load file="archive_page.conf"}
|
|
{include file="header.tpl" title=#archivePageTitle#}
|
|
{* òåëî øàáëîíàe *}
|
|
{include file="footer.tpl"}
|
|
|
|
|
|
header.tpl
|
|
----------
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>{$title|default:"BC News"}</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
|
|
|
|
footer.tpl
|
|
----------
|
|
</BODY>
|
|
</HTML>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<para>
|
|
Åñëè âûâîäèòñÿ ãëàâíàÿ ñòðàíèöà, òî çàãîëîâîê áóäåò "Main Page",
|
|
åñëè àðõèâû, òî çàãîëîâê áåðåòñÿ èç ôàéëà êîíôèãóðàöèè. Åñëè
|
|
çàãîëîâîê áóäåò ïóñòîé, òî âûâåäåòñÿ çíà÷åíèå ïî óìîë÷àíèþ "BC News".
|
|
</para>
|
|
</sect1>
|
|
<sect1 id="tips.dates">
|
|
<title>Äàòû</title>
|
|
<para>
|
|
Îáû÷íî äàòû â Smarty âñåãäà ïåðåäàþòñÿ êàê âðåìåííûå ìåòêè, ÷òî
|
|
ïîçâîëÿåò ïðîåêòèðîâùèêàì øàáëîíîâ èñïîëüçîâàòü <link
|
|
linkend="language.modifier.date.format">date_format</link>
|
|
äëÿ ïîëíîãî êîíòðîëÿ íàä ôîðìàòèðîâàíèåì äàòû è òàêæå äåëàåò ëåãêèì
|
|
ñðàâíåíèå äàò òàì, ãäå ýòî íåîáõîäèìî.
|
|
</para>
|
|
<para>
|
|
ÇÀÌÅ×ÀÍÈÅ: Íà÷èíàÿ ñ âåðñèè Smarty 1.4.0 âû ìîæåòå ïåðåäàâàòü äàòó â âèäå
|
|
âðåìåííîé ìåòêè Unix (Unix timestamp), âðåìåííîé ìåòêè MySQL èëè â ëþáîì
|
|
äðóãîì âèäå, êîòîðûé ïîäåðæèâàåòñÿ ôóíêöèåé PHP strtotime().
|
|
</para>
|
|
<example>
|
|
<title>Èñïîëüçîâàíèå date_format</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{$startDate|date_format}
|
|
|
|
ÂÛÂÎÄ:
|
|
|
|
Jan 4, 2001
|
|
|
|
|
|
{$startDate|date_format:"%Y/%m/%d"}
|
|
|
|
ÂÛÂÎÄ:
|
|
|
|
2001/01/04
|
|
|
|
|
|
{if $date1 < $date2}
|
|
...
|
|
{/if}
|
|
]]></programlisting>
|
|
</example>
|
|
<para>
|
|
Êîãäà {html_select_date} èñïîëüçóåòñÿ â øàáëîíå, ïðîãðàììèñòàì ìîæåò
|
|
ïîíàäîáèòüñÿ ïîëó÷èòü äàòó âèäå âðåìåííîé ìåòêè. Âîò ôóíêöèÿ, êîòîðàÿ ïîìîæåò ýòî
|
|
ñäåëàòü.
|
|
</para>
|
|
<example>
|
|
<title>Ïðåîáðàçîâàíèå ýëåìåíòîâ ôîðìû ââîäà äàòû íàçàä ê âðåìåííîé ìåòêå</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
// Ïðåäïîëàãàåòñÿ, ÷òî âàøè ýëåìåíòû ôîðìû íàçâàíû
|
|
// startDate_Day, startDate_Month, startDate_Year
|
|
|
|
$startDate = makeTimeStamp($startDate_Year,$startDate_Month,$startDate_Day);
|
|
|
|
function makeTimeStamp($year="",$month="",$day="")
|
|
{
|
|
if(empty($year))
|
|
$year = strftime("%Y");
|
|
if(empty($month))
|
|
$month = strftime("%m");
|
|
if(empty($day))
|
|
$day = strftime("%d");
|
|
|
|
return mktime(0,0,0,$month,$day,$year);
|
|
}
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</sect1>
|
|
<sect1 id="tips.wap">
|
|
<title>WAP/WML</title>
|
|
<para>
|
|
WAP/WML øàáëîíû òðåáóþò, ÷òîáû çàãîëîâîê Content-type áûë
|
|
ïåðåäàí âìåñòå ñ øàáëîíîì. Ïðîñòåéøèé ïóòü - íàïèñàòü
|
|
ïîëüçîâàòåëüñêóþ ôóíêöèþ, êîòîðàÿ áóäåò âûâîäèòü çàãîëîâêè.
|
|
Òàê êàê ìû áóäåì ïîëüçîâàòüñÿ òýãàìè insert, òî çàãîëîâêè íå áóäóò
|
|
êýøèðîâàòüñÿ. Òàê æå íè÷åãî íå äîëæíî âûâîäèòüñÿ â áðàóçåð äî
|
|
øàáëîíà, èíà÷å çàãîëîâêè ìîãóò áûòü íåïðàâèëüíî âîñïðèíÿòû.
|
|
</para>
|
|
<example>
|
|
<title>Èñïîëüçîâàíèå insert äëÿ çàïèñè WML Content-Type çàãîëîâêîâ</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
// ïîìåñòèòå ýòó ôóíêöèþ â ïðèëîæåíèå, èëè â Smarty.addons.php
|
|
function insert_header() {
|
|
// this function expects $content argument
|
|
extract(func_get_arg(0));
|
|
if(empty($content))
|
|
return;
|
|
header($content);
|
|
return;
|
|
}
|
|
|
|
// øàáëîí ÄÎËÆÅÍ íà÷èíàòüñÿ ñ òýãà insert. Ïðèìåð:
|
|
{insert name=header content="Content-Type: text/vnd.wap.wml"}
|
|
|
|
<?xml version="1.0"?>
|
|
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
|
|
|
|
<!-- begin new wml deck -->
|
|
<wml>
|
|
<!-- begin first card -->
|
|
<card>
|
|
<do type="accept">
|
|
<go href="#two"/>
|
|
</do>
|
|
<p>
|
|
Welcome to WAP with Smarty!
|
|
Press OK to continue...
|
|
</p>
|
|
</card>
|
|
<!-- begin second card -->
|
|
<card id="two">
|
|
<p>
|
|
Pretty easy isn't it?
|
|
</p>
|
|
</card>
|
|
</wml>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</sect1>
|
|
<sect1 id="tips.componentized.templates">
|
|
<title>Ñîñòàâíûå øàáëîíû</title>
|
|
<para>
|
|
This tip is a bit of a hack, but still a neat idea. Use at your own
|
|
risk. ;-)
|
|
</para>
|
|
<para>
|
|
Traditionally, programming templates into your applications goes as
|
|
follows: First, you accumulate your variables within your PHP
|
|
application, (maybe with database queries.) Then, you instantiate your
|
|
Smarty object, assign the variables and display the template. So lets
|
|
say for example we have a stock ticker on our template. We would
|
|
collect the stock data in our application, then assign these variables
|
|
in the template and display it. Now wouldn't it be nice if you could
|
|
add this stock ticker to any application by merely including the
|
|
template, and not worry about fetching the data up front?
|
|
</para>
|
|
<para>
|
|
You can embed PHP into your templates with the {php}{/php} tags.
|
|
With this, you can setup self contained templates with their own
|
|
data structures for assigning their own variables. With the logic
|
|
embedded like this, you can keep the template & logic together. This
|
|
way no matter where the template source is coming from, it is always
|
|
together as one component.
|
|
</para>
|
|
<example>
|
|
<title>componentized template</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{* Smarty *}
|
|
|
|
{php}
|
|
|
|
// setup our function for fetching stock data
|
|
function fetch_ticker($symbol,&$ticker_name,&$ticker_price) {
|
|
// put logic here that fetches $ticker_name
|
|
// and $ticker_price from some resource
|
|
}
|
|
|
|
// call the function
|
|
fetch_ticker("YHOO",$ticker_name,$ticker_price);
|
|
|
|
// assign template variables
|
|
$this->assign("ticker_name",$ticker_name);
|
|
$this->assign("ticker_price",$ticker_price);
|
|
|
|
{/php}
|
|
|
|
Stock Name: {$ticker_name} Stock Price: {$ticker_price}
|
|
]]></programlisting>
|
|
</example>
|
|
<para>
|
|
As of Smarty 1.5.0, there is even a cleaner way. You can include php in
|
|
your templates with the {include_php ...} tag. This way you can keep
|
|
your PHP logic separated from the template logic. See the <link
|
|
linkend="language.function.include.php">include_php</link> function for
|
|
more information.
|
|
</para>
|
|
<example>
|
|
<title>componentized template with include_php</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
load_ticker.php
|
|
---------------
|
|
|
|
<?php
|
|
// setup our function for fetching stock data
|
|
function fetch_ticker($symbol,&$ticker_name,&$ticker_price) {
|
|
// put logic here that fetches $ticker_name
|
|
// and $ticker_price from some resource
|
|
}
|
|
|
|
// call the function
|
|
fetch_ticker("YHOO",$ticker_name,$ticker_price);
|
|
|
|
// assign template variables
|
|
$this->assign("ticker_name",$ticker_name);
|
|
$this->assign("ticker_price",$ticker_price);
|
|
?>
|
|
|
|
|
|
index.tpl
|
|
---------
|
|
|
|
{* Smarty *}
|
|
|
|
{include_php file="load_ticker.php"}
|
|
|
|
Stock Name: {$ticker_name} Stock Price: {$ticker_price}
|
|
]]></programlisting>
|
|
</example>
|
|
</sect1>
|
|
<sect1 id="tips.obfuscating.email">
|
|
<title>Obfuscating E-mail Addresses</title>
|
|
<para>
|
|
Do you ever wonder how your E-mail address gets on so many spam mailing
|
|
lists? One way spammers collect E-mail addresses is from web pages. To
|
|
help combat this problem, you can make your E-mail address show up in
|
|
scrambled javascript in the HTML source, yet it it will look and work
|
|
correctly in the browser. This is done with the mailto plugin.
|
|
</para>
|
|
<example>
|
|
<title>Example of Obfuscating an E-mail Address</title>
|
|
<programlisting>
|
|
<![CDATA[
|
|
index.tpl
|
|
---------
|
|
|
|
Send inquiries to
|
|
{mailto address=$EmailAddress encode="javascript" subject="Hello"}
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<note>
|
|
<title>Technical Note</title>
|
|
<para>
|
|
This method isn't 100% foolproof. A spammer could conceivably program his
|
|
e-mail collector to decode these values, but not likely.
|
|
</para>
|
|
</note>
|
|
</sect1>
|
|
</chapter>
|
|
<!-- 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
|
|
-->
|
|
|