SID-00175: Conditional rendering in view.pattern.tmpl | contentheader
| Status: |
Answered |
TWiki version: |
4.2.0 |
Perl version: |
5.008008 (linux) |
| Category: |
CategoryRendering |
Server OS: |
Linux 2.6.18-53.1.14.el5pae (i386-linux-thread-multi) |
Last update: |
17 years ago |
I want to define
TMPL:DEF{"contentheader"}to render:
a) "Valid document" if %FORMFIELD{"valid"}% equals True
b) "Deprecated document" if %FORMFIELD{"valid"}% equals False
c) Empty string otherwise
The best I can do so far is:
%TMPL:DEF{"contentheader"}%\
%FORMFIELD{"valid" topic="%BASETOPIC%" format="This document is valid: $value" default="" alttext=""}%\
%TMPL:END%
But this is not exactly what I want.
Thanks in advance for your help.
--
GregoireLeclair - 09 Mar 2009
Discussion and Answer
Look into conditionals, either with
%IF{}% (see
VarIF,
IfStatements), or a
%CALC{$IF()}% (see
SpreadSheetPlugin).
Please report back a working example once you find out so that other TWiki community members can learn.
--
PeterThoeny - 11 Mar 2009
Thanks Peter. I figured out how to build the conditional code, but the problem is the nested double quotes. Is there any way of getting rid of them?
%TMPL:DEF{"contentheadervalid"}%\
%FORMFIELD{"valid" topic="%BASETOPIC%" default="" alttext=""}%
%TMPL:END%
%TMPL:DEF{"contentheader"}%\
%CALC{"$IF($EXACT(%TMPL:P{"contentheadervalid"}%, True) == 1, Valid document, )"}%\
%CALC{"$IF($EXACT(%TMPL:P{"contentheadervalid"}%, False) == 1, Deprecated document, )"}%\
%TMPL:END%
--
GregoireLeclair - 11 Mar 2009
Either omit the quotes (
%TMPL:P{contentheadervalid}% ) or escape them (
%TMPL:P{\"contentheadervalid\"}% ).
--
PeterThoeny - 11 Mar 2009
Also, you can list multiple formulae within one CALC, such as
%CALC{$IF(...)$IF(...)}%
--
PeterThoeny - 11 Mar 2009
I tried without the quotes and with escaped quotes without success.
For a given topic where valid=True, If I use this code:
%TMPL:INCLUDE{"view.pattern"}%
%TMPL:DEF{"contentheadervalid"}%\
%FORMFIELD{"valid" topic="%BASETOPIC%" default="" alttext=""}%
%TMPL:END%
%TMPL:DEF{"contentheader"}%\
%CALC{"$IF($EXACT(%TMPL:P{contentheadervalid}%, True) == 1, Valid document, )"}%\
%CALC{"$IF($EXACT(%TMPL:P{contentheadervalid}%, False) == 1, Deprecated document, )"}%\
%TMPL:END%
Then the following text is rendered:
%CALC{"$IF($EXACT(True , True) == 1, Valid document, )"}%%CALC{"$IF($EXACT(True , False) == 1, Deprecated document, )"}%
It looks like
%TMPL:P{contentheadervalid}% is evaluated after the
IF conditional evaluation.
Therefore, I tried to replace
%TMPL:P{contentheadervalid}% with the plain text
True. I got the expected result:
Valid document
I guess the challenge is to retrieve the formfield value prior to the conditional evaluation.
--
GregoireLeclair - 12 Mar 2009
Why not use a CALC IF on the form field? Such as:
$IF($EXACT(%FORMFIELD{\"valid\" topic=\"%BASETOPIC%\"}%, True), ...)
--
PeterThoeny - 12 Mar 2009
It does work if I remove the escaped quotes:
%TMPL:DEF{"contentheader"}%\
%CALC{"$IF($EXACT(%FORMFIELD{"valid" topic="%BASETOPIC%"}%, True) == 1, Valid document, )"}%<br>\
%CALC{"$IF($EXACT(%FORMFIELD{"valid" topic="%BASETOPIC%"}%, False) == 1, Deprecated document, )"}%<br>\
%TMPL:END%
Not sure exactly how the quotes are evaluated... but it works!
Thanks for your help
--
GregoireLeclair - 12 Mar 2009
Ah, yes, you are right. FORMFIELD is evaluated before CALC, so no escape of quotes in FORMFIELD is needed.
--
PeterThoeny - 12 Mar 2009
If you answer a question - or someone answered one of your questions - please remember to edit the page and set the status to answered. The status selector is below the edit box.