-2

Can someone explain me what does these "tal" commands do within my html tags? I learnt they are text attribute language commands but not clear on what they do.

<div tal:condition="myvar">
 Your command returns:<br><tal:block tal:content="myvar"></tal:block>
</div>

Are they evaluating conditions? or placeholders? Please explain.

deceze
  • 510,633
  • 85
  • 743
  • 889
Tania
  • 1,855
  • 1
  • 15
  • 38
  • You have already tagged your question with related tags; have you perhaps tried reading the TAL documentation? What do you know and what don't you know? How far back do we have to go with an explanation? – deceze Jan 22 '15 at 06:15

1 Answers1

1

You're probably, hopefully familiar with "traditional" templating languages which look something like this:

<?php if ($myvar) : ?>
  <div>
    Your command returns:<br><?php echo $myvar; ?>
  </div>
<?php endif; ?>

or:

{% if myvar %}
  <div>
    Your command returns:<br>{{ myvar }}
  </div>
{% endif %}

or similar variations on that syntax. These languages simply output text and offer certain control structures, nothing more, nothing less.

TAL is HTML aware, it uses the HTML syntax as part of its own syntax.

<div tal:condition="myvar">
  Your command returns:<br><tal:block tal:content="myvar"></tal:block>
</div>

It parses the HTML the same way a browser would, and uses the attributes on HTML elements to alter the template. In the above example, the entire div would be shown or removed depending on the value of myvar. It does the same thing as the other two examples above.

The advantage is twofold:

  1. You're guaranteed to be producing 100% valid HTML output, since the templating system itself requires valid HTML, is HTML aware and will produce guaranteed good output. "Traditional" templating languages are essentially just concatenating strings, which opens you up to all sorts of syntax problems. TAL works on an abstract DOM level, the same thing a browser does.
  2. Your template is "just HTML" at all times, so you can work on it without requiring PHP or whatever other template processor would be needed. A front-end designer can design and code the template entirely independently from any backend and then hand the finished code to the backend developer, who will hook it up to the rest of the code.
deceze
  • 510,633
  • 85
  • 743
  • 889
  • Hi thanks for explaining. Can you please give me a more detailed explanation on what tal:condition and tal:content do? You might have got the point that I am just trying to use tal as a placeholder for a variable. In that case, why do I have to use the condition and content tags? http://www.owlfish.com/software/simpleTAL/tal-guide.html . I went thro This link but I dont seem to have got the point – Tania Jan 22 '15 at 06:43
  • Quite honestly, I'm not sure what you don't understand about *"If the expression evaluates to true then this tag and all its children will be output. If the expression evaluates to false then this tag and all its children will not be included in the output."* – deceze Jan 22 '15 at 07:14
  • As for `tal:block`, it's essentially a placeholder; see http://phptal.org/manual/en/split/phptal-blocks.html. The `` tag itself will not be output, but anything else that TAL will do based on the other attributes will be done. In the example above, the entire `` element will be *replaced* by `myvar`. – deceze Jan 22 '15 at 07:18
  • I understand now. In my program, the expression is just one variable..it evals to true, and hence the output is received. Am I right? – Tania Jan 22 '15 at 07:56
  • Right, if `myvar` is `true` in the above then the output should pretty much just be `
    Your command returns:
    `. It depends on what flavour of TAL you use exactly how `true` may or may not be output in the ``
    – deceze Jan 22 '15 at 08:42