Item2545: Enhance TMPL:DEF to create previous version of template definition

Priority: CurrentState: AppliesTo: Component: WaitingFor:
Enhancement Closed Engine Template  
TMPL:DEF always override the older version of the definition. I am expecting that, if the older version exists, it renames the older version to "XXXX:PrevVersion", then uses the name for the new version of the template.

For example,

%TMPL:DEF{"script"}%%TMPL:P{"script:PrevVersion"}%%TMPL:P{"myaddtionalscripts"}%%TMPL:END%

This will rename the existing script template to script:PrevVersion, and use it in the new definition. Basically, making appending possible.

%TMPL:DEF{"main"}%%TMPL:P{context="use_new" then="mynewmain" else="main:PrevVersion"}%%TMPL:END%

This will conditionally replace the template main.

Furthermore, it might be helpful to enhance "TMPL:P" a little, so "TMPL:P{$:xxx}" , interpreting "$" as the name of the current template (being defined).

For example,

%TMPL:DEF{"script"}%%TMPL:P{"$:PrevVersion"}%%TMPL:P{"myadditionalscripts"}%%TMPL:END%


This would be a terrific feature, and it (or a different solution) is badly needed. Sometimes plugins want to add something to a TMPL:DEF without having to consider which template/what customisations are in place; and without having to force the user to create a customised skin or hack a default one which will be trashed on every upgrade.

We've discussed "recursive" TMPL:DEFs before. Rather than specify the PrevVersion with new syntax; if you TMPL:P{"foo"} inside a TMPL:DEF{"foo"}, then Foswiki would "know" that the TMPL:P is talking about inserting the previously defined template def.

Other topics talking about this feature:

-- PaulHarvey - 31 Dec 2009


I am glad that I am not the only one craving for this feature. Actually, it is very easy to implement. I have made a patch below, in case you are interested, please apply it to lib/Foswiki/Templates.pm and give it try. Instead of using "PrevVersion", a TMPL:PREV will load the previous version.

Index: Templates.pm
===================================================================
--- Templates.pm   (revision 5900)
+++ Templates.pm   (working copy)
@@ -168,6 +168,7 @@
                 $val =~ s/%$p%/$params->{$p}/ge;
             }
         }
+        $val =~ s/%TMPL:PREV%/%TMPL:P{"$template:_PREV"}%/ge;
         $val =~ s/%TMPL:P{(.*?)}%/$this->expandTemplate($1)/ge;
     }
 
@@ -244,7 +245,19 @@
 
             # handle %TMPL:DEF{key}%
             if ($key) {
-                $this->{VARS}->{$key} = $val;
+
+                # if the key is already defined, rename the existing template to  key:_PREV
+                my $new_value    = $val;
+                my $prev_key     = $key;
+                my $prev_value   = $this->{VARS}->{$prev_key};
+                $this->{VARS}->{$prev_key} = $new_value;
+                while ($prev_value) {
+                    $new_value   = $prev_value;
+                    $prev_key    = "$prev_key:_PREV";
+                    $prev_value   = $this->{VARS}->{$prev_key};
+                    $this->{VARS}->{$prev_key} = $new_value;
+                } 
+
             }
             $key = $1;
 
@@ -255,7 +268,19 @@
         elsif (/^END%[\n\r]*(.*)/s) {
 
             # handle %TMPL:END%
-            $this->{VARS}->{$key} = $val;
+
+            # if the key is already defined, rename the existing template to  key:_PREV
+            my $new_value    = $val;
+            my $prev_key     = $key;
+            my $prev_value   = $this->{VARS}->{$prev_key};
+            $this->{VARS}->{$prev_key} = $new_value;
+            while ($prev_value) {
+                $new_value   = $prev_value;
+                $prev_key    = "$prev_key:_PREV";
+                $prev_value   = $this->{VARS}->{$prev_key};
+                $this->{VARS}->{$prev_key} = $new_value;
+            } 
+
             $key                  = '';
             $val                  = '';


Guys, what's next? How do I get some attention here? How do I get the feature implemented officially? I need this feature to port my TWiki application into Foswiki and with some upgrade.

-- ChunHuang - 31 Dec 2009

You need to start a feature proposal In development web, and it must then be accepted. Fastest acceptance timeline is 14 days (assuming there are no disagreements on implementation. Somehow I don't think this will be doable for 1.0.8, better target 1.1.

Whoever implements it in core will also need to provide unit tests and documentation.

-- PaulHarvey - 16 Jan 2010

I've created the feature request at RecursiveTMPLDefinitions - so we can hopefully commit this to trunk in about 2 weeks time :).

unit tests and docco will be appreciated - Paul and I will do what we can to help, as we would like this feature to simplify TinyMCE configuration (among other things)

-- SvenDowideit - 24 Jan 2010

initial code, test and insufficient docco has been commited.

we need to find a few places where the core tmpl's can use it so that we also have examples in the wild smile

-- SvenDowideit - 26 Mar 2010

ItemTemplate edit

Summary Enhance TMPL:DEF to create previous version of template definition
ReportedBy ChunHuang
Codebase 1.0.8, trunk
SVN Range
AppliesTo Engine
Component Template
Priority Enhancement
CurrentState Closed
WaitingFor
Checkins Foswikirev:6928
TargetRelease minor
ReleasedIn 1.1.0
Topic revision: r10 - 04 Oct 2010, KennethLavrsen
 
The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. see CopyrightStatement. Creative Commons License