[Jifty-commit] r3892 - in Template-Declare: lib/Template

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Aug 13 23:32:43 EDT 2007


Author: agentz
Date: Mon Aug 13 23:32:43 2007
New Revision: 3892

Modified:
   Template-Declare/README
   Template-Declare/lib/Template/Declare.pm

Log:
TD - updated README and some other releng work

Modified: Template-Declare/README
==============================================================================
--- Template-Declare/README	(original)
+++ Template-Declare/README	Mon Aug 13 23:32:43 2007
@@ -30,9 +30,9 @@
             html {
                 head {}
                 body {
-                    p {'Hello, world wide web!'};
-                    }
+                    p {'Hello, world wide web!'}
                 }
+            }
         };
 
         package main;
@@ -155,8 +155,52 @@
      #  </body>
      #  <div id="footer">Page last generated at Mon Jul  2 17:09:34 2007.</div>
      # </html>
- 
-    See Template::Declare::Tags for more examples.
+
+    For more options (especially the "native" XML namespace support and more
+    samples, see Template::Declare::Tags.
+
+  Postprocessing
+    Sometimes you just want simple syntax for inline elements. The following
+    shows how to use a postprocessor to emphasize text _like this_.
+
+     package MyApp::Templates;
+     use Template::Declare::Tags;
+     use base 'Template::Declare';
+
+     template before => sub {
+         h1 {
+             outs "Welcome to ";
+             em { "my"};
+             outs " site. It's ";
+             em { "great"};
+             outs "!";
+         };
+     };
+
+     template after => sub {
+         h1 { "Welcome to _my_ site. It's _great_!"};
+         h2 { outs_raw "This is _not_ emphasized."};
+     };
+
+     package main;
+     use Template::Declare;
+     Template::Declare->init( roots => ['MyApp::Templates'], postprocessor => \&emphasize);
+     print Template::Declare->show( 'before');
+     print Template::Declare->show( 'after');
+
+     sub emphasize {
+         my $text = shift;
+         $text =~ s{_(.+?)_}{<em>$1</em>}g;
+         return $text;
+     }
+
+     # Output:
+     #
+     # <h1>Welcome to 
+     #  <em>my</em> site. It&#39;s 
+     #  <em>great</em>!</h1>
+     # <h1>Welcome to <em>my</em> site. It&#39;s <em>great</em>!</h1>
+     # <h2>This is _not_ emphasized.</h2>
 
   Multiple template roots (search paths)
   Inheritance
@@ -168,6 +212,70 @@
     roots
     postprocessor
 
+  PITFALLS
+    We're reusing the perl interpreter to for our templating langauge, but
+    Perl was not designed perfectly for our purpose here. Here are some
+    known pitfalls while you're scripting your templates with this module.
+
+    *   It's quite common to see tag sub calling statements without trailing
+        semi-colons right after "}" in code that uses this module. For
+        example,
+
+            template foo => {
+                p {
+                    a { attr { src => '1.png' } }
+                    a { attr { src => '2.png' } }
+                    a { attr { src => '3.png' } }
+                }
+            };
+
+        is equivalent to
+
+            template foo => {
+                p {
+                    a { attr { src => '1.png' } };
+                    a { attr { src => '2.png' } };
+                    a { attr { src => '3.png' } };
+                };
+            };
+
+        But "xml_decl" is a notable exception. Please always put a trailing
+        semicolon after "xml_decl { ... }", or the outputs will be messed
+        up.
+
+    *   Another place that requires trailing semicolon is the statements
+        before a Perl looping and branching statements, for example:
+
+            p { "My links:" };
+            for (@links) {
+                with( src => $_ ), a {}
+            }
+
+        the ";" after " p { ... } " is required here, or Perl will croak for
+        syntax errors.
+
+    *   Literal strings that are not in the last statement in a code block
+        won't be captured. So the following template
+
+            p { 'hello'; em { 'world' } }
+
+        produces
+
+          <p>
+           <em>world</em>
+          </p>
+
+        instead of the desired output
+
+          <p>
+           hello
+           <em>world</em>
+          </p>
+
+        You can use "outs" here to solve this problem:
+
+            p { outs 'hello'; em { 'world' } }
+
   show TEMPLATE_NAME
     Call "show" with a "template_name" and "Template::Declare" will render
     that template. Content generated by show can be accessed with the

Modified: Template-Declare/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/lib/Template/Declare.pm	(original)
+++ Template-Declare/lib/Template/Declare.pm	Mon Aug 13 23:32:43 2007
@@ -7,7 +7,7 @@
 use Template::Declare::Buffer;
 use Class::ISA;
 
-$Template::Declare::VERSION = "0.26";
+our $VERSION = "0.26";
 
 use base 'Class::Data::Inheritable';
 __PACKAGE__->mk_classdata('roots');


More information about the Jifty-commit mailing list