Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

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