File: template_statements.rst

package info (click to toggle)
libtemplates-parser 25.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,404 kB
  • sloc: ada: 12,020; python: 658; makefile: 351; sh: 37
file content (631 lines) | stat: -rw-r--r-- 17,395 bytes parent folder | download | duplicates (3)
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

.. _Template_statements:

.. highlight:: xml

*******************
Template statements
*******************

There are five different type statements. A tag statement is surrounded
by *@@*.

.. _Comments:

Comments
========

.. index:: Command, comments
.. index:: Command, @@--

Every line starting with *@@--* are comments and are completely
ignored by the parser. The resulting page will have the exact same
format and number of lines with or without the comments::

 @@-- This template is used to display the client's data
 @@-- It uses the following tags:
 @@--
 @@--    @_CID_@       Client ID
 @@--    @_ITEMS_V_@   List of items (vector tag)

 <P>Client @_CID_@

 ...

.. _INCLUDE_statement:

INCLUDE statement
=================

.. index:: Command, INCLUDE

This statement is used to include another template file. This is useful if you
have the same header and/or footer in all your HTML pages. For example::

 @@INCLUDE@@ header.tmplt

 <P>This is by Web page

 @@INCLUDE@@ footer.tmplt

It is also possible to pass arguments to the include file. These parameters
are given after the include file name. It is possible to reference these
parameters into the included file with the special variable names
*@_$<n>_@*, where *n* is the include's parameter index (0 is
the include file name, 1 the first parameter and so on)::

 @@INCLUDE@@ another.tmplt @_VAR_@ azerty

In file :file:`another.tmplt`:

**@_$0_@**
  is another.tmplt

**@_$1_@**
  is the variable @_VAR_@

**@_$2_@**
  is the string "azerty"

If an include variable references a non existing include parameter the
tag is kept as-is.

Note that it is possible to pass the include parameters using names,
a set of positional parameters can be pass first, so all following
include commands are identical::

 @@INCLUDE@@ another.tmplt one two three four "a text"
 @@INCLUDE@@ another.tmplt (one, two, 3 => three, 4 => four, 5 => "a text")
 @@INCLUDE@@ another.tmplt (one, 5 => "a text", 3 => three, 2 => two, 4 => four)

The file name can also be a tag. In this case the file loading is deferred at
the parsing time.

For security reasons the filename can't be a full pathname. If a full
pathname is passed then the leading directory separator is removed.

.. _IF_statement:

IF statement
============

.. index:: Command, IF

This is the conditional statement. The complete form is::

 @@IF@@ <expression1>
   part1
 @@ELSIF@@ <expression2
   part2
 @@ELSE@@
   part3
 @@END_IF@

<expression> is TRUE if it evaluates to one of "TRUE", "T" or
"1" and FALSE otherwise. Note that the test is not case sensitive.

The part1 one will be parsed if expression1 evaluate to TRUE, part2
will be parsed if expression2 evaluate to TRUE and the part3 will
be parse in any other case. The `ELSIF` and `ELSE` parts are
optional.

The expression here is composed of Boolean variables
and/or Boolean expression. Recognized operators are:

.. index:: Command, IF expression

**A = B**
  Returns TRUE if A equal B

**A /= B**
  Returns TRUE if A is not equal B

**A > B**
  Returns TRUE if A greater than B. If A and B are numbers it returns the
  the number comparison (5 > 003 = TRUE) otherwise it returns the string
  comparison ('5' > '003' = FALSE).

**A >= B**
  Returns TRUE if A greater than or equal to B. See above for rule about
  numbers.

**A < B**
  Returns TRUE if A lesser than B. See above for rule about numbers.

**A <= B**
  Returns TRUE if A lesser than or equal to B. See above for rule about
  numbers.

**A and B**
  Returns TRUE if A and B is TRUE and FALSE otherwise.

**A or B**
  Returns TRUE if A or B is TRUE and FALSE otherwise.

**A xor B**
  Returns TRUE if either A or B (but not both) is TRUE and FALSE otherwise.

**A in B**
  Returns TRUE if A is found into the composite tag B and FALSE
  otherwise. B must be a tag. If B contains a single value then this
  expression is equivalent to (A = B).

**not A**
  Returns TRUE if either A is FALSE and FALSE otherwise.

**A & B**
  Returns the catenation of A and B. A and B can be either strings or variables.

The default evaluation order is done from left to right, all operators
having the same precedence. To build an expression it is possible to
use parenthesis to change the evaluation order. A value with
spaces must be quoted as a string. So valid expressions could be::

 @@IF@@ (@_VAR1_@ > 3) or (@_COND1_@ and @_COND2_@)

 @@IF@@ not (@_VAR1_@ > 3) or (@_COND1_@ and @_COND2_@)

 @@IF@@ (@_VAR1_@ > 3) and not @_COND1_@

 @@IF@@ @_VAR1_@ = "a value"

 @@IF@@ "/" & @_FILE_@ = "/filename"

Note also that variables and values can be surrounded by quotes if needed.
Quotes are needed if a value contain spaces.

Let's see an example using an `IF` tag statement. With the following
template:

.. literalinclude:: src/user.tmplt
   :language: xml

The following program:

.. literalinclude:: src/user1.adb
   :language: ada

Will display:

.. literalinclude:: build/samples/user1.adb.res
   :language: xml

But the following program:

.. literalinclude:: src/user2.adb
   :language: ada

Will display:

.. literalinclude:: build/samples/user2.adb.res
   :language: xml

.. _TABLE_statement:

TABLE statement
===============

.. index:: Command, TABLE

Table tags are useful to generate `HTML` tables for example.
Basically the code between the *@@TABLE@@* and
*@@END_TABLE@@* will be repeated as many times as the vector
tag has values. If many vector tags are specified in a table
statement, the code between the table will be repeated a number of
times equal to the maximum length of all vector tags in the
`TABLE` tag statement.

A `TABLE` tag statement is a kind of implicit iterator. This is a very
important concept to build HTML tables. Using a composite tag variable in
a *@@TABLE@@* tag statement it is possible to build very
complex Web pages.

Syntax:

.. index:: Command, TERMINATE_SECTIONS
.. index:: Command, REVERSE
.. index:: Command, TERSE

::

 @@TABLE['REVERSE]['TERMINATE_SECTIONS]['TERSE]['ALIGN_ON("sep")]@@
 ...
 [@@BEGIN@@]
 ...
 [@@SECTION@@]
 ...
 [@@END@@]
 ...
 @@END_TABLE@

Let's have an example. With the following template:

.. literalinclude:: src/table.tmplt
   :language: xml

And the following program:

.. literalinclude:: src/table.adb
   :language: ada

The following output will be generated:

.. literalinclude:: build/samples/table.adb.res
   :language: xml

Note that we use vector tag variables here. A discrete variable tag in a table
will be replaced by the same (the only one) value for each row. A vector
tag outside a table will be displayed as a list of values, each value
being separated by a specified separator. The default is a comma and a
space ", ".

The complete prototype for the `Tag` Assoc function is:

.. code-block:: ada

 function Assoc
   (Variable  : in String;
    Value     : in Tag;
    Separator : in String := Default_Separator) return Association;
 --  Build an Association (Variable = Value) to be added to Translate_Table.
 --  This is a tag association. Separator will be used when outputting the
 --  a flat representation of the Tag (outside a table statement).

A table can contain many sections. The section to use will be selected
depending on the current line. For example, a table with two sections
will use different data on even and odd lines. This is useful when you
want to alternate the line background color for a better readability
when working on HTML pages.

A table with sections can have attributes:

**REVERSE**
  The items will be displayed in the reverse order.

**TERMINATE_SECTIONS**
  This ensure that the table output will end
  with the last section. If the number of data in the vector variable tag
  is not a multiple of the number of sections then the remaining section
  will be complete with empty tag value.

**TERSE**
  Empty lines won't be output. If the composite tag used into the table has
  an empty value then the corresponding line won't be output. This is
  especially important to avoid empty ending lines for table containing
  vector of different size.

**ALIGN_ON**
  The content of table will be aligned on the given separators.
  Multiple separators may be specified as coma separated strings, for
  example ALIGN_ON(":",":="). Each line will have the corresponding
  separator aligned in the specified order. That is, on the example
  above we first align on ":" and then ":=", if another ":" is found
  on the line it is not taken into account.

For the following template:

.. literalinclude:: src/table_section.tmplt
   :language: xml

And the following program:

.. literalinclude:: src/table_section.adb
   :language: ada

The following output will be generated:

.. literalinclude:: build/samples/table_section.adb.res
   :language: xml

It is important to note that it is possible to avoid code
duplication by using the *@@BEGIN@@* and *@@END@@*
block statements. In this case only the code inside the block is part
of the section, the code outside is common to all sections. Here is
an example to generate an HTML table with different colors for each line:

The template file above can be written this way:

.. literalinclude:: src/table_block.tmplt
   :language: xml

Another example to for the **ALIGN_ON** table attribute:

.. literalinclude:: src/table_align.tmplt
   :language: text

And the following program:

.. literalinclude:: src/table_align.adb
   :language: ada

The following output will be generated:

.. literalinclude:: build/samples/table_align.adb.res
   :language: ada

Into a table construct there are some additional variable tags available:

**@_UP_TABLE_LINE_@**
  .. index:: @_UP_TABLE_LINE_@

  This tag will be replaced by the table line number of the upper table
  statement. It will be set to 0 outside a table statement or inside a
  single table statement.

**@_TABLE_LINE_@**
  .. index:: @_TABLE_LINE_@

  This tag will be replaced by the current table line number. It will be
  replaced by 0 outside a table statement.

**@_NUMBER_LINE_@**
  .. index:: @_NUMBER_LINE_@

  This is the number of line displayed in the table. It will be replaced
  by 0 outside a table statement.

**@_TABLE_LEVEL_@**
  .. index:: @_TABLE_LEVEL_@

  This is the table level number. A table construct declared in a table
  has a level value of 2.  It will be replaced by 0 outside a table statement.

Let's have a look at a more complex example with mixed IF and TABLE
statements.

Here is the template:

.. literalinclude:: src/table_if.tmplt
   :language: xml

And the following program:

.. literalinclude:: src/table_if.adb
   :language: ada

The following output will be generated:

.. literalinclude:: build/samples/table_if.adb.res
   :language: xml

Table tag statements can also be used with matrix tag or more nested
tag variables. In this case, for a tag variable with N nested levels,
the Nth closest enclosing `TABLE` tag statement will be used for
the corresponding index. If there are not enough indexes, the last
axis are just streamed as a single text value.

Let's see what happens for a matrix tag:

* Inside a table of level 2 (a TABLE statement inside a TABLE
  statement).

  In this case the first `TABLE` iterates through the matrix lines.
  First iteration will use the first matrix's vector, second
  iteration will use the second matrix's vector and so on. And the second
  `TABLE` will be used to iterate through the vector's values.

* Inside a table of level 1.

  In this case the `TABLE` iterates through the matrix lines. First
  iteration will use the first matrix's vector, second iteration will
  use the second matrix's vector and so on. Each vector is then converted to
  a string by concatenating all values using the specified separator
  (see Assoc constructor for Tag or `Set_Separator` routine).

* Outside a table statement.

  In this case the matrix is converted to a string. Each line represents
  a vector converted to a string using the supplied separator (see point
  2 above), and each vector is separated by an ASCII.LF character. The
  separators to use for each level can be specified using `Set_Separator`.

Let's look at an example, with the following template:

.. literalinclude:: src/matrix.tmplt
   :language: xml

Using the program:

.. literalinclude:: src/matrix.adb
   :language: ada

We get the following result:

.. literalinclude:: build/samples/matrix.adb.res
   :language: xml

.. _SET_statement:

SET statement
=============

.. index:: Command, SET

The `SET` command tag can be used to define a constant or an
alias for an include file parameter. This is especially important in
the context of reusable template files. For example, instead of having
many references to the **red** color in an HTML document, it is better to
define a constant `COLOR` with the value **red** and use `COLOR`
everywhere. It is then easier to change the color afterward.

The first form, to define a simple constant that can be used as any
other variable in a template file, is::

 @@SET@@ <name> = <value>

The second form, to define an alias for a template file parameter, is::

 @@SET@@ <name> = $n [| <default_value>]

In this case <name> is an alias for the Nth include parameter. In this
form it is also possible to define a default value that would be used
if the Nth include parameter is not specified.

Some examples::

 @@SET@@ COLOR = red

 @@SET@@ SIZE = $1

 @@SET@@ COLOR = $4 | green

It is important to note that a variable is set global to a template
file. It means that constants set into an include file are visible
into the parent template. This is an important feature to be able to
have a "theme" like include template file for example.

.. _INLINE_statement:

INLINE statement
================

.. index:: Command, INLINE

The `INLINE` statement can be used to better control the
result's layout. For example it is not possible to have the results of
a vector tag on the same line, also it is not possible to have a
conditional output in the middle of a line. The `INLINE` block
tag statement can be used to achieve that.

Elements in an inlined block are separated by a single space by
default. It is possible to specify any string as the separator. The
text layout on an `INLINE` block has no meaning (the lines are
trimmed on both side). As part of the inline command it is possible to
specify texts to output before and after the block.

Syntax::

 @@INLINE[(<before>)(<separator>)(<after>)]@@
 ...
 @@END_INLINE@

There are three supported uses:

**@@INLINE@@**
  In this case there is no text before and after the block and the
  separator is a single space.


**@@INLINE(<separator>)@@**
  In this case there is no text before and after the block and the
  separator is the string given as parameter *<separator>*.


**@@INLINE(<before>)(<separator>)(<after>)@@**
  In this case all three values are explicitly given.

  *<before>*, *<separator>* and *<after>* may contain control characters:

    **\\n**
      To insert a new-line (CR+LF or LF depending on the Operation System)

    **\\r**
      To insert a line-feed

    **\\\\**
      To insert a single backslash

Let's look at an example, with the following template:

.. literalinclude:: src/table_inline.tmplt
   :language: xml

Using the program:

.. literalinclude:: src/table_inline.adb
   :language: ada

We get the following result:

.. literalinclude:: build/samples/table_inline.adb.res
   :language: xml

Another example with an `IF` tag statement:

.. literalinclude:: src/if_inline.tmplt
   :language: xml

Using the program:

.. literalinclude:: src/if_inline.adb
   :language: ada

We get the following result:

.. literalinclude:: build/samples/if_inline.adb.res
   :language: xml

.. _MACRO_statement:

MACRO statement
===============

.. index:: Command, MACRO

The `MACRO` statement is used to defined macros that can be used
in other places in the template. The macro statement takes a single
parameter which is the name of the macro.

Syntax::

 @@MACRO(NAME)@@
 ...
 @@END_MACRO@

The code inside the macro can be anything supported by the templates
engine. There is no restriction. The parameters inside the macro are
referenced as @_$N_@ (where N is a number and corresponds to the Nth
parameter passed to the macro). There is no maximum number of
parameters. A reference to a parameter that has no corresponding formal
parameter at the call point is ignored (the value will be the empty
string).

For example:

.. literalinclude:: src/macro.tmplt
   :language: xml

For using macros see :ref:`Macros`.

.. _EXTENDS_and_BLOCK_statements:

EXTENDS and BLOCK statements
============================

.. index:: Command, EXTENDS
.. index:: Command, BLOCK

The `EXTENDS` statement is similar to `INCLUDE`. However, it is
possible to replace parts of the included file. These parts are defined
with the `BLOCK` statement.

Syntax::

 @@EXTENDS@@ filename [variables]
   @@BLOCK(name1)@@
   ...
   @@END_BLOCK@@

   @@BLOCK(name2)@@
   ...
   @@END_BLOCK@@
 @@END_EXTENDS@

And in the included file (:file:`filename` in the above example)::

 ...

 @@BLOCK(name1)@@
   default contents1
 @@END_BLOCK@@

 @@BLOCK(name3)@@
 default contents3
 @@END_BLOCK@@
 ...

When parsing the first file, it will automatically include the
contents of the second file. However, the various `BLOCK`
will be replaced by the value given in the `EXTENDS` statement,
if a value is provided. If no value is provided, the default value
given in the included file will be used.