simples rendering mit Cheetah
Verfasst: Montag 24. Dezember 2007, 16:44
manchmal klappen die einfachsten sachen nicht:
wie kann ich denn einfach nur eine Cheetah template rendern?
hier ein paar misslungene ansaetze:
geht auch nicht *snif*
wie kann ich denn einfach nur eine Cheetah template rendern?
hier ein paar misslungene ansaetze:
Code: Alles auswählen
In [9]: from Cheetah.Template import Template
In [10]: str(Template(file='/home/kirsch/Desktop/domino/templates/base.tmpl', se
---------------------------------------------------------------------------
<type 'exceptions.NameError'> Traceback (most recent call last)
/home/kirsch/<ipython console> in <module>()
/var/lib/python-support/python2.5/Cheetah/Template.py in __str__(self)
991 mainMethName = getattr(concreteTemplateClass,mainMethNameAtt
992 if mainMethName:
--> 993 def __str__(self): return getattr(self, mainMethName)()
994 elif (hasattr(concreteTemplateClass, 'respond')
995 and concreteTemplateClass.respond!=Servlet.respond):
/home/kirsch/_home_kirsch_Desktop_domino_templates_base_tmpl.py in respond(self,
/home/kirsch/_home_kirsch_Desktop_domino_templates_base_tmpl.py in login(self, *
<type 'exceptions.NameError'>: global name 'user' is not defined
In [11]:
Code: Alles auswählen
In [11]: from Cheetah.Template import Template
In [12]: str(Template(file='/home/kirsch/Desktop/domino/templates/index.tmpl', s
earchList=[{"user": "test"}]))
---------------------------------------------------------------------------
<type 'exceptions.ImportError'> Traceback (most recent call last)
/home/kirsch/<ipython console> in <module>()
/var/lib/python-support/python2.5/Cheetah/Template.py in __init__(self, source,
namespaces, searchList, file, filter, filtersLib, errorCatcher, compilerSettings
, _globalSetVars, _preBuiltSearchList)
1201 ## Now, compile if we're meant to
1202 if (source is not None) or (file is not None):
-> 1203 self._compile(source, file, compilerSettings=compilerSetting
s)
1204
1205 def generatedModuleCode(self):
/var/lib/python-support/python2.5/Cheetah/Template.py in _compile(self, source,
file, compilerSettings, moduleName, mainMethodName)
1489 mainMethodName=mainMethodName,
1490 compilerSettings=compilerSettings,
-> 1491 keepRefToGeneratedCode=True)
1492 self.__class__ = templateClass
1493 # must initialize it so instance attributes are accessible
/var/lib/python-support/python2.5/Cheetah/Template.py in compile(klass, source,
file, returnAClass, compilerSettings, compilerClass, moduleName, className, main
MethodName, baseclass, moduleGlobals, cacheCompilationResults, useCache, preproc
essors, cacheModuleFilesForTracebacks, cacheDirForModuleFiles, keepRefToGenerate
dCode)
777 try:
778 co = compile(generatedModuleCode, __file__, 'exec')
--> 779 exec co in mod.__dict__
780 except SyntaxError, e:
781 try:
/home/kirsch/_home_kirsch_Desktop_domino_templates_index_tmpl.py in <module>()
<type 'exceptions.ImportError'>: No module named base
In [13]:
Code: Alles auswählen
def render(template, **namespace):
print __file__ + ': rendering: ' + template + ' args: ' + str(namespace)
namespace.update(user=get_user())
#fuer render_last
ch.session['last_tmpl'] = template
t = getattr(getattr(templates, template), template)()
for key, value in namespace.iteritems():
setattr(t, key, value)
return str(t)