From 66ea7ff841c5142053277733f92e9c3527aa7144 Mon Sep 17 00:00:00 2001 From: "R. Tyler Ballance" Date: Sun, 31 May 2009 18:48:31 -0700 Subject: Add the @staticmethod and @classmethod recipe Signed-off-by: R. Tyler Ballance --- .../content/@staticmethod_and_@classmethod.html | 436 +++++++++++++++++++++ .../@staticmethod_and_@classmethod.markdown | 52 +++ recipes/content/Basic_Inheritance.html | 2 +- recipes/content/Precompiled_Templates.html | 2 +- recipes/content/Writing_A_Recipe.html | 2 +- recipes/index.html | 1 + 6 files changed, 492 insertions(+), 3 deletions(-) create mode 100644 recipes/content/@staticmethod_and_@classmethod.html create mode 100644 recipes/content/@staticmethod_and_@classmethod.markdown diff --git a/recipes/content/@staticmethod_and_@classmethod.html b/recipes/content/@staticmethod_and_@classmethod.html new file mode 100644 index 0000000..30e622e --- /dev/null +++ b/recipes/content/@staticmethod_and_@classmethod.html @@ -0,0 +1,436 @@ + + + + + + + Community Cheetah - The Python-Powered Template Engine - @staticmethod_and_@classmethod + + + + + + + + + + + Fork me on GitHub + +
+ +
+ Home |  + Issues |  + Source |  + Roadmap |  +
+
+ + + + + +
+

@staticmethod and @classmethod

+

Refer the Python's documentation if you're unfamiliar with either +@staticmethod or @classmethod and their uses in Python, as they +pertain to their uses in Cheetah as well. Using @staticmethod it's +trivial to create utility templates which are common when using +Cheetah for web development. These utility templates might contain +a number of small functions which generate useful snippets of markup.

+

For example:

+
#def copyright()
+    #import time
+    © CheetahCorp, Inc. $time.strftime('%Y', time.gmtime())
+#end def
+
+

Figure 1, util.tmpl

+

Prior to version v2.2.0 of Cheetah, there wasn't really an easy means +of filling templates with bunches of these small utility functions. In +v2.2.0 however, you can decorate these methods with #@staticmethod +and use "proper" Python syntax for calling them, fig 1 revisited:

+
#@staticmethod
+#def copyright()
+    #import time
+    © CheetahCorp, Inc. $time.strftime('%Y', time.gmtime())
+#end def
+
+

Figure 1.1, util.tmpl

+

With the addition of the @staticmethod decorator, the copyright() +function can now be used without instantiating an instance of the util +template class. In effect:

+
#from util import util
+
+<strong>This is my page</strong>
+<br/>
+<hr noshade/>
+$util.copyright()
+
+

Figure 2, index.tmpl

+

This approach is however no means to structure anything complex, +@staticmethod and @classmethod use in Cheetah is not meant as a +replacement for properly structured class hierarchies (which +Cheetah supports). That said if you are building a web application +@staticmethod/@classmethod are quite useful for the little snippets +of markup, etc that are needed (Google AdSense blocks, footers, +banners, etc).

+

Last edited: Sun May 31 18:40:24 2009

+
+ +
+ + +
+ +
    + + follow Cheetah on Twitter +
    + + + +
    + +
    +
    + + Python-powered +
    +
    + + + + + + + + + + + + + + + diff --git a/recipes/content/@staticmethod_and_@classmethod.markdown b/recipes/content/@staticmethod_and_@classmethod.markdown new file mode 100644 index 0000000..de2f995 --- /dev/null +++ b/recipes/content/@staticmethod_and_@classmethod.markdown @@ -0,0 +1,52 @@ +@staticmethod and @classmethod +============================== +#set $staticmethod = '[@staticmethod](http://docs.python.org/library/functions.html#staticmethod)' +#set $classmethod = '[@classmethod](http://docs.python.org/library/functions.html#classmethod)' + +Refer the Python's documentation if you're unfamiliar with either +$staticmethod or $classmethod and their uses in Python, as they +pertain to their uses in Cheetah as well. Using $staticmethod it's +trivial to create *utility templates* which are common when using +Cheetah for web development. These *utility templates* might contain +a number of small functions which generate useful snippets of markup. + +For example: + + \#def copyright() + \#import time + © CheetahCorp, Inc. \$time.strftime('%Y', time.gmtime()) + \#end def +**Figure 1, util.tmpl** + +Prior to version **v2.2.0** of Cheetah, there wasn't really an easy means +of filling templates with bunches of these small utility functions. In +**v2.2.0** however, you can decorate these methods with `\#@staticmethod` +and use "proper" Python syntax for calling them, **fig 1** revisited: + + \#@staticmethod + \#def copyright() + \#import time + © CheetahCorp, Inc. \$time.strftime('%Y', time.gmtime()) + \#end def +**Figure 1.1, util.tmpl** + +With the addition of the $staticmethod decorator, the `copyright()` +function can now be used without instantiating an instance of the `util` +template class. In effect: + + \#from util import util + + This is my page +
    +
    + \$util.copyright() +**Figure 2, index.tmpl** + + +This approach is however no means to structure anything complex, +$staticmethod and $classmethod use in Cheetah is not meant as a +replacement for properly structured class hierarchies (which +Cheetah supports). That said if you are building a web application +$staticmethod/$classmethod are quite useful for the little snippets +of markup, etc that are needed (Google AdSense blocks, footers, +banners, etc). diff --git a/recipes/content/Basic_Inheritance.html b/recipes/content/Basic_Inheritance.html index 213263c..1331ecd 100644 --- a/recipes/content/Basic_Inheritance.html +++ b/recipes/content/Basic_Inheritance.html @@ -399,7 +399,7 @@ implicitly subclassing from Cheetah.Template.Template

    ## The rest of my recipe template would be below

    Figure 2. recipe1.tmpl

    -

    Last edited: Sat May 30 12:05:40 2009

    +

    Last edited: Sun May 31 18:40:24 2009

    diff --git a/recipes/content/Precompiled_Templates.html b/recipes/content/Precompiled_Templates.html index 5e2c213..08ed9c2 100644 --- a/recipes/content/Precompiled_Templates.html +++ b/recipes/content/Precompiled_Templates.html @@ -396,7 +396,7 @@ method" for executing the Cheetah template. You can adjust the example above in results = getattr(tmpl, mainMethod)()

    Figure 3. Dynamic runner.py

    -

    Last edited: Sat May 30 12:05:40 2009

    +

    Last edited: Sun May 31 18:40:24 2009

    diff --git a/recipes/content/Writing_A_Recipe.html b/recipes/content/Writing_A_Recipe.html index ca30611..e78b003 100644 --- a/recipes/content/Writing_A_Recipe.html +++ b/recipes/content/Writing_A_Recipe.html @@ -353,7 +353,7 @@ border:dashed 1px #888; padding:6px; font-size:1.1em; color:#fff;}

    Writing a "Recipe"

    I'll fill this out soon enough :)

    -

    Last edited: Sat May 30 12:05:40 2009

    +

    Last edited: Sun May 31 18:40:24 2009

    diff --git a/recipes/index.html b/recipes/index.html index a7d34b6..4542cca 100644 --- a/recipes/index.html +++ b/recipes/index.html @@ -357,6 +357,7 @@ border:dashed 1px #888; padding:6px; font-size:1.1em; color:#fff;}
  • Precompiled Templates
  • Writing A Recipe
  • Basic Inheritance
  • +
  • @staticmethod and @classmethod
  • If you're really hungry for some Cheetah recipies, you can check out the out-of-date Cheetah recipes page -- cgit v1.2.1