Quantcast

How to work with conditional template expansion?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to work with conditional template expansion?

BramVanOosterhout
Hi All,

I want to render a page dependent on a class of user. Users can be in one of
two groups: Admin or View.

User in group View should not have a sidebar, search, buttons, links other
than the ones defined on the editable text.

I believe the way to do it is by having 2 templates. One with all the normal
facilities as rendered by the skin for Admin (call that one "view"). And one
emasculated template (call that one "plain").

The two presentations I want are close to what I get with the default url
(view) and qualified with ?skin=plain (plain) url parameter

I tried (I am testing this on WebHome) setting the VIEW template in the topic:
%IF{ "'%USERINFO{ format="$isadmin" }%'='true'"
  then="$percentSET{ \"VIEW_TEMPLATE\" value=\"\" }$percent"
  else="$percentSET{ \"VIEW_TEMPLATE\" value=\"WebHomeView\" }$percent" }%

The VIEW_TEMPLATE is set to the appropriate value for the user class, but the
template is not activated. I think that is as intended, although I cannot put
my finger on it in the documentation.

After reading the template documentation I decided to try the conditional
expansion with context identifiers.

I defined:
%TMPL:DEF{"ORIGINAL_document"}%%TMPL:P{"htmldoctype"}%%TMPL:P{"htmlstart"}%
%TMPL:P{"head"}%
%TMPL:P{"bodystart"}%%TMPL:P{"main"}%%TMPL:P{"bodyend"}%%TMPL:P{"htmlend"}%
%TMPL:END%
%TMPL:DEF{"document"}%%TMPL:END%
%TMPL:DEF{"admindocument"}%this is the admin document%TMPL:END%
%TMPL:DEF{"viewdocument"}%this is the view document%TMPL:END%
%TMPL:P{context="isadmin" then="admindocument" else="viewdocument"}%

And hey presto: The Admin gets the "this is the admin document" line and the
View gets the "this is the view document" line

Now I put the contents of the ORIGINAL_document in admin document
%TMPL:DEF{"admindocument"}%%TMPL:P{"htmldoctype"}%%TMPL:P{"htmlstart"}%
%TMPL:P{"head"}%
%TMPL:P{"bodystart"}%%TMPL:P{"main"}%%TMPL:P{"bodyend"}%%TMPL:P{"htmlend"}%%TMPL:END%

And the frame is rendered with top, sidebar and bottom. BUT %TEXT% and
%REVISIONS% are not expanded.

When I change the %TMPL:P{context="isadmin" ... }% to
%TMPL:P{"admindocument"}%
the page gets rendered without issues.

I can not imagine why this would happen. The template is clearly emitted, but
the treatment is different when the condition is there?

Any suggestion what I can do different to achieve the result I am after?

I tried to understand "Conditional expansion - Using parameters. But I cannot
find a way to make that work using:
%IF{ "'%USERINFO{ format="$isadmin" }%'='true'"
  then="admindocument"
  else="viewdocument" }%
to select the template name.

All suggestions welcome. Thanks for your help.

--
Bram van Oosterhout


------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to work with conditional template expansion?

Chris Hoefler
Your %IF% isn't working because the VIEW_TEMPLATE has to be set before the macros are expanded.

Skin templates are really hard to grok (for me at least). Does it work if you try something like this:
%TMPL:INCLUDE{"view"}%

%TMPL:DEF{"admindocument"}%%TMPL:P{"more_link"}%%TMPL:END%
%TMPL:DEF{"viewdocument"}%You are not the admin!%TMPL:END%
%TMPL:DEF{"topicactionbuttons"}%%TMPL:P{context="isadmin" then="admindocument" else="viewdocument"}%%TMPL:END%
?

Another way that might work, if you have a fixed set of topics that you want to do this way, is to use the "Rules" section of AutoTemplatePlugin. I believe you should be able to include your %IF% macro in the rule set, but I've never tried this.

On Fri, May 6, 2016 at 1:29 AM, Bram van Oosterhout <[hidden email]> wrote:
Hi All,

I want to render a page dependent on a class of user. Users can be in one of
two groups: Admin or View.

User in group View should not have a sidebar, search, buttons, links other
than the ones defined on the editable text.

I believe the way to do it is by having 2 templates. One with all the normal
facilities as rendered by the skin for Admin (call that one "view"). And one
emasculated template (call that one "plain").

The two presentations I want are close to what I get with the default url
(view) and qualified with ?skin=plain (plain) url parameter

I tried (I am testing this on WebHome) setting the VIEW template in the topic:
%IF{ "'%USERINFO{ format="$isadmin" }%'='true'"
  then="$percentSET{ \"VIEW_TEMPLATE\" value=\"\" }$percent"
  else="$percentSET{ \"VIEW_TEMPLATE\" value=\"WebHomeView\" }$percent" }%

The VIEW_TEMPLATE is set to the appropriate value for the user class, but the
template is not activated. I think that is as intended, although I cannot put
my finger on it in the documentation.

After reading the template documentation I decided to try the conditional
expansion with context identifiers.

I defined:
%TMPL:DEF{"ORIGINAL_document"}%%TMPL:P{"htmldoctype"}%%TMPL:P{"htmlstart"}%
%TMPL:P{"head"}%
%TMPL:P{"bodystart"}%%TMPL:P{"main"}%%TMPL:P{"bodyend"}%%TMPL:P{"htmlend"}%
%TMPL:END%
%TMPL:DEF{"document"}%%TMPL:END%
%TMPL:DEF{"admindocument"}%this is the admin document%TMPL:END%
%TMPL:DEF{"viewdocument"}%this is the view document%TMPL:END%
%TMPL:P{context="isadmin" then="admindocument" else="viewdocument"}%

And hey presto: The Admin gets the "this is the admin document" line and the
View gets the "this is the view document" line

Now I put the contents of the ORIGINAL_document in admin document
%TMPL:DEF{"admindocument"}%%TMPL:P{"htmldoctype"}%%TMPL:P{"htmlstart"}%
%TMPL:P{"head"}%
%TMPL:P{"bodystart"}%%TMPL:P{"main"}%%TMPL:P{"bodyend"}%%TMPL:P{"htmlend"}%%TMPL:END%

And the frame is rendered with top, sidebar and bottom. BUT %TEXT% and
%REVISIONS% are not expanded.

When I change the %TMPL:P{context="isadmin" ... }% to
%TMPL:P{"admindocument"}%
the page gets rendered without issues.

I can not imagine why this would happen. The template is clearly emitted, but
the treatment is different when the condition is there?

Any suggestion what I can do different to achieve the result I am after?

I tried to understand "Conditional expansion - Using parameters. But I cannot
find a way to make that work using:
%IF{ "'%USERINFO{ format="$isadmin" }%'='true'"
  then="admindocument"
  else="viewdocument" }%
to select the template name.

All suggestions welcome. Thanks for your help.

--
Bram van Oosterhout


------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss



--
Chris Hoefler, PhD
Postdoctoral Research Associate
Straight Lab
Texas A&M University
2128 TAMU
College Station, TX 77843-2128

------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
_______________________________________________
Foswiki-discuss mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/foswiki-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to work with conditional template expansion?

BramVanOosterhout
Hi Chris,

Thanks for your response.

I tried the suggested code: No luck.

I tried the rules in AutoTemplatePlugin (I did not realise how they could be used. Thanks for the pointer:) But I get caught in the same bind. The decision needs to be made BEFORE the templates are expanded. And the MACROS are not expanded until AFTER the templates are rendered.

That got me thinking about another approach and now I have solved my problem another way: redirect the topic depending on a condition.

Sidesteps all the issues with templates and has the desired result! It can also easily be extended to any condition. Here is the code I use in WebHome:

%IF{ "'%USERINFO{ "%USERNAME%" format="$isadmin" }%'='true'"
  then=""
  else="$percentADDTOZONE{ \"head\" id=\"redirect\" text=\"<meta http-equiv='refresh' content='0; url=%SCRIPTURL{ "view" web="%WEB%" topic="UserHome" }%' />\" }$percent"
}%

Thanks for listening :-)
Loading...