aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/smarty/smarty/docs/appendixes/tips.md
blob: b0ea40cc7079e65c8b9f85e31e97de086e1ed048 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
Tips & Tricks {#tips}
=============

Blank Variable Handling {#tips.blank.var.handling}
=======================

There may be times when you want to print a default value for an empty
variable instead of printing nothing, such as printing ` ` so that
html table backgrounds work properly. Many would use an
[`{if}`](#language.function.if) statement to handle this, but there is a
shorthand way with Smarty, using the
[`default`](#language.modifier.default) variable modifier.

> **Note**
>
> "Undefined variable" errors will show an E\_NOTICE if not disabled in
> PHP\'s [`error_reporting()`](&url.php-manual;error_reporting) level or
> Smarty\'s [`$error_reporting`](#variable.error.reporting) property and
> a variable had not been assigned to Smarty.


    {* the long way *}
    {if $title eq ''}
        
    {else}
       {$title}
    {/if}

    {* the short way *}
    {$title|default:' '}

        

See also [`default`](#language.modifier.default) modifier and [default
variable handling](#tips.default.var.handling).

Default Variable Handling {#tips.default.var.handling}
=========================

If a variable is used frequently throughout your templates, applying the
[`default`](#language.modifier.default) modifier every time it is
mentioned can get a bit ugly. You can remedy this by assigning the
variable its default value with the
[`{assign}`](#language.function.assign) function.


    {* do this somewhere at the top of your template *}
    {assign var='title' value=$title|default:'no title'}

    {* if $title was empty, it now contains the value "no title" when you use it *}
    {$title}

        

See also [`default`](#language.modifier.default) modifier and [blank
variable handling](#tips.blank.var.handling).

Passing variable title to header template {#tips.passing.vars}
=========================================

When the majority of your templates use the same headers and footers, it
is common to split those out into their own templates and
[`{include}`](#language.function.include) them. But what if the header
needs to have a different title, depending on what page you are coming
from? You can pass the title to the header as an
[attribute](#language.syntax.attributes) when it is included.

`mainpage.tpl` - When the main page is drawn, the title of "Main Page"
is passed to the `header.tpl`, and will subsequently be used as the
title.


    {include file='header.tpl' title='Main Page'}
    {* template body goes here *}
    {include file='footer.tpl'}

        

`archives.tpl` - When the archives page is drawn, the title will be
"Archives". Notice in the archive example, we are using a variable from
the `archives_page.conf` file instead of a hard coded variable.


    {config_load file='archive_page.conf'}

    {include file='header.tpl' title=#archivePageTitle#}
    {* template body goes here *}
    {include file='footer.tpl'}

        

`header.tpl` - Notice that "Smarty News" is printed if the `$title`
variable is not set, using the [`default`](#language.modifier.default)
variable modifier.


    <html>
    <head>
    <title>{$title|default:'Smarty News'}</title>
    </head>
    <body>

        

`footer.tpl`


    </body>
    </html>

        

Dates {#tips.dates}
=====

As a rule of thumb, always pass dates to Smarty as
[timestamps](&url.php-manual;time). This allows template designers to
use the [`date_format`](#language.modifier.date.format) modifier for
full control over date formatting, and also makes it easy to compare
dates if necessary.


    {$startDate|date_format}

        

This will output:


    Jan 4, 2009

        


    {$startDate|date_format:"%Y/%m/%d"}

        

This will output:


    2009/01/04

        

Dates can be compared in the template by timestamps with:


    {if $order_date < $invoice_date}
       ...do something..
    {/if}

        

When using [`{html_select_date}`](#language.function.html.select.date)
in a template, the programmer will most likely want to convert the
output from the form back into timestamp format. Here is a function to
help you with that.


    <?php

    // this assumes your form elements are named
    // 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);
    }
    ?>

        

See also [`{html_select_date}`](#language.function.html.select.date),
[`{html_select_time}`](#language.function.html.select.time),
[`date_format`](#language.modifier.date.format) and
[`$smarty.now`](#language.variables.smarty.now),

WAP/WML {#tips.wap}
=======

WAP/WML templates require a php [Content-Type
header](&url.php-manual;header) to be passed along with the template.
The easist way to do this would be to write a custom function that
prints the header. If you are using [caching](#caching), that won\'t
work so we\'ll do it using the [`{insert}`](#language.function.insert)
tag; remember `{insert}` tags are not cached! Be sure that there is
nothing output to the browser before the template, or else the header
may fail.


    <?php

    // be sure apache is configure for the .wml extensions!
    // put this function somewhere in your application, or in Smarty.addons.php
    function insert_header($params)
    {
       // this function expects $content argument
       if (empty($params['content'])) {
           return;
       }
       header($params['content']);
       return;
    }

    ?>

        

your Smarty template *must* begin with the insert tag :


    {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>

        

Componentized Templates {#tips.componentized.templates}
=======================

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()`](#api.assign) the variables and
[`display()`](#api.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?

You can do this by writing a custom plugin for fetching the content and
assigning it to a template variable.

`function.load_ticker.php` - drop file in
[`$plugins directory`](#variable.plugins.dir)


    <?php

    // setup our function for fetching stock data
    function fetch_ticker($symbol)
    {
       // put logic here that fetches $ticker_info
       // from some ticker resource
       return $ticker_info;
    }

    function smarty_function_load_ticker($params, $smarty)
    {
       // call the function
       $ticker_info = fetch_ticker($params['symbol']);

       // assign template variable
       $smarty->assign($params['assign'], $ticker_info);
    }
    ?>

        

`index.tpl`


    {load_ticker symbol='SMARTY' assign='ticker'}

    Stock Name: {$ticker.name} Stock Price: {$ticker.price}

        

See also [`{include_php}`](#language.function.include.php),
[`{include}`](#language.function.include) and
[`{php}`](#language.function.php).

Obfuscating E-mail Addresses {#tips.obfuscating.email}
============================

Do you ever wonder how your email address gets on so many spam mailing
lists? One way spammers collect email addresses is from web pages. To
help combat this problem, you can make your email 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}`](#language.function.mailto) plugin.


    <div id="contact">Send inquiries to
    {mailto address=$EmailAddress encode='javascript' subject='Hello'}
    </div>

        

> **Note**
>
> This method isn\'t 100% foolproof. A spammer could conceivably program
> his e-mail collector to decode these values, but not likely\....
> hopefully..yet \... wheres that quantum computer :-?.

See also [`escape`](#language.modifier.escape) modifier and
[`{mailto}`](#language.function.mailto).