Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 148 Revision 991
Zeile 1... Zeile 1...
1
Tips & Tricks {#tips}
1
# Tips & Tricks
2
=============
-
 
Zeile 3... Zeile 2...
3
 
2
 
4
Blank Variable Handling {#tips.blank.var.handling}
-
 
Zeile 5... Zeile 3...
5
=======================
3
## Blank Variable Handling
6
 
4
 
7
There may be times when you want to print a default value for an empty
5
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
6
variable instead of printing nothing, such as printing ` ` so that
9
html table backgrounds work properly. Many would use an
7
html table backgrounds work properly. Many would use an
10
[`{if}`](#language.function.if) statement to handle this, but there is a
8
[`{if}`](../designers/language-builtin-functions/language-function-if.md) statement to handle this, but there is a
Zeile 11... Zeile 9...
11
shorthand way with Smarty, using the
9
shorthand way with Smarty, using the
12
[`default`](#language.modifier.default) variable modifier.
10
[`default`](../designers/language-modifiers/language-modifier-default.md) variable modifier.
13
 
11
 
14
> **Note**
12
> **Note**
15
>
13
>
16
> "Undefined variable" errors will show an E\_NOTICE if not disabled in
14
> "Undefined variable" errors will show an E\_NOTICE if not disabled in
Zeile -... Zeile 15...
-
 
15
> PHP's [`error_reporting()`](https://www.php.net/error_reporting) level or
Zeile 17... Zeile 16...
17
> PHP\'s [`error_reporting()`](&url.php-manual;error_reporting) level or
16
> Smarty's [`$error_reporting`](../programmers/api-variables/variable-error-reporting.md) property and
18
> Smarty\'s [`$error_reporting`](#variable.error.reporting) property and
17
> a variable had not been assigned to Smarty.
19
> a variable had not been assigned to Smarty.
18
 
20
 
19
```smarty
Zeile 27... Zeile 26...
27
    {/if}
26
    {/if}
Zeile 28... Zeile 27...
28
 
27
 
29
    {* the short way *}
28
    {* the short way *}
Zeile 30... Zeile 29...
30
    {$title|default:' '}
29
    {$title|default:' '}
Zeile 31... Zeile 30...
31
 
30
 
32
        
31
```        
Zeile 33... Zeile 32...
33
 
32
 
34
See also [`default`](#language.modifier.default) modifier and [default
-
 
Zeile 35... Zeile 33...
35
variable handling](#tips.default.var.handling).
33
See also [`default`](../designers/language-modifiers/language-modifier-default.md) modifier and [default
36
 
34
variable handling](#default-variable-handling).
37
Default Variable Handling {#tips.default.var.handling}
35
 
38
=========================
36
## Default Variable Handling
39
 
37
 
Zeile 40... Zeile 38...
40
If a variable is used frequently throughout your templates, applying the
38
If a variable is used frequently throughout your templates, applying the
41
[`default`](#language.modifier.default) modifier every time it is
39
[`default`](../designers/language-modifiers/language-modifier-default.md) modifier every time it is
Zeile 42... Zeile 40...
42
mentioned can get a bit ugly. You can remedy this by assigning the
40
mentioned can get a bit ugly. You can remedy this by assigning the
43
variable its default value with the
41
variable its default value with the
Zeile 44... Zeile 42...
44
[`{assign}`](#language.function.assign) function.
42
[`{assign}`](../designers/language-builtin-functions/language-function-assign.md) function.
45
 
43
 
Zeile 46... Zeile 44...
46
 
44
 
47
    {* do this somewhere at the top of your template *}
-
 
Zeile 48... Zeile 45...
48
    {assign var='title' value=$title|default:'no title'}
45
    {* do this somewhere at the top of your template *}
49
 
46
    {assign var='title' value=$title|default:'no title'}
50
    {* if $title was empty, it now contains the value "no title" when you use it *}
47
 
51
    {$title}
48
    {* if $title was empty, it now contains the value "no title" when you use it *}
52
 
49
    {$title}
53
        
50
 
Zeile 54... Zeile 51...
54
 
51
        
55
See also [`default`](#language.modifier.default) modifier and [blank
52
 
56
variable handling](#tips.blank.var.handling).
53
See also [`default`](../designers/language-modifiers/language-modifier-default.md) modifier and [blank
Zeile -... Zeile 54...
-
 
54
variable handling](#blank-variable-handling).
Zeile 57... Zeile 55...
57
 
55
 
58
Passing variable title to header template {#tips.passing.vars}
56
## Passing variable title to header template
59
=========================================
57
 
Zeile 60... Zeile 58...
60
 
58
When the majority of your templates use the same headers and footers, it
Zeile 61... Zeile 59...
61
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
62
is common to split those out into their own templates and
60
[`{include}`](../designers/language-builtin-functions/language-function-include.md) them. But what if the header
63
[`{include}`](#language.function.include) them. But what if the header
61
needs to have a different title, depending on what page you are coming
Zeile -... Zeile 62...
-
 
62
from? You can pass the title to the header as an
Zeile 64... Zeile 63...
64
needs to have a different title, depending on what page you are coming
63
[attribute](../designers/language-basic-syntax/language-syntax-attributes.md) when it is included.
Zeile 65... Zeile 64...
65
from? You can pass the title to the header as an
64
 
66
[attribute](#language.syntax.attributes) when it is included.
65
`mainpage.tpl` - When the main page is drawn, the title of "Main Page"
67
 
66
is passed to the `header.tpl`, and will subsequently be used as the
Zeile -... Zeile 67...
-
 
67
title.
Zeile 68... Zeile 68...
68
`mainpage.tpl` - When the main page is drawn, the title of "Main Page"
68
 
69
is passed to the `header.tpl`, and will subsequently be used as the
69
```smarty
70
title.
70
 
Zeile -... Zeile 71...
-
 
71
{include file='header.tpl' title='Main Page'}
Zeile 71... Zeile 72...
71
 
72
{* template body goes here *}
72
 
73
{include file='footer.tpl'}
73
    {include file='header.tpl' title='Main Page'}
74
 
74
    {* template body goes here *}
75
```
75
    {include file='footer.tpl'}
76
 
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
Zeile 77... Zeile 79...
77
        
79
the `archives_page.conf` file instead of a hard coded variable.
Zeile -... Zeile 80...
-
 
80
 
Zeile 78... Zeile 81...
78
 
81
```smarty
79
`archives.tpl` - When the archives page is drawn, the title will be
82
 
Zeile -... Zeile 83...
-
 
83
{config_load file='archive_page.conf'}
Zeile 80... Zeile 84...
80
"Archives". Notice in the archive example, we are using a variable from
84
 
81
the `archives_page.conf` file instead of a hard coded variable.
-
 
Zeile 82... Zeile 85...
82
 
85
{include file='header.tpl' title=#archivePageTitle#}
83
 
86
{* template body goes here *}
84
    {config_load file='archive_page.conf'}
87
{include file='footer.tpl'}
85
 
88
 
86
    {include file='header.tpl' title=#archivePageTitle#}
89
```
Zeile 87... Zeile 90...
87
    {* template body goes here *}
90
        
88
    {include file='footer.tpl'}
91
 
89
 
92
`header.tpl` - Notice that "Smarty News" is printed if the `$title`
Zeile 90... Zeile 93...
90
        
93
variable is not set, using the [`default`](../designers/language-modifiers/language-modifier-default.md)
Zeile -... Zeile 94...
-
 
94
variable modifier.
-
 
95
 
-
 
96
```smarty
Zeile 91... Zeile 97...
91
 
97
 
Zeile 92... Zeile -...
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
 
98
<html>
Zeile -... Zeile 99...
-
 
99
    <head>
Zeile 96... Zeile 100...
96
 
100
        <title>{$title|default:'Smarty News'}</title>
Zeile 97... Zeile 101...
97
    <html>
101
    </head>
98
    <head>
102
<body>
99
    <title>{$title|default:'Smarty News'}</title>
-
 
100
    </head>
103
    
Zeile 101... Zeile 104...
101
    <body>
104
```
Zeile -... Zeile 105...
-
 
105
        
Zeile 102... Zeile 106...
102
 
106
 
103
        
107
`footer.tpl`
104
 
108
 
Zeile 105... Zeile 109...
105
`footer.tpl`
109
```smarty
Zeile 106... Zeile 110...
106
 
110
 
107
 
111
    </body>
108
    </body>
112
</html>
109
    </html>
113
 
Zeile -... Zeile 114...
-
 
114
```
Zeile 110... Zeile 115...
110
 
115
        
Zeile 111... Zeile 116...
111
        
116
 
112
 
117
## Dates
Zeile 113... Zeile 118...
113
Dates {#tips.dates}
118
 
Zeile 114... Zeile 119...
114
=====
119
As a rule of thumb, always pass dates to Smarty as
115
 
120
[timestamps](https://www.php.net/time). This allows template designers to
116
As a rule of thumb, always pass dates to Smarty as
121
use the [`date_format`](../designers/language-modifiers/language-modifier-date-format.md) modifier for
117
[timestamps](&url.php-manual;time). This allows template designers to
122
full control over date formatting, and also makes it easy to compare
118
use the [`date_format`](#language.modifier.date.format) modifier for
123
dates if necessary.
119
full control over date formatting, and also makes it easy to compare
124
 
120
dates if necessary.
125
```smarty
121
 
126
{$startDate|date_format}
122
 
127
```
123
    {$startDate|date_format}
128
        
124
 
129
 
125
        
130
This will output:
126
 
131
 
127
This will output:
132
```
-
 
133
Jan 4, 2009
128
 
134
```
129
 
135
 
130
    Jan 4, 2009
136
```smarty
-
 
137
 
-
 
138
{$startDate|date_format:"%Y/%m/%d"}
-
 
139
 
-
 
140
```
Zeile 131... Zeile -...
131
 
-
 
132
        
-
 
133
 
-
 
134
 
-
 
135
    {$startDate|date_format:"%Y/%m/%d"}
-
 
136
 
141
        
137
        
-
 
Zeile 138... Zeile 142...
138
 
142
 
139
This will output:
143
This will output:
140
 
144
 
141
 
145
```
142
    2009/01/04
146
2009/01/04
143
 
147
```
144
        
148
 
145
 
149
Dates can be compared in the template by timestamps with:
146
Dates can be compared in the template by timestamps with:
150
 
147
 
151
```smarty
Zeile 148... Zeile 152...
148
 
152
 
149
    {if $order_date < $invoice_date}
153
{if $order_date < $invoice_date}
Zeile 150... Zeile 154...
150
       ...do something..
154
   ...do something..
151
    {/if}
155
{/if}
Zeile -... Zeile 156...
-
 
156
 
Zeile 152... Zeile 157...
152
 
157
```        
Zeile 153... Zeile 158...
153
        
158
 
154
 
159
When using [`{html_select_date}`](../designers/language-custom-functions/language-function-html-select-date.md)
155
When using [`{html_select_date}`](#language.function.html.select.date)
160
in a template, the programmer will most likely want to convert the
156
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
157
output from the form back into timestamp format. Here is a function to
162
help you with that.
158
help you with that.
163
 
159
 
164
```php
160
 
165
 
161
    <?php
166
<?php
162
 
167
 
163
    // this assumes your form elements are named
168
// this assumes your form elements are named
164
    // startDate_Day, startDate_Month, startDate_Year
169
// startDate_Day, startDate_Month, startDate_Year
165
 
170
 
166
    $startDate = makeTimeStamp($startDate_Year, $startDate_Month, $startDate_Day);
171
$startDate = makeTimeStamp($startDate_Year, $startDate_Month, $startDate_Day);
167
 
172
 
168
    function makeTimeStamp($year='', $month='', $day='')
173
function makeTimeStamp($year='', $month='', $day='')
169
    {
-
 
Zeile 170... Zeile 174...
170
       if(empty($year)) {
174
{
Zeile 171... Zeile 175...
171
           $year = strftime('%Y');
175
   if(empty($year)) {
Zeile -... Zeile 176...
-
 
176
       $year = strftime('%Y');
Zeile 172... Zeile 177...
172
       }
177
   }
Zeile 173... Zeile 178...
173
       if(empty($month)) {
178
   if(empty($month)) {
Zeile 174... Zeile 179...
174
           $month = strftime('%m');
179
       $month = strftime('%m');
Zeile 175... Zeile 180...
175
       }
180
   }
Zeile 176... Zeile 181...
176
       if(empty($day)) {
181
   if(empty($day)) {
177
           $day = strftime('%d');
-
 
Zeile 178... Zeile 182...
178
       }
182
       $day = strftime('%d');
179
 
183
   }
180
       return mktime(0, 0, 0, $month, $day, $year);
184
 
181
    }
185
   return mktime(0, 0, 0, $month, $day, $year);
182
    ?>
186
}
183
 
187
 
Zeile -... Zeile 188...
-
 
188
```
Zeile 184... Zeile 189...
184
        
189
    
185
 
190
 
186
See also [`{html_select_date}`](#language.function.html.select.date),
191
See also [`{html_select_date}`](../designers/language-custom-functions/language-function-html-select-date.md),
Zeile 187... Zeile 192...
187
[`{html_select_time}`](#language.function.html.select.time),
192
[`{html_select_time}`](../designers/language-custom-functions/language-function-html-select-time.md),
Zeile 188... Zeile 193...
188
[`date_format`](#language.modifier.date.format) and
193
[`date_format`](../designers/language-modifiers/language-modifier-date-format.md) and
189
[`$smarty.now`](#language.variables.smarty.now),
194
[`$smarty.now`](../designers/language-variables/language-variables-smarty.md#smarty-now),
190
 
195
 
191
Componentized Templates {#tips.componentized.templates}
196
## Componentized Templates
192
=======================
197
 
Zeile 193... Zeile 198...
193
 
198
Traditionally, programming templates into your applications goes as
194
Traditionally, programming templates into your applications goes as
199
follows: First, you accumulate your variables within your PHP