| 148 |
lars |
1 |
Tips & Tricks {#tips}
|
|
|
2 |
=============
|
|
|
3 |
|
|
|
4 |
Blank Variable Handling {#tips.blank.var.handling}
|
|
|
5 |
=======================
|
|
|
6 |
|
|
|
7 |
There may be times when you want to print a default value for an empty
|
|
|
8 |
variable instead of printing nothing, such as printing ` ` so that
|
|
|
9 |
html table backgrounds work properly. Many would use an
|
|
|
10 |
[`{if}`](#language.function.if) statement to handle this, but there is a
|
|
|
11 |
shorthand way with Smarty, using the
|
|
|
12 |
[`default`](#language.modifier.default) variable modifier.
|
|
|
13 |
|
|
|
14 |
> **Note**
|
|
|
15 |
>
|
|
|
16 |
> "Undefined variable" errors will show an E\_NOTICE if not disabled in
|
|
|
17 |
> PHP\'s [`error_reporting()`](&url.php-manual;error_reporting) level or
|
|
|
18 |
> Smarty\'s [`$error_reporting`](#variable.error.reporting) property and
|
|
|
19 |
> a variable had not been assigned to Smarty.
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
{* the long way *}
|
|
|
23 |
{if $title eq ''}
|
|
|
24 |
|
|
|
25 |
{else}
|
|
|
26 |
{$title}
|
|
|
27 |
{/if}
|
|
|
28 |
|
|
|
29 |
{* the short way *}
|
|
|
30 |
{$title|default:' '}
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
See also [`default`](#language.modifier.default) modifier and [default
|
|
|
35 |
variable handling](#tips.default.var.handling).
|
|
|
36 |
|
|
|
37 |
Default Variable Handling {#tips.default.var.handling}
|
|
|
38 |
=========================
|
|
|
39 |
|
|
|
40 |
If a variable is used frequently throughout your templates, applying the
|
|
|
41 |
[`default`](#language.modifier.default) modifier every time it is
|
|
|
42 |
mentioned can get a bit ugly. You can remedy this by assigning the
|
|
|
43 |
variable its default value with the
|
|
|
44 |
[`{assign}`](#language.function.assign) function.
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
{* do this somewhere at the top of your template *}
|
|
|
48 |
{assign var='title' value=$title|default:'no title'}
|
|
|
49 |
|
|
|
50 |
{* if $title was empty, it now contains the value "no title" when you use it *}
|
|
|
51 |
{$title}
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
See also [`default`](#language.modifier.default) modifier and [blank
|
|
|
56 |
variable handling](#tips.blank.var.handling).
|
|
|
57 |
|
|
|
58 |
Passing variable title to header template {#tips.passing.vars}
|
|
|
59 |
=========================================
|
|
|
60 |
|
|
|
61 |
When the majority of your templates use the same headers and footers, it
|
|
|
62 |
is common to split those out into their own templates and
|
|
|
63 |
[`{include}`](#language.function.include) them. But what if the header
|
|
|
64 |
needs to have a different title, depending on what page you are coming
|
|
|
65 |
from? You can pass the title to the header as an
|
|
|
66 |
[attribute](#language.syntax.attributes) when it is included.
|
|
|
67 |
|
|
|
68 |
`mainpage.tpl` - When the main page is drawn, the title of "Main Page"
|
|
|
69 |
is passed to the `header.tpl`, and will subsequently be used as the
|
|
|
70 |
title.
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
{include file='header.tpl' title='Main Page'}
|
|
|
74 |
{* template body goes here *}
|
|
|
75 |
{include file='footer.tpl'}
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
`archives.tpl` - When the archives page is drawn, the title will be
|
|
|
80 |
"Archives". Notice in the archive example, we are using a variable from
|
|
|
81 |
the `archives_page.conf` file instead of a hard coded variable.
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
{config_load file='archive_page.conf'}
|
|
|
85 |
|
|
|
86 |
{include file='header.tpl' title=#archivePageTitle#}
|
|
|
87 |
{* template body goes here *}
|
|
|
88 |
{include file='footer.tpl'}
|
|
|
89 |
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
`header.tpl` - Notice that "Smarty News" is printed if the `$title`
|
|
|
93 |
variable is not set, using the [`default`](#language.modifier.default)
|
|
|
94 |
variable modifier.
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
<html>
|
|
|
98 |
<head>
|
|
|
99 |
<title>{$title|default:'Smarty News'}</title>
|
|
|
100 |
</head>
|
|
|
101 |
<body>
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
`footer.tpl`
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
</body>
|
|
|
109 |
</html>
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
Dates {#tips.dates}
|
|
|
114 |
=====
|
|
|
115 |
|
|
|
116 |
As a rule of thumb, always pass dates to Smarty as
|
|
|
117 |
[timestamps](&url.php-manual;time). This allows template designers to
|
|
|
118 |
use the [`date_format`](#language.modifier.date.format) modifier for
|
|
|
119 |
full control over date formatting, and also makes it easy to compare
|
|
|
120 |
dates if necessary.
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
{$startDate|date_format}
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
This will output:
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
Jan 4, 2009
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
{$startDate|date_format:"%Y/%m/%d"}
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
This will output:
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
2009/01/04
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
Dates can be compared in the template by timestamps with:
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
{if $order_date < $invoice_date}
|
|
|
150 |
...do something..
|
|
|
151 |
{/if}
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
When using [`{html_select_date}`](#language.function.html.select.date)
|
|
|
156 |
in a template, the programmer will most likely want to convert the
|
|
|
157 |
output from the form back into timestamp format. Here is a function to
|
|
|
158 |
help you with that.
|
|
|
159 |
|
|
|
160 |
|
|
|
161 |
<?php
|
|
|
162 |
|
|
|
163 |
// this assumes your form elements are named
|
|
|
164 |
// startDate_Day, startDate_Month, startDate_Year
|
|
|
165 |
|
|
|
166 |
$startDate = makeTimeStamp($startDate_Year, $startDate_Month, $startDate_Day);
|
|
|
167 |
|
|
|
168 |
function makeTimeStamp($year='', $month='', $day='')
|
|
|
169 |
{
|
|
|
170 |
if(empty($year)) {
|
|
|
171 |
$year = strftime('%Y');
|
|
|
172 |
}
|
|
|
173 |
if(empty($month)) {
|
|
|
174 |
$month = strftime('%m');
|
|
|
175 |
}
|
|
|
176 |
if(empty($day)) {
|
|
|
177 |
$day = strftime('%d');
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
return mktime(0, 0, 0, $month, $day, $year);
|
|
|
181 |
}
|
|
|
182 |
?>
|
|
|
183 |
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
See also [`{html_select_date}`](#language.function.html.select.date),
|
|
|
187 |
[`{html_select_time}`](#language.function.html.select.time),
|
|
|
188 |
[`date_format`](#language.modifier.date.format) and
|
|
|
189 |
[`$smarty.now`](#language.variables.smarty.now),
|
|
|
190 |
|
|
|
191 |
Componentized Templates {#tips.componentized.templates}
|
|
|
192 |
=======================
|
|
|
193 |
|
|
|
194 |
Traditionally, programming templates into your applications goes as
|
|
|
195 |
follows: First, you accumulate your variables within your PHP
|
|
|
196 |
application, (maybe with database queries.) Then, you instantiate your
|
|
|
197 |
Smarty object, [`assign()`](#api.assign) the variables and
|
|
|
198 |
[`display()`](#api.display) the template. So lets say for example we
|
|
|
199 |
have a stock ticker on our template. We would collect the stock data in
|
|
|
200 |
our application, then assign these variables in the template and display
|
|
|
201 |
it. Now wouldn't it be nice if you could add this stock ticker to any
|
|
|
202 |
application by merely including the template, and not worry about
|
|
|
203 |
fetching the data up front?
|
|
|
204 |
|
|
|
205 |
You can do this by writing a custom plugin for fetching the content and
|
|
|
206 |
assigning it to a template variable.
|
|
|
207 |
|
|
|
208 |
`function.load_ticker.php` - drop file in
|
|
|
209 |
[`$plugins directory`](#variable.plugins.dir)
|
|
|
210 |
|
|
|
211 |
|
|
|
212 |
<?php
|
|
|
213 |
|
|
|
214 |
// setup our function for fetching stock data
|
|
|
215 |
function fetch_ticker($symbol)
|
|
|
216 |
{
|
|
|
217 |
// put logic here that fetches $ticker_info
|
|
|
218 |
// from some ticker resource
|
|
|
219 |
return $ticker_info;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
function smarty_function_load_ticker($params, $smarty)
|
|
|
223 |
{
|
|
|
224 |
// call the function
|
|
|
225 |
$ticker_info = fetch_ticker($params['symbol']);
|
|
|
226 |
|
|
|
227 |
// assign template variable
|
|
|
228 |
$smarty->assign($params['assign'], $ticker_info);
|
|
|
229 |
}
|
|
|
230 |
?>
|
|
|
231 |
|
|
|
232 |
|
|
|
233 |
|
|
|
234 |
`index.tpl`
|
|
|
235 |
|
|
|
236 |
|
|
|
237 |
{load_ticker symbol='SMARTY' assign='ticker'}
|
|
|
238 |
|
|
|
239 |
Stock Name: {$ticker.name} Stock Price: {$ticker.price}
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
See also: [`{include}`](#language.function.include).
|
|
|
244 |
|
|
|
245 |
Obfuscating E-mail Addresses {#tips.obfuscating.email}
|
|
|
246 |
============================
|
|
|
247 |
|
|
|
248 |
Do you ever wonder how your email address gets on so many spam mailing
|
|
|
249 |
lists? One way spammers collect email addresses is from web pages. To
|
|
|
250 |
help combat this problem, you can make your email address show up in
|
|
|
251 |
scrambled javascript in the HTML source, yet it it will look and work
|
|
|
252 |
correctly in the browser. This is done with the
|
|
|
253 |
[`{mailto}`](#language.function.mailto) plugin.
|
|
|
254 |
|
|
|
255 |
|
|
|
256 |
<div id="contact">Send inquiries to
|
|
|
257 |
{mailto address=$EmailAddress encode='javascript' subject='Hello'}
|
|
|
258 |
</div>
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
|
|
|
262 |
> **Note**
|
|
|
263 |
>
|
|
|
264 |
> This method isn\'t 100% foolproof. A spammer could conceivably program
|
|
|
265 |
> his e-mail collector to decode these values, but not likely\....
|
|
|
266 |
> hopefully..yet \... wheres that quantum computer :-?.
|
|
|
267 |
|
|
|
268 |
See also [`escape`](#language.modifier.escape) modifier and
|
|
|
269 |
[`{mailto}`](#language.function.mailto).
|