Question: Howto render conditionally by the result of a plugin
Especially the LDAP-Plugin in my case. I try to setup a table ontop of every userpage which shows informations regarding the user pulled out of our LDAP-server. I found the
LdapPlugin very helpful for this. But not for every user is every information filed in the LDAP. And the complete table is long, about 20 field or so. Some users have only 5 fields filled, some users have all fields filled.
So i search for a way to show the tablerows only if the LDAP holds informations for this field. So by example only if
%LDAP{host="ldap.ourcompany.priv" base="dc=ourcompany,dc=de" filter="deOurCompanyWikiname=%TOPIC%" format="$telephoneNumber"}%
returns actually an value I would liked the line
| *Phone:* | %LDAP{host="ldap.ourcompany.priv" base="dc=ourcompany,dc=de" filter="deOurCompanyWikiname=%TOPIC%" format="$telephoneNumber"}% |
get rendered.
I found the
IfDefinedPlugin, the
ConditionalPlugins and
IfStatements, but did not found the solution I searched for. I tried with
IfStatements stuff like this:
* Set USERPHONE = %LDAP{host="ldap.ourcompany.priv" base="dc=ourcompany,dc=de" filter="deOurCompanyWikiname=%TOPIC%" format="$telephoneNumber"}%
%IF{"%USERPHONE% != 0" then="| *Telefon:* | %USERPHONE% |" else=""}%
but got only
IF{ "%LDAP{host=" ldap="1" }: Bad expression at %LDAP{host= back
Any ideas?
Environment
--
MarcTeichgraeber - 10 Aug 2006
Answer
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.
You are thinking TWiki has an embedded programming language; it doesn't it's just a macro processor. TWiki "variables" are not really variables, they are more like #defines in 'C'. if you examine your condition:
* Set USERPHONE = %LDAP{host="ldap.ourcompany.priv" base="dc=ourcompany,dc=de" filter="deOurCompanyWikiname=%TOPIC%" format="$telephoneNumber"}%
%IF{"%USERPHONE% != 0" then="| *Telefon:* | %USERPHONE% |" else=""}%
Now, TWiki "variables" are are expanded
inside-out and
left-to-right. So when TWiki expands your condition, the first think it does is to expand variables
in the condition. In your case, the %USERPHONE variables get expanded first, thus:
%IF{"%LDAP{host="ldap.ourcompany.priv" base="dc=ourcompany,dc=de" filter="deOurCompanyWikiname=%TOPIC%" != 0" then="| *Telefon:* | %LDAP{host="ldap.ourcompany.priv" base="dc=ourcompany,dc=de" filter="deOurCompanyWikiname=%TOPIC%" |" else=""}%
the expansion proceeds recursively to expand first the %TOPIC and then the %LDAP before finally trying to expand the %IF - which by that time is hopelessly confused.
%IF supports single-quoted constants for exactly this reason. if you write:
%IF{"'%USERPHONE%' != ''" then="| *Telefon:* | %USERPHONE% |" else=""}%
(note the single quotes) then you should get closer to a working solution (note that I am assuming that %USERPHONE expands to the empty string if no phone number is defined, which is more likely to reflect what you want).
FYI %IF also supports $USERPHONE, which is synonymous with, a bit less typing than, '%USERINFO%'
%IF{"$USERPHONE != ''" then="| *Telefon:* | %USERPHONE% |" else=""}%
--
CrawfordCurrie - 21 Aug 2006