def packageCallback_genshisolari(packagename): print "packageCallback_genshisolari", packagename _loader_dict[packagename] = loader.package(packagename, '_webapp/templates') templateLoader = TemplateLoader(loader.prefixed(**_loader_dict), max_cache_size=100*len(_loader_dict)) def generate_tmpl(template_path, data=None, cls=None): render_dict = {} if tmpl.__dict__: render_dict.update(tmpl.__dict__) if data: render_dict.update(data) return templateLoader.load(template_path, cls=cls).generate(**render_dict) def text(template_path, data=None, method='text'): return generate_tmpl(template_path, data, NewTextTemplate).render(method) def render(template_path, data=None, method='xhtml'): return generate_tmpl(template_path, data).render(method) def serialize(template_path, data=None, method='xhtml'): return generate_tmpl(template_path, data).serialize(method) context.defaults( templateLoader = templateLoader, text = text, render = render, serialize = serialize, )
def setup_tw_middleware(app, config): # Set up the TW middleware, as per errors and instructions at: # http://groups.google.com/group/toscawidgets-discuss/browse_thread/thread/c06950b8d1f62db9 # http://toscawidgets.org/documentation/ToscaWidgets/install/pylons_app.html def enable_i18n_for_template(template): template.filters.insert(0, Translator(ugettext)) def filename_suffix_adder(inner_loader, suffix): def _add_suffix(filename): return inner_loader(filename + suffix) return _add_suffix # Ensure that the toscawidgets template loader includes the search paths # from our main template loader. tw_engine_options = {"genshi.loader_callback": enable_i18n_for_template} tw_engines = EngineManager(extra_vars_func=None, options=tw_engine_options) tw_engines["genshi"] = MarkupTemplateEnginePlugin() tw_engines["genshi"].loader = config["pylons.app_globals"].genshi_loader # Disable the built-in package name template resolution. tw_engines["genshi"].use_package_naming = False # Rebuild package name template resolution using mostly standard Genshi # load functions. With our customizations to the TemplateLoader, the # absolute paths that the builtin resolution produces are erroneously # treated as being relative to the search path. # Search the tw templates dir using the pkg_resources API. # Expected input: 'input_field.html' tw_loader = loader.package("tw.forms", "templates") # Include the .html extension automatically. # Expected input: 'input_field' tw_loader = filename_suffix_adder(tw_loader, ".html") # Apply this loader only when the filename starts with tw.forms.templates. # This prefix is stripped off when calling the above loader. # Expected input: 'tw.forms.templates.input_field' tw_loader = loader.prefixed(**{"tw.forms.templates.": tw_loader}) # Add this path to our global loader tw_engines["genshi"].loader.search_path.append(tw_loader) app = tw.api.make_middleware( app, { "toscawidgets.framework": "pylons", "toscawidgets.framework.default_view": "genshi", "toscawidgets.framework.translator": lazy_ugettext, "toscawidgets.framework.engines": tw_engines, }, ) return app
def template_loader(self): """A :class:`genshi.template.TemplateLoader` that loads templates from the same places as Flask. """ search_paths = {} for name, jinja_loader in self._iter_jinja_loaders(): assert type(jinja_loader.searchpath) is list and len(jinja_loader.searchpath) == 1 search_paths[name] = loader.directory(jinja_loader.searchpath[0]) return TemplateLoader(loader.prefixed(**search_paths), auto_reload=self.app.debug, callback=self.callback)
def template_loader(self): """A :class:`genshi.template.TemplateLoader` that loads templates from the same places as Flask. """ path = loader.directory(os.path.join(self.app.root_path, 'templates')) module_paths = {} modules = getattr(self.app, 'modules', {}) for name, module in list(modules.items()): module_path = os.path.join(module.root_path, 'templates') if os.path.isdir(module_path): module_paths[name] = loader.directory(module_path) return TemplateLoader([path, loader.prefixed(**module_paths)], auto_reload=self.app.debug, callback=self.callback)
def template_loader(self): """A :class:`genshi.template.TemplateLoader` that loads templates from the same places as Flask. """ path = loader.directory(os.path.join(self.app.root_path, 'templates')) module_paths = {} modules = getattr(self.app, 'modules', {}) for name, module in modules.iteritems(): module_path = os.path.join(module.root_path, 'templates') if os.path.isdir(module_path): module_paths[name] = loader.directory(module_path) return TemplateLoader([path, loader.prefixed(**module_paths)], auto_reload=self.app.debug, callback=self.callback)
def setup_tw_middleware(app, config): def filename_suffix_adder(inner_loader, suffix): def _add_suffix(filename): return inner_loader(filename + suffix) return _add_suffix # Ensure that the toscawidgets template loader includes the search paths # from our main template loader. tw_engines = EngineManager(extra_vars_func=None) tw_engines["genshi"] = MarkupTemplateEnginePlugin() tw_engines["genshi"].loader = config["pylons.app_globals"].genshi_loader # Disable the built-in package name template resolution. tw_engines["genshi"].use_package_naming = False # Rebuild package name template resolution using mostly standard Genshi # load functions. With our customizations to the TemplateLoader, the # absolute paths that the builtin resolution produces are erroneously # treated as being relative to the search path. # Search the tw templates dir using the pkg_resources API. # Expected input: 'input_field.html' tw_loader = loader.package("tw.forms", "templates") # Include the .html extension automatically. # Expected input: 'input_field' tw_loader = filename_suffix_adder(tw_loader, ".html") # Apply this loader only when the filename starts with tw.forms.templates. # This prefix is stripped off when calling the above loader. # Expected input: 'tw.forms.templates.input_field' tw_loader = loader.prefixed(**{"tw.forms.templates.": tw_loader}) # Add this path to our global loader tw_engines["genshi"].loader.search_path.append(tw_loader) app = tw.api.make_middleware( app, { "toscawidgets.framework": "pylons", "toscawidgets.framework.default_view": "genshi", "toscawidgets.framework.engines": tw_engines, }, ) return app
def setup_tw_middleware(app, config): def filename_suffix_adder(inner_loader, suffix): def _add_suffix(filename): return inner_loader(filename + suffix) return _add_suffix # Ensure that the toscawidgets template loader includes the search paths # from our main template loader. tw_engines = EngineManager(extra_vars_func=None) tw_engines['genshi'] = MarkupTemplateEnginePlugin() tw_engines['genshi'].loader = config['pylons.app_globals'].genshi_loader # Disable the built-in package name template resolution. tw_engines['genshi'].use_package_naming = False # Rebuild package name template resolution using mostly standard Genshi # load functions. With our customizations to the TemplateLoader, the # absolute paths that the builtin resolution produces are erroneously # treated as being relative to the search path. # Search the tw templates dir using the pkg_resources API. # Expected input: 'input_field.html' tw_loader = loader.package('tw.forms', 'templates') # Include the .html extension automatically. # Expected input: 'input_field' tw_loader = filename_suffix_adder(tw_loader, '.html') # Apply this loader only when the filename starts with tw.forms.templates. # This prefix is stripped off when calling the above loader. # Expected input: 'tw.forms.templates.input_field' tw_loader = loader.prefixed(**{'tw.forms.templates.': tw_loader}) # Add this path to our global loader tw_engines['genshi'].loader.search_path.append(tw_loader) app = tw.api.make_middleware( app, { 'toscawidgets.framework': 'pylons', 'toscawidgets.framework.default_view': 'genshi', 'toscawidgets.framework.engines': tw_engines, }) return app
def template_loader(self): """A :class:`genshi.template.TemplateLoader` that loads templates from the same places as Flask. .. versionchanged:: 0.6 Removed support for flask modules and enabled support for blueprints """ path = loader.directory( os.path.join(self.app.root_path, self.app.template_folder or 'templates') ) blueprint_paths = {} blueprints = getattr(self.app, 'blueprints', {}) for name, blueprint in blueprints.items(): blueprint_path = os.path.join( blueprint.root_path, blueprint.template_folder or 'templates' ) if os.path.isdir(blueprint_path): blueprint_paths[name] = loader.directory(blueprint_path) return TemplateLoader([path, loader.prefixed(**blueprint_paths)], auto_reload=self.app.debug, callback=self.callback)
def template_loaders(self): """Return genshi loaders for all the plugins that provide templates. Plugin template are found under its module name:: <xi:include "{plugin.name}/index.html" /> Maps to:: `{path_to_module}/templates/index.html` :rtype: list :returns: Instances of :class:`genshi.template.loader.TemplateLoader` """ loaders = {} for name, plugin in self.plugins.iteritems(): if plugin.templates_path: loaders[name] = plugin.templates_path if loaders: log.debug('Template loaders: %r', loaders) return [loader.prefixed(**loaders)] return []
def template_loaders(self): """Return genshi loaders for all the plugins that provide templates. Plugin template are found under its module name:: <xi:include "{plugin.name}/index.html" /> Maps to:: `{path_to_module}/templates/index.html` :rtype: list :returns: Instances of :class:`genshi.template.loader.TemplateLoader` """ loaders = {} for name, plugin in self.plugins.iteritems(): if plugin.templates_path: loaders[name + '/'] = plugin.templates_path if loaders: log.debug('Template loaders: %r', loaders) return [loader.prefixed(**loaders)] return []
def create_tw_engine_manager(app_globals): def filename_suffix_adder(inner_loader, suffix): def _add_suffix(filename): return inner_loader(filename + suffix) return _add_suffix # Ensure that the toscawidgets template loader includes the search paths # from our main template loader. tw_engines = EngineManager(extra_vars_func=None) tw_engines['genshi'] = MarkupTemplateEnginePlugin() tw_engines['genshi'].loader = app_globals.genshi_loader # Disable the built-in package name template resolution. tw_engines['genshi'].use_package_naming = False # Rebuild package name template resolution using mostly standard Genshi # load functions. With our customizations to the TemplateLoader, the # absolute paths that the builtin resolution produces are erroneously # treated as being relative to the search path. # Search the tw templates dir using the pkg_resources API. # Expected input: 'input_field.html' tw_loader = loader.package('tw.forms', 'templates') # Include the .html extension automatically. # Expected input: 'input_field' tw_loader = filename_suffix_adder(tw_loader, '.html') # Apply this loader only when the filename starts with tw.forms.templates. # This prefix is stripped off when calling the above loader. # Expected input: 'tw.forms.templates.input_field' tw_loader = loader.prefixed(**{'tw.forms.templates.': tw_loader}) # Add this path to our global loader tw_engines['genshi'].loader.search_path.append(tw_loader) return tw_engines