We Recommend

Learning Python Learning Python
The authors of Learning Python show you enough essentials of the Python scripting language to enable you to begin solving problems right away, then reveal more powerful aspects of the language one at a time. This approach is sure to appeal to programmers and system administrators who have urgent problems and a preference for learning by semi-guided experimentation.


Posted By

Twain on 10/28/07


Tagged

web


Versions (?)


Dotted access to uncompiled Cheetah templates


Published in: Python 


Seemed like a good idea - lets you used a.b.c to access uncompiled Cheetah templates via directory structure, and the same syntax auto-imports compiled templates.

  1. class TemplateMapper:
  2. """Attribute-style access to non-compiled Cheetah templates..
  3.  
  4. This hides the distinction between importing a pre-compiled template from a
  5. module versus parsing a raw file template in a subdirectory. Seemed like a
  6. good idea at the time."""
  7.  
  8. def __init__(self,
  9. root=path("templates"),
  10. ext=".tmpl",
  11. module=None,
  12. gvars={},
  13. template_class=Cheetah.Template.Template,
  14. kwargs={}):
  15. """Initialise template mapper.
  16.  
  17. @param root: Path to root template directory
  18.  
  19. @param ext: Template extension
  20.  
  21. @param module: Root modulue for templates (root is then not used)
  22.  
  23. @param gvars: Global variables dictionary
  24.  
  25. @param template_class: Defaults to Cheetah.Template.Template
  26.  
  27. @param kwargs: Keyword args for tmpl_class.__init__()
  28. """
  29. self.root = path(root)
  30. self.gvars = gvars
  31. self.gvars["_inc"] = lambda x: str(self.root / x)
  32. self.ext = ext
  33. self.module = module
  34. self.template_class = template_class
  35. self.kwargs = kwargs
  36.  
  37.  
  38. def __getattr__(self, name):
  39. """Attribute access to mapper creates a callable template object"""
  40. # Instantiate using self.root / name
  41. if self.module is None:
  42. return self.CallableTemplate(
  43. template = self.root / name + self.ext,
  44. default_search = self.gvars,
  45. template_class = self.template_class)
  46. # Instantiate using self.module.name.name
  47. else:
  48. return self.CallableTemplate(
  49. template = getattr(getattr(self.module, name), name),
  50. default_search = self.gvars,
  51. template_class = self.template_class)
  52.  
  53.  
  54. class CallableTemplate:
  55. """Proxy for a Cheetah template.
  56.  
  57. Calling this proxy template fills it, with correct constructor
  58. invocation for file, string or compiled templates."""
  59.  
  60. def __init__(self,
  61. template,
  62. default_search={},
  63. template_class=Cheetah.Template.Template,
  64. kwargs={}):
  65. """Initialise callable template with set of global variables
  66.  
  67. @param template: path, string, or Template subclass
  68.  
  69. @param default_search: Default search dictionary
  70.  
  71. @param template_class: What to instantiate if template is path/string
  72.  
  73. @param kwargs: Keyword arguments for Template constructor
  74. """
  75. self.template = template
  76. self.default_search = default_search
  77. self.template_class = template_class
  78. self.kwargs = kwargs
  79.  
  80.  
  81. def __call__(self, *pvars, **kvars):
  82. """Call template with self.gvars for globals.
  83.  
  84. @param pvars: Dictionaries to add to the searchlist
  85. @param kvars: Keywords to add to search list."""
  86. slist = list(pvars) + [kvars, self.default_search]
  87. if isinstance(self.template, path):
  88. return self.template_class(
  89. file=str(self.template), searchList=slist, **self.kwargs)
  90. elif isinstance(self.template, basestring):
  91. return self.template_class(
  92. source=self.template, searchList=slist, **self.kwargs)
  93. elif issubclass(self.template, Template):
  94. return self.template(
  95. searchList=slist, **self.kwargs)
  96. else:
  97. raise ValueError("Unrecognised template")

Report this snippet 

You need to login to post a comment.