Changeset 5714
- Timestamp:
- 11/18/08 14:42:04 (2 months ago)
- Files:
-
- branches/1.1/turbogears/view/base.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/turbogears/view/base.py
r5699 r5714 1 """Template processing for TurboGears view templates.""" 1 """Template processing for TurboGears view layer. 2 The engines are configured here. 3 We also define the functions and variables that will be available in the 4 template scope. 5 """ 2 6 3 7 import sys … … 10 14 import cherrypy 11 15 import genshi 16 from genshi.filters import Translator 12 17 import pkg_resources 13 18 14 19 import turbogears 15 20 from turbogears import identity, config 16 from turbogears.i18n import i18n_filter, get_locale 17 from turbogears.util import ( 18 Bunch, adapt_call, get_template_encoding_default, 19 get_mime_type_for_format, mime_type_has_charset) 21 from turbogears.i18n import i18n_filter, get_locale, gettext 22 from turbogears.util import Bunch, adapt_call, get_template_encoding_default 23 from turbogears.util import get_mime_type_for_format, mime_type_has_charset 20 24 21 25 log = logging.getLogger("turbogears.view") … … 81 85 82 86 @param fragment: passed through to tell the template if only a 83 fragment of a page is desired 87 fragment of a page is desired. This is a way to allow 88 xml template engines to generate non valid html/xml 89 because you warn them to not bother about it. 84 90 @type fragment: bool 85 91 … … 125 131 if isinstance(content_format, (tuple, list)): 126 132 content_format = content_format[0] 133 127 134 if isinstance(content_format, str): 128 135 content_format = content_format.split( 129 136 )[0].split('-' , 1)[0].lower() 137 130 138 else: 131 139 content_format = 'html' 140 132 141 else: 133 142 content_format = 'html' 143 134 144 content_type = get_mime_type_for_format(content_format) 145 135 146 if mime_type_has_charset( 136 147 content_type) and '; charset=' not in content_type: 137 148 charset = get_template_encoding_default(enginename) 149 138 150 if charset: 139 151 content_type += '; charset=' + charset 152 140 153 headers['Content-Type'] = content_type 141 154 … … 285 298 if hasattr(element, 'tag'): 286 299 return genshi.input.ET(element) 300 287 301 elif isinstance(element, list): 288 302 return chain(*imap(genshi_et, element)) 303 289 304 else: 290 305 return element … … 391 406 392 407 """ 408 409 def genshi_loader_callback(template): 410 """Callback to add a gettext Translator filter to Genshi templates. 411 The Translator filter must be first in the list of filters. 412 413 Defined inside the scope of the load_engines because this is 414 not a public function and we don't want to temp people with it. 415 """ 416 template.filters.insert(0, Translator(gettext)) 417 393 418 get = config.get 419 420 # Check if the i18n filter is activated in configuration. 421 # if so we'll need to apply a translator to the Genshi filter list 422 if get("i18n.run_template_filter", False): 423 callback = get("genshi.loader_callback", genshi_loader_callback) 424 config.update({'genshi.loader_callback': callback}) 425 394 426 engine_options = { 395 427 "cheetah.importhooks": get("cheetah.importhooks", False), … … 421 453 "mako.output_encoding": get("mako.output_encoding", "utf-8") 422 454 } 455 423 456 for entrypoint in pkg_resources.iter_entry_points( 424 457 "python.templating.engines"): 425 458 engine = entrypoint.load() 426 459 engines[entrypoint.name] = engine(stdvars, engine_options) 460