aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/smarty/smarty/docs/designers/language-builtin-functions/language-function-section.md
blob: 0bab5c71550a6b115753c59f0549855e2737409e (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
{section},{sectionelse} {#language.function.section}
=======================

A `{section}` is for looping over **sequentially indexed arrays of
data**, unlike [`{foreach}`](#language.function.foreach) which is used
to loop over a **single associative array**. Every `{section}` tag must
be paired with a closing `{/section}` tag.

> **Note**
>
> The [`{foreach}`](#language.function.foreach) loop can do everything a
> {section} loop can do, and has a simpler and easier syntax. It is
> usually preferred over the {section} loop.

> **Note**
>
> {section} loops cannot loop over associative arrays, they must be
> numerically indexed, and sequential (0,1,2,\...). For associative
> arrays, use the [`{foreach}`](#language.function.foreach) loop.

   Attribute Name    Type     Required   Default  Description
  ---------------- --------- ---------- --------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        name        string      Yes       *n/a*   The name of the section
        loop         mixed      Yes       *n/a*   Value to determine the number of loop iterations
       start        integer      No        *0*    The index position that the section will begin looping. If the value is negative, the start position is calculated from the end of the array. For example, if there are seven values in the loop array and start is -2, the start index is 5. Invalid values (values outside of the length of the loop array) are automatically truncated to the closest valid value.
        step        integer      No        *1*    The step value that will be used to traverse the loop array. For example, step=2 will loop on index 0,2,4, etc. If step is negative, it will step through the array backwards.
        max         integer      No       *n/a*   Sets the maximum number of times the section will loop.
        show        boolean      No      *TRUE*   Determines whether or not to show this section

**Option Flags:**

    Name    Description
  --------- ------------------------------------------
   nocache  Disables caching of the `{section}` loop

-   Required attributes are `name` and `loop`.

-   The `name` of the `{section}` can be anything you like, made up of
    letters, numbers and underscores, like [PHP
    variables](&url.php-manual;language.variables).

-   {section}\'s can be nested, and the nested `{section}` names must be
    unique from each other.

-   The `loop` attribute, usually an array of values, determines the
    number of times the `{section}` will loop. You can also pass an
    integer as the loop value.

-   When printing a variable within a `{section}`, the `{section}`
    `name` must be given next to variable name within \[brackets\].

-   `{sectionelse}` is executed when there are no values in the loop
    variable.

-   A `{section}` also has its own variables that handle `{section}`
    properties. These properties are accessible as:
    [`{$smarty.section.name.property}`](#language.variables.smarty.loops)
    where "name" is the attribute `name`.

-   `{section}` properties are [`index`](#section.property.index),
    [`index_prev`](#section.property.index.prev),
    [`index_next`](#section.property.index.next),
    [`iteration`](#section.property.iteration),
    [`first`](#section.property.first),
    [`last`](#section.property.last),
    [`rownum`](#section.property.rownum),
    [`loop`](#section.property.loop), [`show`](#section.property.show),
    [`total`](#section.property.total).

[`assign()`](#api.assign) an array to Smarty


    <?php
    $data = array(1000,1001,1002);
    $smarty->assign('custid',$data);
    ?>

The template that outputs the array


    {* this example will print out all the values of the $custid array *}
    {section name=customer loop=$custid}
    {section customer $custid} {* short-hand *}
      id: {$custid[customer]}<br />
    {/section}
    <hr />
    {*  print out all the values of the $custid array reversed *}
    {section name=foo loop=$custid step=-1}
    {section foo $custid step=-1} {* short-hand *}
      {$custid[foo]}<br />
    {/section}

      

The above example will output:


    id: 1000<br />
    id: 1001<br />
    id: 1002<br />
    <hr />
    id: 1002<br />
    id: 1001<br />
    id: 1000<br />

      


    {section name=foo start=10 loop=20 step=2}
      {$smarty.section.foo.index}
    {/section}
    <hr />
    {section name=bar loop=21 max=6 step=-2}
      {$smarty.section.bar.index}
    {/section}

The above example will output:

      
    10 12 14 16 18
    <hr />
    20 18 16 14 12 10

      

The `name` of the `{section}` can be anything you like, see [PHP
variables](&url.php-manual;language.variables). It is used to reference
the data within the `{section}`.


    {section name=anything loop=$myArray}
      {$myArray[anything].foo}
      {$name[anything]}
      {$address[anything].bar}
    {/section}

      

This is an example of printing an associative array of data with a
`{section}`. Following is the php script to assign the `$contacts` array
to Smarty.

      
    <?php
    $data = array(
              array('name' => 'John Smith', 'home' => '555-555-5555',
                    'cell' => '666-555-5555', 'email' => 'john@myexample.com'),
              array('name' => 'Jack Jones', 'home' => '777-555-5555',
                    'cell' => '888-555-5555', 'email' => 'jack@myexample.com'),
              array('name' => 'Jane Munson', 'home' => '000-555-5555',
                    'cell' => '123456', 'email' => 'jane@myexample.com')
            );
    $smarty->assign('contacts',$data);
    ?>

      

The template to output `$contacts`


    {section name=customer loop=$contacts}
    <p>
      name: {$contacts[customer].name}<br />
      home: {$contacts[customer].home}<br />
      cell: {$contacts[customer].cell}<br />
      e-mail: {$contacts[customer].email}
    </p>
    {/section}

      

The above example will output:


    <p>
      name: John Smith<br />
      home: 555-555-5555<br />
      cell: 666-555-5555<br />
      e-mail: john@myexample.com
    </p>
    <p>
      name: Jack Jones<br />
      home phone: 777-555-5555<br />
      cell phone: 888-555-5555<br />
      e-mail: jack@myexample.com
    </p>
    <p>
      name: Jane Munson<br />
      home phone: 000-555-5555<br />
      cell phone: 123456<br />
      e-mail: jane@myexample.com
    </p>

      

This example assumes that `$custid`, `$name` and `$address` are all
arrays containing the same number of values. First the php script that
assign\'s the arrays to Smarty.


    <?php

    $id = array(1001,1002,1003);
    $smarty->assign('custid',$id);

    $fullnames = array('John Smith','Jack Jones','Jane Munson');
    $smarty->assign('name',$fullnames);

    $addr = array('253 Abbey road', '417 Mulberry ln', '5605 apple st');
    $smarty->assign('address',$addr);

    ?>

The `loop` variable only determines the number of times to loop. You can
access ANY variable from the template within the `{section}`. This is
useful for looping multiple arrays. You can pass an array which will
determine the loop count by the array size, or you can pass an integer
to specify the number of loops.


    {section name=customer loop=$custid}
    <p>
      id: {$custid[customer]}<br />
      name: {$name[customer]}<br />
      address: {$address[customer]}
    </p>
    {/section}

      

The above example will output:


    <p>
      id: 1000<br />
      name: John Smith<br />
      address: 253 Abbey road
    </p>
    <p>
      id: 1001<br />
      name: Jack Jones<br />
      address: 417 Mulberry ln
    </p>
    <p>
      id: 1002<br />
      name: Jane Munson<br />
      address: 5605 apple st
    </p>

      

{section}\'s can be nested as deep as you like. With nested
{section}\'s, you can access complex data structures, such as
multi-dimensional arrays. This is an example `.php` script thats
assign\'s the arrays.


    <?php

    $id = array(1001,1002,1003);
    $smarty->assign('custid',$id);

    $fullnames = array('John Smith','Jack Jones','Jane Munson');
    $smarty->assign('name',$fullnames);

    $addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st');
    $smarty->assign('address',$addr);

    $types = array(
               array( 'home phone', 'cell phone', 'e-mail'),
               array( 'home phone', 'web'),
               array( 'cell phone')
             );
    $smarty->assign('contact_type', $types);

    $info = array(
               array('555-555-5555', '666-555-5555', 'john@myexample.com'),
               array( '123-456-4', 'www.example.com'),
               array( '0457878')
            );
    $smarty->assign('contact_info', $info);

    ?>
      

In this template, *\$contact\_type\[customer\]* is an array of contact
types for the current customer.


    {section name=customer loop=$custid}
    <hr>
      id: {$custid[customer]}<br />
      name: {$name[customer]}<br />
      address: {$address[customer]}<br />
      {section name=contact loop=$contact_type[customer]}
        {$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br />
      {/section}
    {/section}

      

The above example will output:


    <hr>
      id: 1000<br />
      name: John Smith<br />
      address: 253 N 45th<br />
        home phone: 555-555-5555<br />
        cell phone: 666-555-5555<br />
        e-mail: john@myexample.com<br />
    <hr>
      id: 1001<br />
      name: Jack Jones<br />
      address: 417 Mulberry ln<br />
        home phone: 123-456-4<br />
        web: www.example.com<br />
    <hr>
      id: 1002<br />
      name: Jane Munson<br />
      address: 5605 apple st<br />
        cell phone: 0457878<br />

      

Results of a database search (eg ADODB or PEAR) are assigned to Smarty

      
    <?php
    $sql = 'select id, name, home, cell, email from contacts '
          ."where name like '$foo%' ";
    $smarty->assign('contacts', $db->getAll($sql));
    ?>

The template to output the database result in a HTML table


    <table>
    <tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr>
    {section name=co loop=$contacts}
      <tr>
        <td><a href="view.php?id={$contacts[co].id}">view<a></td>
        <td>{$contacts[co].name}</td>
        <td>{$contacts[co].home}</td>
        <td>{$contacts[co].cell}</td>
        <td>{$contacts[co].email}</td>
      <tr>
    {sectionelse}
      <tr><td colspan="5">No items found</td></tr>
    {/section}
    </table>

.index {#section.property.index}
------

`index` contains the current array index, starting with zero or the
`start` attribute if given. It increments by one or by the `step`
attribute if given.

> **Note**
>
> If the `step` and `start` properties are not modified, then this works
> the same as the [`iteration`](#section.property.iteration) property,
> except it starts at zero instead of one.

> **Note**
>
> `$custid[customer.index]` and `$custid[customer]` are identical.


    {section name=customer loop=$custid}
      {$smarty.section.customer.index} id: {$custid[customer]}<br />
    {/section}

      

The above example will output:


    0 id: 1000<br />
    1 id: 1001<br />
    2 id: 1002<br />

       

.index\_prev {#section.property.index.prev}
------------

`index_prev` is the previous loop index. On the first loop, this is set
to -1.

.index\_next {#section.property.index.next}
------------

`index_next` is the next loop index. On the last loop, this is still one
more than the current index, respecting the setting of the `step`
attribute, if given.


    <?php
    $data = array(1001,1002,1003,1004,1005);
    $smarty->assign('rows',$data);
    ?>

Template to output the above array in a table


    {* $rows[row.index] and $rows[row] are identical in meaning *}
    <table>
      <tr>
        <th>index</th><th>id</th>
        <th>index_prev</th><th>prev_id</th>
        <th>index_next</th><th>next_id</th>
      </tr>
    {section name=row loop=$rows}
      <tr>
        <td>{$smarty.section.row.index}</td><td>{$rows[row]}</td>
        <td>{$smarty.section.row.index_prev}</td><td>{$rows[row.index_prev]}</td>
        <td>{$smarty.section.row.index_next}</td><td>{$rows[row.index_next]}</td>
      </tr>
    {/section}
    </table>

      

The above example will output a table containing the following:


    index  id    index_prev prev_id index_next next_id
    0      1001  -1                 1          1002
    1      1002  0          1001    2          1003
    2      1003  1          1002    3          1004
    3      1004  2          1003    4          1005
    4      1005  3          1004    5

       

.iteration {#section.property.iteration}
----------

`iteration` contains the current loop iteration and starts at one.

> **Note**
>
> This is not affected by the `{section}` properties `start`, `step` and
> `max`, unlike the [`index`](#section.property.index) property.
> `iteration` also starts with one instead of zero unlike `index`.
> [`rownum`](#section.property.rownum) is an alias to `iteration`, they
> are identical.


    <?php
    // array of 3000 to 3015
    $id = range(3000,3015);
    $smarty->assign('arr',$id);
    ?>

Template to output every other element of the `$arr` array as `step=2`


    {section name=cu loop=$arr start=5 step=2}
      iteration={$smarty.section.cu.iteration}
      index={$smarty.section.cu.index}
      id={$custid[cu]}<br />
    {/section}

      

The above example will output:


    iteration=1 index=5 id=3005<br />
    iteration=2 index=7 id=3007<br />
    iteration=3 index=9 id=3009<br />
    iteration=4 index=11 id=3011<br />
    iteration=5 index=13 id=3013<br />
    iteration=6 index=15 id=3015<br />

       

Another example that uses the `iteration` property to output a table
header block every five rows.


    <table>
    {section name=co loop=$contacts}
      {if $smarty.section.co.iteration is div by 5}
        <tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr>
      {/if}
      <tr>
        <td><a href="view.php?id={$contacts[co].id}">view<a></td>
        <td>{$contacts[co].name}</td>
        <td>{$contacts[co].home}</td>
        <td>{$contacts[co].cell}</td>
        <td>{$contacts[co].email}</td>
      <tr>
    {/section}
    </table>

      

An that uses the `iteration` property to alternate a text color every
third row.

      
      <table>
      {section name=co loop=$contacts}
        {if $smarty.section.co.iteration is even by 3}
          <span style="color: #ffffff">{$contacts[co].name}</span>
        {else}
          <span style="color: #dddddd">{$contacts[co].name}</span>
        {/if}
      {/section}
      </table>
      
        

> **Note**
>
> The *\"is div by\"* syntax is a simpler alternative to the PHP mod
> operator syntax. The mod operator is allowed:
> `{if $smarty.section.co.iteration % 5 == 1}` will work just the same.

> **Note**
>
> You can also use *\"is odd by\"* to reverse the alternating.

.first {#section.property.first}
------

`first` is set to TRUE if the current `{section}` iteration is the
initial one.

.last {#section.property.last}
-----

`last` is set to TRUE if the current section iteration is the final one.

This example loops the `$customers` array, outputs a header block on the
first iteration and on the last outputs the footer block. Also uses the
[`total`](#section.property.total) property.


    {section name=customer loop=$customers}
      {if $smarty.section.customer.first}
        <table>
        <tr><th>id</th><th>customer</th></tr>
      {/if}

      <tr>
        <td>{$customers[customer].id}}</td>
        <td>{$customers[customer].name}</td>
      </tr>

      {if $smarty.section.customer.last}
        <tr><td></td><td>{$smarty.section.customer.total} customers</td></tr>
        </table>
      {/if}
    {/section}

      

.rownum {#section.property.rownum}
-------

`rownum` contains the current loop iteration, starting with one. It is
an alias to [`iteration`](#section.property.iteration), they work
identically.

.loop {#section.property.loop}
-----

`loop` contains the last index number that this {section} looped. This
can be used inside or after the `{section}`.


    {section name=customer loop=$custid}
      {$smarty.section.customer.index} id: {$custid[customer]}<br />
    {/section}
    There are {$smarty.section.customer.loop} customers shown above.

      

The above example will output:


    0 id: 1000<br />
    1 id: 1001<br />
    2 id: 1002<br />
    There are 3 customers shown above.

       

.show {#section.property.show}
-----

`show` is used as a parameter to section and is a boolean value. If
FALSE, the section will not be displayed. If there is a `{sectionelse}`
present, that will be alternately displayed.

Boolean `$show_customer_info` has been passed from the PHP application,
to regulate whether or not this section shows.


    {section name=customer loop=$customers show=$show_customer_info}
      {$smarty.section.customer.rownum} id: {$customers[customer]}<br />
    {/section}

    {if $smarty.section.customer.show}
      the section was shown.
    {else}
      the section was not shown.
    {/if}

      

The above example will output:


    1 id: 1000<br />
    2 id: 1001<br />
    3 id: 1002<br />

    the section was shown.

       

.total {#section.property.total}
------

`total` contains the number of iterations that this `{section}` will
loop. This can be used inside or after a `{section}`.


    {section name=customer loop=$custid step=2}
      {$smarty.section.customer.index} id: {$custid[customer]}<br />
    {/section}
       There are {$smarty.section.customer.total} customers shown above.

      

See also [`{foreach}`](#language.function.foreach),
[`{for}`](#language.function.for), [`{while}`](#language.function.while)
and [`$smarty.section`](#language.variables.smarty.loops).