diff options
author | R. Tyler Ballance <tyler@slide.com> | 2009-05-28 16:31:23 -0700 |
---|---|---|
committer | R. Tyler Ballance <tyler@slide.com> | 2009-05-28 16:31:23 -0700 |
commit | baae1981ad20d7ab2a61efd7145ea9332a57a7a2 (patch) | |
tree | 633f79377b2c6191789ba8be63d2382617a5a0d9 | |
parent | f51d7949797862de12e75ded96d255f7f184443d (diff) | |
download | python-cheetah-baae1981ad20d7ab2a61efd7145ea9332a57a7a2.tar.gz |
Add the basics of importing and using pre-compiled templates, not sure what else to use here
Signed-off-by: R. Tyler Ballance <tyler@slide.com>
-rw-r--r-- | recipes/content/Precompiled_Templates.html | 40 | ||||
-rw-r--r-- | recipes/content/Precompiled_Templates.markdown | 44 |
2 files changed, 83 insertions, 1 deletions
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 <p>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.</p> +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.</p> +<h2>Basic Pre-compilation</h2> +<p>Suppose you have a template that looks something like this:</p> +<pre><code>#attr title = "This is my Template" +<html> + <head> + <title>${title}</title> + </head> + <body> + Hello ${who}! + </body> +</html> +</code></pre> +<p><strong>Figure 1. hello.tmpl</strong></p> +<p>In order to compile this down to a Python file, you need to only execute the +<code>cheetah compile hello.tmpl</code> command. The results will be a Python file (.py) +which you can then treat as any other Python module in your code base.</p> +<h2>Importing and lookup</h2> +<p>Typically for the template in <em>Figure 1</em>, I could easily import it post-compilation +as any other Python module:</p> +<pre><code>from templates import hello + +def myMethod(): + tmpl = hello.hello(searchList=[{'who' : 'world'}]) + results = tmpl.respond() +</code></pre> +<p><strong>Figure 2. runner.py</strong></p> +<p><em>Note:</em> If you use the <code>#implements</code> directive, <code>respond</code> may not be your "main +method" for executing the Cheetah template. You can adjust the example above in +<em>Figure 2</em> by using <code>getattr()</code> to make the lookup of the main method dynamic:</p> +<pre><code>def myMethod(): + tmpl = hello.hello(searchList=[{'who' : 'world'}]) + mainMethod = getattr(tmpl, '_mainCheetahMethod_for_%s' % tmpl.__class__.__name__) + results = getattr(tmpl, mainMethod)() +</code></pre> +<p><strong>Figure 3. Dynamic runner.py</strong></p> +<h3>Even more advanced lookup</h3> </div> <div id="rightcontent"> 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" + <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** + |