You are here: Foswiki>Tasks Web>Item11745 (14 Apr 2012, MichaelDaum)Edit Attach

Item11745: parameters to TMPL:P aren't recognized by IF

pencil
Priority: Urgent
Current State: No Action Required
Released In: 1.1.5
Target Release: patch
Applies To: Engine
Component:
Branches:
Reported By: MichaelDaum
Waiting For:
Last Change By: MichaelDaum
While %IF can check for parameters of an %INCLUDE, it doesn't work in TMPL:P

This works fine:
%INCLUDE{"%WEB%.%TOPIC%" section="test" test="foo"}%

%STARTSECTION{"test"}% INCLUDE: %IF{"defined test" then="test is defined" else="test is NOT defined"}% %ENDSECTION{"test"}%

The equivalent using TMPL:P doesn't:
%TMPL:P{"test" test="foo"}%

%TMPL:DEF{"test"}% TMPL: %IF{"defined test" then="test is defined" else="test is NOT defined"}% %TMPL:END%

-- MichaelDaum - 11 Apr 2012

This is not a bug, its a reality of when and how TMPL's are evaluated.

TMPL:P params are not local variables, whereas INCLUDE ones are

by the time the IF is evaluated, the TMPL is totally gone from the text - the only way to do what you want would be to write something like:

%TMPL:P{"test" test="foo"}%

%TMPL:DEF{"test"}% TMPL: %IF{"'' != '%test%'" then="test is defined" else="test is NOT defined"}% %TMPL:END%

(assuming you can deal with empty strings.)

yup, this is another reason i'd like to try to replace TMPL with INCLUDE stuff, but might also be why its going to be very hard to do.

(leaving as urgent so Crawford sees it - incase i'm wrong.)

-- SvenDowideit - 11 Apr 2012

(further from irc)

<MichaelDaum> not the $percntTMPL:P or multi-line %TMPL:P{"foo" \n key="value"}%
<MichaelDaum> TMPL:P is a proper macro registered in Foswiki.pm
<MichaelDaum> which expands when found / left over
<SvenDowideit> i'll bet the same rules apply
<MichaelDaum> even then: a %variable% is expanded properly, alas %IF doesn't see it
<SvenDowideit> INCLUDE tosses the param onto the pref stack - TMPL is unlikely to
<SvenDowideit> talk about a horrid mis-feature :/
<MichaelDaum> there _must_ be some mechanism that binds %variable% to its value
<SvenDowideit> regex?
<MichaelDaum> Crawford would know
<SvenDowideit> I'd expect the macro to call the TMPL code - which does nasty stuff...
<SvenDowideit> y, thats why i left the task urgent for him :)
<SvenDowideit>         'TMPL:P'            => sub { $_[0]->templates->tmplP( $_[1] ) },
<SvenDowideit> yup
<SvenDowideit> and that makes the most sense
<SvenDowideit> as the ***** that made that was being 'expedient' in the worst way
* MartinCleaver has quit (Quit: MartinCleaver)
<SvenDowideit> and _that_ code does 
<SvenDowideit>             $pvalue =~ s/\%(.*?)\%/$params->{$1}/go;
<MichaelDaum> params need to be pushed/popped onto the prefs stack

-- SvenDowideit - 11 Apr 2012

The real work-around is

%IF{"not '%test%'=~'^([%]test)?$'" then="..."}%

-- MichaelDaum - 11 Apr 2012

See also: Item10187#WhatTheDefined

-- PaulHarvey - 12 Apr 2012

Sven is correct, TMPL:P and INCLUDE work using quite different mechanisms. Both are executed at runtime, but only INCLUDE uses the preferences mechanism for parameter instantiation.

The two macros followed different development paths reflecting their different applications. TMPL:P does a pure textual expansion of parameters; none of the preference tests, such as defined, are available. The reason is simply performance; the TMPL: macros are designed (I think) to be expanded as fast as possible, and integrating the preferences mechanisms (setting session variables in a topic context for each TMPL:P expansion) would be prohibitive.

There is no bug here; the operation is as expected. Michael's workaround will mostly work, but of course won't work if the parameter is multi-line or contains active characters.

-- CrawfordCurrie - 13 Apr 2012

Thanks for the clarification.

-- MichaelDaum - 14 Apr 2012
 

ItemTemplate edit

Summary parameters to TMPL:P aren't recognized by IF
ReportedBy MichaelDaum
Codebase 1.1.5 RC2, trunk
SVN Range
AppliesTo Engine
Component
Priority Urgent
CurrentState No Action Required
WaitingFor
Checkins
TargetRelease patch
ReleasedIn 1.1.5
CheckinsOnBranches
trunkCheckins
Release01x01Checkins
Topic revision: r6 - 14 Apr 2012, MichaelDaum
The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License    Legal Imprint    Privacy Policy