From baae1981ad20d7ab2a61efd7145ea9332a57a7a2 Mon Sep 17 00:00:00 2001 From: "R. Tyler Ballance" Date: Thu, 28 May 2009 16:31:23 -0700 Subject: Add the basics of importing and using pre-compiled templates, not sure what else to use here Signed-off-by: R. Tyler Ballance --- recipes/content/Precompiled_Templates.html | 40 ++++++++++++++++++++++- recipes/content/Precompiled_Templates.markdown | 44 ++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/recipes/content/Precompiled_Templates.html b/recipes/content/Precompiled_Templates.html index efef3cf..ab3cd85 100644 --- a/recipes/content/Precompiled_Templates.html +++ b/recipes/content/Precompiled_Templates.html @@ -358,7 +358,45 @@ a lot of options when it comes to utilizing Cheetah, particularly in web environ

There is added speed to be gained by using pre-compiled templates, especially when using mod_python with Apache. Precompiling your templates means Apache/mod_python can load your template's generated module into memory and then execution is only -limited by the speed of the Python being executed, and not the Cheetah compiler.

+limited by the speed of the Python being executed, and not the Cheetah compiler. +You can further optimize things by then pre-compiling the generated Python files +(.py) down to Python byte-code (.pyc) so save cycles interpreting the Python.

+

Basic Pre-compilation

+

Suppose you have a template that looks something like this:

+
#attr title = "This is my Template"
+<html>
+    <head>
+        <title>${title}</title>
+    </head>
+    <body>
+        Hello ${who}!
+    </body>
+</html>
+
+

Figure 1. hello.tmpl

+

In order to compile this down to a Python file, you need to only execute the +cheetah compile hello.tmpl command. The results will be a Python file (.py) +which you can then treat as any other Python module in your code base.

+

Importing and lookup

+

Typically for the template in Figure 1, I could easily import it post-compilation +as any other Python module:

+
from templates import hello
+
+def myMethod():
+    tmpl = hello.hello(searchList=[{'who' : 'world'}])
+    results = tmpl.respond()
+
+

Figure 2. runner.py

+

Note: If you use the #implements directive, respond may not be your "main +method" for executing the Cheetah template. You can adjust the example above in +Figure 2 by using getattr() to make the lookup of the main method dynamic:

+
def myMethod():
+    tmpl = hello.hello(searchList=[{'who' : 'world'}])
+    mainMethod = getattr(tmpl, '_mainCheetahMethod_for_%s' % tmpl.__class__.__name__)
+    results = getattr(tmpl, mainMethod)()
+
+

Figure 3. Dynamic runner.py

+

Even more advanced lookup

diff --git a/recipes/content/Precompiled_Templates.markdown b/recipes/content/Precompiled_Templates.markdown index d1994b3..3849d16 100644 --- a/recipes/content/Precompiled_Templates.markdown +++ b/recipes/content/Precompiled_Templates.markdown @@ -10,5 +10,49 @@ There is added speed to be gained by using pre-compiled templates, especially wh using mod_python with Apache. Precompiling your templates means Apache/mod_python can load your template's generated module into memory and then execution is only limited by the speed of the Python being executed, and not the Cheetah compiler. +You can further optimize things by then pre-compiling the generated Python files +(.py) down to Python byte-code (.pyc) so save cycles interpreting the Python. +Basic Pre-compilation +--------------------- +Suppose you have a template that looks something like this: + + \#attr title = "This is my Template" + + + \${title} + + + Hello \${who}! + + +**Figure 1. hello.tmpl** + +In order to compile this down to a Python file, you need to only execute the +`cheetah compile hello.tmpl` command. The results will be a Python file (.py) +which you can then treat as any other Python module in your code base. + + +Importing and lookup +-------------------- +Typically for the template in *Figure 1*, I could easily import it post-compilation +as any other Python module: + + from templates import hello + + def myMethod(): + tmpl = hello.hello(searchList=[{'who' : 'world'}]) + results = tmpl.respond() +**Figure 2. runner.py** + +*Note:* If you use the `\#implements` directive, `respond` may not be your "main +method" for executing the Cheetah template. You can adjust the example above in +*Figure 2* by using `getattr()` to make the lookup of the main method dynamic: + + def myMethod(): + tmpl = hello.hello(searchList=[{'who' : 'world'}]) + mainMethod = getattr(tmpl, '_mainCheetahMethod_for_%s' % tmpl.__class__.__name__) + results = getattr(tmpl, mainMethod)() +**Figure 3. Dynamic runner.py** + -- cgit v1.2.1