示例#1
0
    def template_caching_options(self, **kwargs):
        _cache_options = {}

        class FakeCache(object):
            def get_cache(self, *args, **kwargs):
                _cache_options['args'] = args
                _cache_options['kwargs'] = kwargs
                try:
                    c = cache.get_cache(*args, **kwargs)
                    _cache_options['cls'] = c.namespace.__class__.__name__
                except TypeError:
                    _cache_options['cls'] = 'NoImplementation'
                    c = cache.get_cache(*args, type='memory', **kwargs)
                return c

        tg.cache.kwargs['type'] = 'NoImplementation'
        old_cache = tg.cache
        tg.cache = FakeCache()

        try:

            def render_func(*args, **kw):
                return 'OK'

            cached_template('index.html', render_func, **kwargs)
            return _cache_options
        finally:
            tg.cache = old_cache
示例#2
0
文件: root.py 项目: antsfee/tg2
    def template_caching_options(self, **kwargs):
        _cache_options = {}
        class FakeCache(object):
            def get_cache(self, *args, **kwargs):
                _cache_options['args'] = args
                _cache_options['kwargs'] = kwargs
                try:
                    c = cache.get_cache(*args, **kwargs)
                    _cache_options['cls'] = c.namespace.__class__.__name__
                except TypeError:
                    _cache_options['cls'] = 'NoImplementation'
                    c = cache.get_cache(*args, type='memory', **kwargs)
                return c

        tg.cache.kwargs['type'] = 'NoImplementation'
        old_cache = tg.cache
        tg.cache = FakeCache()

        try:
            def render_func(*args, **kw):
                return 'OK'
            cached_template('index.html', render_func, **kwargs)
            return _cache_options
        finally:
            tg.cache = old_cache
示例#3
0
文件: root.py 项目: TurboGears/tg2
    def template_caching_options(self, **kwargs):
        _cache_options = {}

        class FakeCache(object):
            def get_cache(self, *args, **kwargs):
                _cache_options["args"] = args
                _cache_options["kwargs"] = kwargs
                try:
                    c = cache.get_cache(*args, **kwargs)
                    _cache_options["cls"] = c.namespace.__class__.__name__
                except TypeError:
                    _cache_options["cls"] = "NoImplementation"
                    c = cache.get_cache(*args, type="memory", **kwargs)
                return c

        tg.cache.kwargs["type"] = "NoImplementation"
        old_cache = tg.cache
        tg.cache = FakeCache()

        try:

            def render_func(*args, **kw):
                return "OK"

            cached_template("index.html", render_func, **kwargs)
            return _cache_options
        finally:
            tg.cache = old_cache
示例#4
0
    def __call__(self,
                 template_name,
                 template_vars,
                 cache_key=None,
                 cache_type=None,
                 cache_expire=None):

        if self.use_dotted_templatenames and not template_name.endswith(
                self.template_extension):
            template_name = self.dotted_loader.find_template_file(
                template_name)
            loader = self.dotted_loader
        else:
            loader = self.normal_loader

        # Create a render callable for the cache function
        def render_template():
            # Grab a template reference
            template = loader.get_template(template_name)
            return Markup(template.render_unicode(**template_vars))

        return cached_template(template_name,
                               render_template,
                               cache_key=cache_key,
                               cache_type=cache_type,
                               cache_expire=cache_expire)
示例#5
0
    def __call__(self,
                 template_name,
                 template_vars,
                 cache_key=None,
                 cache_type=None,
                 cache_expire=None,
                 **render_params):
        """Render a template with Kajiki

        Accepts the cache options ``cache_key``, ``cache_type``, and
        ``cache_expire``.

        """

        # Create a render callable for the cache function
        def render_template():
            # Grab a template reference
            template = self.loader.load(template_name, **render_params)
            return Markup(template(template_vars).render())

        return cached_template(template_name,
                               render_template,
                               cache_key=cache_key,
                               cache_type=cache_type,
                               cache_expire=cache_expire)
示例#6
0
    def __call__(self, template_name, template_vars, **kwargs):
        """Render the template_vars with the Genshi template.

        If you don't pass a doctype or pass 'auto' as the doctype,
        then the doctype will be automatically determined.
        If you pass a doctype of None, then no doctype will be injected.
        If you don't pass a method or pass 'auto' as the method,
        then the method will be automatically determined.

        """
        response = tg.response._current_obj()

        template_vars.update(self.genshi_functions)

        # Gets document type from content type or from config options
        doctype = kwargs.get('doctype', 'auto')
        if doctype == 'auto':
            doctype = self.default_doctype
            if not doctype:
                method = kwargs.get('method') or self.default_method or 'xhtml'
                doctype = self.doctypes_for_methods.get(method)
            doctypes = self.doctypes_for_content_type.get(
                response.content_type)
            if doctypes and (not doctype or doctype not in doctypes):
                doctype = doctypes[0]
            kwargs['doctype'] = doctype

        # Gets rendering method from content type or from config options
        method = kwargs.get('method')
        if not method or method == 'auto':
            method = self.default_method
            if not method:
                method = self.method_for_doctype(doctype)
            methods = self.methods_for_content_type.get(response.content_type)
            if methods and (not method or method not in methods):
                method = methods[0]
            kwargs['method'] = method

        def render_template():
            template = self.load_template(template_name)
            return Markup(
                template.generate(**template_vars).render(doctype=doctype,
                                                          method=method,
                                                          encoding=None))

        return cached_template(template_name,
                               render_template,
                               ns_options=('doctype', 'method'),
                               **kwargs)
示例#7
0
文件: mako.py 项目: DINKIN/tg2
    def __call__(self, template_name, template_vars,
                 cache_key=None, cache_type=None, cache_expire=None):

        if self.use_dotted_templatenames:
            template_name = self.dotted_finder.get_dotted_filename(template_name,
                                                                   template_extension='.mak')

        # Create a render callable for the cache function
        def render_template():
            # Grab a template reference
            template = self.loader.get_template(template_name)
            return Markup(template.render_unicode(**template_vars))

        return cached_template(template_name, render_template, cache_key=cache_key,
                               cache_type=cache_type, cache_expire=cache_expire)
示例#8
0
    def __call__(self, template_name, template_vars, **kwargs):
        """Render the template_vars with the Genshi template.

        If you don't pass a doctype or pass 'auto' as the doctype,
        then the doctype will be automatically determined.
        If you pass a doctype of None, then no doctype will be injected.
        If you don't pass a method or pass 'auto' as the method,
        then the method will be automatically determined.

        """
        response = tg.response._current_obj()

        template_vars.update(self.genshi_functions)

        # Gets document type from content type or from config options
        doctype = kwargs.get('doctype', 'auto')
        if doctype == 'auto':
            doctype = self.default_doctype
            if not doctype:
                method = kwargs.get('method') or self.default_method or 'xhtml'
                doctype = self.doctypes_for_methods.get(method)
            doctypes = self.doctypes_for_content_type.get(response.content_type)
            if doctypes and (not doctype or doctype not in doctypes):
                doctype = doctypes[0]
            kwargs['doctype'] = doctype

        # Gets rendering method from content type or from config options
        method = kwargs.get('method')
        if not method or method == 'auto':
            method = self.default_method
            if not method:
                method = self.method_for_doctype(doctype)
            methods = self.methods_for_content_type.get(response.content_type)
            if methods and (not method or method not in methods):
                method = methods[0]
            kwargs['method'] = method

        def render_template():
            template = self.load_template(template_name)
            return Markup(template.generate(**template_vars).render(
                doctype=doctype,
                method=method,
                encoding=None)
            )

        return cached_template(template_name, render_template,
                               ns_options=('doctype', 'method'), **kwargs)
示例#9
0
文件: kajiki.py 项目: 984958198/tg2
    def __call__(self, template_name, template_vars, cache_key=None,
                 cache_type=None, cache_expire=None):
        """Render a template with Kajiki

        Accepts the cache options ``cache_key``, ``cache_type``, and
        ``cache_expire``.

        """
        # Create a render callable for the cache function
        def render_template():
            # Grab a template reference
            template = self.loader.load(template_name)
            return Markup(template(template_vars).render())

        return cached_template(template_name, render_template,
                               cache_key=cache_key, cache_type=cache_type,
                               cache_expire=cache_expire)
示例#10
0
文件: mako.py 项目: TurboGears/tg2
    def __call__(self, template_name, template_vars, cache_key=None, cache_type=None, cache_expire=None):

        if self.use_dotted_templatenames and not template_name.endswith(self.template_extension):
            template_name = self.dotted_loader.find_template_file(template_name)
            loader = self.dotted_loader
        else:
            loader = self.normal_loader

        # Create a render callable for the cache function
        def render_template():
            # Grab a template reference
            template = loader.get_template(template_name)
            return Markup(template.render_unicode(**template_vars))

        return cached_template(
            template_name, render_template, cache_key=cache_key, cache_type=cache_type, cache_expire=cache_expire
        )
示例#11
0
    def __call__(self, template_name, template_vars, cache_key=None,
                 cache_type=None, cache_expire=None):
        """Render a template with Jinja2

        Accepts the cache options ``cache_key``, ``cache_type``, and
        ``cache_expire``.

        """
        # Create a render callable for the cache function
        def render_template():
            # Grab a template reference
            template = self.jinja2_env.get_template(template_name)
            return Markup(template.render(**template_vars))

        return cached_template(template_name, render_template,
                               cache_key=cache_key,
                               cache_type=cache_type,
                               cache_expire=cache_expire)
示例#12
0
    def __call__(self, template_name, template_vars, cache_key=None,
                 cache_type=None, cache_expire=None, method='xhtml'):
        """Render a template with Kajiki

        Accepts the cache options ``cache_key``, ``cache_type``, and
        ``cache_expire`` in addition to method which are passed to Kajiki's
        render function.

        """
        # Create a render callable for the cache function
        def render_template():
            # Grab a template reference
            template = self.loader.load(template_name)
            return Markup(template(template_vars).render())

        return cached_template(template_name, render_template,
                               cache_key=cache_key, cache_type=cache_type,
                               cache_expire=cache_expire,
                               ns_options=('method'), method=method)
示例#13
0
文件: kajiki.py 项目: Cito/tg2
    def __call__(self, template_name, template_vars, cache_key=None,
                 cache_type=None, cache_expire=None, method='xhtml'):
        """Render a template with Kajiki

        Accepts the cache options ``cache_key``, ``cache_type``, and
        ``cache_expire`` in addition to method which are passed to Kajiki's
        render function.

        """
        # Create a render callable for the cache function
        def render_template():
            # Grab a template reference
            template = self.loader.load(template_name)
            return Markup(template(template_vars).render())

        return cached_template(template_name, render_template,
                               cache_key=cache_key, cache_type=cache_type,
                               cache_expire=cache_expire,
                               ns_options=('method'), method=method)
示例#14
0
    def __call__(self, template_name, template_vars, **kwargs):

        # Gets document type from content type or from config options
        doctype = 'application/vnd.oasis.opendocument.text'
        method='odt'
        kwargs['doctype'] = doctype
        kwargs['method'] = method

        def render_template():
            finder = config['pylons.app_globals'].dotted_filename_finder
            odt_template_file = finder.get_dotted_filename(
                             template_name=template_name,
                             template_extension=".odt")
            output = NamedTemporaryFile()
            t = Template(odt_template_file, output.name)
            t.render(dict(document=template_vars))
            return output.read()

        return cached_template(
            template_name, render_template,
            **kwargs)
    def __call__(self, template_name, template_vars, **kwargs):
        """Render the template_vars with the Chameleon-Genshi template."""
        config = self.tg_config

        # Gets template format from content type or from config options
        format = kwargs.get('format')
        if not format:
            format = self.format_for_content_type.get(tg.response.content_type)
            if not format:
                format = config.get('templating.chameleon.genshi.format')
                if not format:
                    format = config.get('templating.genshi.method')
                    if not format or format not in ('xml', 'text'):
                        format = 'xml'

        def render_template():
            template = self.load_template(template_name, format=format)
            return Markup(template.render(**template_vars))

        return cached_template(template_name, render_template,
                               ns_options=('doctype', 'method'), **kwargs)
示例#16
0
文件: mako.py 项目: 984958198/tg2
    def __call__(self,
                 template_name,
                 template_vars,
                 cache_key=None,
                 cache_type=None,
                 cache_expire=None):

        if self.use_dotted_templatenames:
            template_name = self.dotted_finder.get_dotted_filename(
                template_name, template_extension='.mak')

        # Create a render callable for the cache function
        def render_template():
            # Grab a template reference
            template = self.loader.get_template(template_name)
            return Markup(template.render_unicode(**template_vars))

        return cached_template(template_name,
                               render_template,
                               cache_key=cache_key,
                               cache_type=cache_type,
                               cache_expire=cache_expire)