Beispiel #1
0
def render(template_name,
           extra_vars=None,
           form_fill=None,
           form_errors={},
           cache_expire=3600,
           cache_private=False,
           method='xhtml'):

    _setup_cache(cache_expire, cache_private)

    # Pull in extra vars if needed
    globs = extra_vars or {}

    # Second, get the globals
    globs.update(pylons_globals())
    globs['g'] = app_globals
    globs['can'] = can
    globs['_form_errors'] = form_errors

    # Grab a template reference
    template = globs['app_globals'].genshi_loader.load(template_name)

    stream = template.generate(**globs)

    if form_fill is not None:
        filler = HTMLFormFiller(data=form_fill)
        stream = stream | filler

    for item in PluginImplementations(IGenshiStreamFilter):
        stream = item.filter(stream)

    return literal(stream.render(method=method, encoding=None))
Beispiel #2
0
    def render_template():
        globs = extra_vars or {}
        globs.update(pylons_globals())

        # Using pylons.url() directly destroys the localisation stuff so
        # we remove it so any bad templates crash and burn
        del globs['url']

        try:
            template_path, template_type = render_.template_info(template_name)
        except render_.TemplateNotFound:
            raise

        log.debug('rendering %s [%s]' % (template_path, template_type))
        if config.get('debug'):
            context_vars = globs.get('c')
            if context_vars:
                context_vars = dir(context_vars)
            debug_info = {'template_name': template_name,
                          'template_path': template_path,
                          'template_type': template_type,
                          'vars': globs,
                          'c_vars': context_vars,
                          'renderer': renderer}
            if 'CKAN_DEBUG_INFO' not in request.environ:
                request.environ['CKAN_DEBUG_INFO'] = []
            request.environ['CKAN_DEBUG_INFO'].append(debug_info)

        del globs['config']
        return render_jinja2(template_name, globs)
Beispiel #3
0
def render(template_name,
           extra_vars=None,
           form_fill=None,
           form_errors={},
           method='xhtml'):

    # Pull in extra vars if needed
    globs = extra_vars or {}

    # Second, get the globals
    globs.update(pylons_globals())
    globs['g'] = app_globals
    globs['can'] = can
    globs['_form_errors'] = form_errors

    # Grab a template reference
    template = globs['app_globals'].genshi_loader.load(template_name)

    stream = template.generate(**globs)

    if form_fill is not None:
        filler = HTMLFormFiller(data=form_fill)
        stream = stream | filler

    return literal(stream.render(method=method, encoding=None))
Beispiel #4
0
def render(
    template_name,
    extra_vars=None,
    form_fill=None,
    form_errors={},
    cache_expire=3600,
    cache_private=False,
    method="xhtml",
):

    _setup_cache(cache_expire, cache_private)

    # Pull in extra vars if needed
    globs = extra_vars or {}

    # Second, get the globals
    globs.update(pylons_globals())
    globs["g"] = app_globals
    globs["_form_errors"] = form_errors

    # Grab a template reference
    template = globs["app_globals"].genshi_loader.load(template_name)

    stream = template.generate(**globs)

    if form_fill is not None:
        filler = HTMLFormFiller(data=form_fill)
        stream = stream | filler

    for item in PluginImplementations(IGenshiStreamFilter):
        stream = item.filter(stream)

    return literal(stream.render(method=method, encoding=None))
Beispiel #5
0
    def render_template():
        globs = extra_vars or {}
        globs.update(pylons_globals())
        globs['actions'] = model.Action

        # Using pylons.url() directly destroys the localisation stuff so
        # we remove it so any bad templates crash and burn
        del globs['url']

        try:
            template_path, template_type = render_.template_info(template_name)
        except render_.TemplateNotFound:
            template_type = 'genshi'
            template_path = ''

        # snippets should not pass the context
        # but allow for legacy genshi templates
        if renderer == 'snippet' and template_type != 'genshi':
            del globs['c']
            del globs['tmpl_context']

        log.debug('rendering %s [%s]' % (template_path, template_type))
        if config.get('debug'):
            context_vars = globs.get('c')
            if context_vars:
                context_vars = dir(context_vars)
            debug_info = {
                'template_name': template_name,
                'template_path': template_path,
                'template_type': template_type,
                'vars': globs,
                'c_vars': context_vars,
                'renderer': renderer
            }
            if 'CKAN_DEBUG_INFO' not in request.environ:
                request.environ['CKAN_DEBUG_INFO'] = []
            request.environ['CKAN_DEBUG_INFO'].append(debug_info)

        # Jinja2 templates
        if template_type == 'jinja2':
            # We don't want to have the config in templates it should be
            # accessed via g (app_globals) as this gives us flexability such
            # as changing via database settings.
            del globs['config']
            # TODO should we raise error if genshi filters??
            return render_jinja2(template_name, globs)

        # Genshi templates
        template = globs['app_globals'].genshi_loader.load(
            template_name.encode('utf-8'), cls=loader_class)
        stream = template.generate(**globs)

        for item in p.PluginImplementations(p.IGenshiStreamFilter):
            stream = item.filter(stream)

        if loader_class == NewTextTemplate:
            return literal(stream.render(method="text", encoding=None))

        return literal(
            stream.render(method=method, encoding=None, strip_whitespace=True))
Beispiel #6
0
    def render_template():
        globs = extra_vars or {}
        globs.update(pylons_globals())

        # Using pylons.url() directly destroys the localisation stuff so
        # we remove it so any bad templates crash and burn
        del globs["url"]

        try:
            template_path, template_type = render_.template_info(template_name)
        except render_.TemplateNotFound:
            raise

        log.debug("rendering %s [%s]" % (template_path, template_type))
        if config.get("debug"):
            context_vars = globs.get("c")
            if context_vars:
                context_vars = dir(context_vars)
            debug_info = {
                "template_name": template_name,
                "template_path": template_path,
                "template_type": template_type,
                "vars": globs,
                "c_vars": context_vars,
                "renderer": renderer,
            }
            if "CKAN_DEBUG_INFO" not in request.environ:
                request.environ["CKAN_DEBUG_INFO"] = []
            request.environ["CKAN_DEBUG_INFO"].append(debug_info)

        del globs["config"]
        return render_jinja2(template_name, globs)
Beispiel #7
0
def render(template_name, extra_vars=None, form_fill=None, form_errors={}, suppress_cache=True, method="xhtml"):

    if suppress_cache:
        response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
        response.headers["Pragma"] = "no-cache"
        response.headers["Expires"] = "0"

    # Pull in extra vars if needed
    globs = extra_vars or {}

    # Second, get the globals
    globs.update(pylons_globals())
    globs["g"] = app_globals
    globs["can"] = can
    globs["_form_errors"] = form_errors

    # Grab a template reference
    template = globs["app_globals"].genshi_loader.load(template_name)

    stream = template.generate(**globs)

    if form_fill is not None:
        filler = HTMLFormFiller(data=form_fill)
        stream = stream | filler

    return literal(stream.render(method=method, encoding=None))
Beispiel #8
0
def render(template_name,
           extra_vars=None,
           form_fill=None, form_errors={},
           suppress_cache=True,
           method='xhtml'):

    if suppress_cache:
        response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
        response.headers['Pragma'] = 'no-cache'
        response.headers['Expires'] = '0'

    # Pull in extra vars if needed
    globs = extra_vars or {}

    # Second, get the globals
    globs.update(pylons_globals())
    globs['g'] = app_globals
    globs['can'] = can
    globs['_form_errors'] = form_errors

    # Grab a template reference
    template = globs['app_globals'].genshi_loader.load(template_name)

    stream = template.generate(**globs)

    if form_fill is not None:
        filler = HTMLFormFiller(data=form_fill)
        stream = stream | filler

    for item in PluginImplementations(IGenshiStreamFilter):
        stream = item.filter(stream)

    return literal(stream.render(method=method, encoding=None))
Beispiel #9
0
    def render_template():
        globs = extra_vars or {}
        globs.update(pylons_globals())

        # Using pylons.url() directly destroys the localisation stuff so
        # we remove it so any bad templates crash and burn
        del globs['url']

        try:
            template_path, template_type = render_.template_info(template_name)
        except render_.TemplateNotFound:
            raise

        log.debug('rendering %s [%s]' % (template_path, template_type))
        if config.get('debug'):
            context_vars = globs.get('c')
            if context_vars:
                context_vars = dir(context_vars)
            debug_info = {
                'template_name': template_name,
                'template_path': template_path,
                'template_type': template_type,
                'vars': globs,
                'c_vars': context_vars,
                'renderer': renderer
            }
            if 'CKAN_DEBUG_INFO' not in request.environ:
                request.environ['CKAN_DEBUG_INFO'] = []
            request.environ['CKAN_DEBUG_INFO'].append(debug_info)

        del globs['config']
        return render_jinja2(template_name, globs)
Beispiel #10
0
    def render_template():
        globs = extra_vars or {}
        globs.update(pylons_globals())
        globs['actions'] = model.Action

        # Using pylons.url() directly destroys the localisation stuff so
        # we remove it so any bad templates crash and burn
        del globs['url']

        try:
            template_path, template_type = render_.template_info(template_name)
        except render_.TemplateNotFound:
            template_type = 'genshi'
            template_path = ''

        # snippets should not pass the context
        # but allow for legacy genshi templates
        if renderer == 'snippet' and template_type != 'genshi':
            del globs['c']
            del globs['tmpl_context']

        log.debug('rendering %s [%s]' % (template_path, template_type))
        if config.get('debug'):
            context_vars = globs.get('c')
            if context_vars:
                context_vars = dir(context_vars)
            debug_info = {'template_name': template_name,
                          'template_path': template_path,
                          'template_type': template_type,
                          'vars': globs,
                          'c_vars': context_vars,
                          'renderer': renderer}
            if 'CKAN_DEBUG_INFO' not in request.environ:
                request.environ['CKAN_DEBUG_INFO'] = []
            request.environ['CKAN_DEBUG_INFO'].append(debug_info)

        # Jinja2 templates
        if template_type == 'jinja2':
            # We don't want to have the config in templates it should be
            # accessed via g (app_globals) as this gives us flexability such
            # as changing via database settings.
            del globs['config']
            # TODO should we raise error if genshi filters??
            return render_jinja2(template_name, globs)

        # Genshi templates
        template = globs['app_globals'].genshi_loader.load(
            template_name.encode('utf-8'), cls=loader_class
        )
        stream = template.generate(**globs)

        for item in p.PluginImplementations(p.IGenshiStreamFilter):
            stream = item.filter(stream)

        if loader_class == NewTextTemplate:
            return literal(stream.render(method="text", encoding=None))

        return literal(stream.render(method=method, encoding=None,
                                     strip_whitespace=True))
Beispiel #11
0
 def render_template():
     # First, get the globals
     globs = pylons_globals()
     
     # Grab a template reference
     template = genshi_loader.load(template_name)
     
     return template.generate(**globs).render()
Beispiel #12
0
    def render_template():
        globs = extra_vars or {}
        globs.update(pylons_globals())
        globs["actions"] = model.Action

        # Using pylons.url() directly destroys the localisation stuff so
        # we remove it so any bad templates crash and burn
        del globs["url"]

        try:
            template_path, template_type = lib.render.template_info(template_name)
        except lib.render.TemplateNotFound:
            template_type = "genshi"
            template_path = ""

        # snippets should not pass the context
        # but allow for legacy genshi templates
        if renderer == "snippet" and template_type != "genshi":
            del globs["c"]
            del globs["tmpl_context"]

        log.debug("rendering %s [%s]" % (template_path, template_type))
        if config.get("debug"):
            context_vars = globs.get("c")
            if context_vars:
                context_vars = dir(context_vars)
            debug_info = {
                "template_name": template_name,
                "template_path": template_path,
                "template_type": template_type,
                "vars": globs,
                "c_vars": context_vars,
                "renderer": renderer,
            }
            if "CKAN_DEBUG_INFO" not in request.environ:
                request.environ["CKAN_DEBUG_INFO"] = []
            request.environ["CKAN_DEBUG_INFO"].append(debug_info)

        # Jinja2 templates
        if template_type == "jinja2":
            # We don't want to have the config in templates it should be
            # accessed via g (app_globals) as this gives us flexability such
            # as changing via database settings.
            del globs["config"]
            # TODO should we raise error if genshi filters??
            return render_jinja2(template_name, globs)

        # Genshi templates
        template = globs["app_globals"].genshi_loader.load(template_name, cls=loader_class)
        stream = template.generate(**globs)

        for item in PluginImplementations(IGenshiStreamFilter):
            stream = item.filter(stream)

        if loader_class == NewTextTemplate:
            return literal(stream.render(method="text", encoding=None))

        return literal(stream.render(method=method, encoding=None, strip_whitespace=True))
Beispiel #13
0
def render_def(template_name, name, **kwargs):
    globs = pylons_globals()
 
    if kwargs:
        globs = globs.copy()
        globs.update(kwargs)
 
    template = globs['app_globals'].mako_lookup.get_template(template_name).get_def(name)
    return literal(template.render_unicode(**globs))
Beispiel #14
0
 def display(self, *args, **kw):
     # TODO: See if this is still necessary. Furthermore, find out which variables it actually adds.
     # Update the kwargs with the same values that are included in main templates
     # this allows us to access the following objects in widget templates:
     # ['tmpl_context', 'translator', 'session', 'ungettext', 'response', '_',
     #  'c', 'app_globals', 'g', 'url', 'h', 'request', 'helpers', 'N_', 'tg',
     #  'config']
     kw.update(pylons_globals())
     return forms.Widget.display(self, *args, **kw)
Beispiel #15
0
 def display(self, *args, **kw):
     # Update the kwargs with the same values that are included in main templates
     # this allows us to access the following objects in widget templates:
     # ['tmpl_context', 'translator', 'session', 'ungettext', 'response', '_',
     #  'c', 'app_globals', 'g', 'url', 'h', 'request', 'helpers', 'N_', 'tg',
     #  'config']
     kw.update(_get_tg_vars())
     kw.update(pylons_globals())
     return forms.Widget.display(self, *args, **kw)
Beispiel #16
0
    def render_template():
        # Pull in extra vars if needed
        globs = extra_vars or {}

        # Second, get the globals
        globs.update(templating.pylons_globals())

        # Grab a template reference
        template = globs['app_globals'].kajiki_loader.load(template_name)

        return literal(template(globs).render())
Beispiel #17
0
    def render_template():
        # Pull in extra vars if needed
        globs = extra_vars or {}

        # Second, get the globals
        globs.update(templating.pylons_globals())

        # Grab a template reference
        template = globs['app_globals'].kajiki_loader.load(template_name)

        return literal(template(globs).render())
Beispiel #18
0
 def render_template():
     # Pull in extra vars if needed
     globs = {}
     
     # Second, get the globals
     globs.update(pylons_globals())
     
     # Grab a template reference
     template = \
         getattr(globs['app_globals'].jinja2_env.get_template(template_name).make_module(vars=globs), name)(**kwargs)
     return template        
Beispiel #19
0
 def render_template():
     globs = extra_vars or {}
     globs.update(pylons_globals())
     globs['actions'] = model.Action
     template = globs['app_globals'].genshi_loader.load(template_name,
         cls=loader_class)
     stream = template.generate(**globs)
     
     for item in PluginImplementations(IGenshiStreamFilter):
         stream = item.filter(stream)
     
     return literal(stream.render(method=method, encoding=None))
Beispiel #20
0
    def render_template():
        # Pull in extra vars if needed
        globs = extra_vars or {}

        # Second, get the globals
        globs.update(pylons_globals())

        # Grab a template reference
        template = \
            globs['app_globals'].pytiles_container.get_template(template_name)

        return literal(template.render(**globs))
Beispiel #21
0
def render(template_name, tmpl_params=None, loader=None, fragment=False):
    if tmpl_params is None:
        tmpl_params = {}

    g = pylons_globals()
    tmpl_params.update(g)

    try:
        opts = g['app_globals'].breve_opts
    except AttributeError:
        opts = {}

    t = Template(tags, **opts)
    return t.render(template_name, params=tmpl_params, loader=loader, fragment=fragment)
Beispiel #22
0
 def content(self, path):
     """Handles returning "content" files: static content shoved more or
     less verbatim into the wrapper.
     """
     # Static files need to go through Mako to e.g. set their titles and
     # generate links, but TemplateLookup will not fetch templates outside
     # its known template directories.
     # Instead, load the template manually, and use the same lookup object
     # as everything else so references to other templates can still work
     # XXX when there's a real cache mechanism, this should use it!
     lookup = config['pylons.app_globals'].mako_lookup
     template = Template(filename=path, lookup=lookup,
                         **lookup.template_args)
     return template.render_unicode(**pylons_globals())
Beispiel #23
0
 def render_template():
     # Get the globals
     # Note: when running in restricted mode which of these globals are
     # to be exposed should be a lot more selective. 
     pg = pylons_globals()
     
     # Update any extra vars if needed
     if extra_vars:
         pg.update(extra_vars)
     
     # Grab a template reference
     template = pg['app_globals'].evoque_domain.get_template(
         template_name, collection=collection, raw=raw, quoting=quoting)
     return template.evoque(pg, raw=raw, quoting=quoting)
Beispiel #24
0
    def render_template():
        # Pull in extra vars if needed
        globs = extra_vars or {}

        # Second, get the globals
        globs.update(pylons_globals())
        globs['g'] = app_globals
        globs['_form_errors'] = form_errors

        # Grab a template reference
        template = globs['app_globals'].genshi_loader.load(template_name)
        stream = template.generate(**globs)
        if form_fill is not None:
            filler = HTMLFormFiller(data=form_fill)
            stream = stream | filler

        return literal(stream.render(method=method, encoding=None))
Beispiel #25
0
def render(template_name, tmpl_vars=None, loader=None, fragment=False):
    if tmpl_vars is None:
        tmpl_vars = {}

    g = pylons_globals()
    tmpl_vars.update(g)

    try:
        opts = g['app_globals'].breve_opts
    except AttributeError:
        opts = {}

    t = Template(tags, **opts)
    return t.render(template_name,
                    vars=tmpl_vars,
                    loader=loader,
                    fragment=fragment)
Beispiel #26
0
    def render_template():
        globs = extra_vars or {}
        globs.update(pylons_globals())
        globs['actions'] = model.Action

        # Using pylons.url() or pylons.url_for() directly destroys the
        # localisation stuff so we remove it so any bad templates crash
        # and burn
        del globs['url']

        template = globs['app_globals'].genshi_loader.load(template_name,
            cls=loader_class)
        stream = template.generate(**globs)
        
        for item in PluginImplementations(IGenshiStreamFilter):
            stream = item.filter(stream)
        
        return literal(stream.render(method=method, encoding=None, strip_whitespace=False))
Beispiel #27
0
    def render_template():
        # Pull in extra vars if needed
        globs = extra_vars or {}
        
        # Second, get the globals
        globs.update(pylons_globals())

        # Grab a template reference
        template = globs['app_globals'].genshi_loader.load(template_name)

        stream = template.generate(**globs) | DocTypeInserter(DocType.HTML5)

        if form_data:
            stream = stream | HTMLFormFiller(data=form_data)

        if form_errors:
            stream = stream | HTMLFormErrors(errors=form_errors)
 
        return literal(stream.render(method=method, encoding=None))
Beispiel #28
0
    def render_template():
        globs = extra_vars or {}
        globs.update(pylons_globals())
        globs["actions"] = model.Action
        # add the template name to the context to help us know where we are
        # used in depreciating functions etc
        c.__template_name = template_name

        # Using pylons.url() directly destroys the localisation stuff so
        # we remove it so any bad templates crash and burn
        del globs["url"]

        template = globs["app_globals"].genshi_loader.load(template_name, cls=loader_class)
        stream = template.generate(**globs)

        for item in PluginImplementations(IGenshiStreamFilter):
            stream = item.filter(stream)

        if loader_class == NewTextTemplate:
            return literal(stream.render(method="text", encoding=None))

        return literal(stream.render(method=method, encoding=None, strip_whitespace=True))
Beispiel #29
0
def render(template, tmpl_vars=None, method=None):
    """Generate a markup stream from the given template and vars.

    :param template: A template path.
    :param tmpl_vars: A dict of variables to pass into the template.
    :param method: Optional serialization method for Genshi to use.
        If None, we don't serialize the markup stream into a string.
        Provide 'auto' to use the best guess. See :func:`render_stream`.
    :rtype: :class:`genshi.Stream` or :class:`genshi.Markup`
    :returns: An iterable markup stream, or a serialized markup string
        if `method` was not None.

    """
    if tmpl_vars is None:
        tmpl_vars = {}
    assert isinstance(tmpl_vars, dict), \
        'tmpl_vars must be a dict or None, given: %r' % tmpl_vars

    # Steal a page from TurboGears' book:
    # include the genshi XML helper for convenience in templates.
    tmpl_vars.setdefault('XML', XML)
    tmpl_vars.update(pylons_globals())

    # Pass in all the plugin templates that will manipulate this template
    # The idea is that these paths should be <xi:include> somewhere in the
    # top of the template file.
    plugin_templates = app_globals.plugin_mgr.match_templates(template)
    tmpl_vars['plugin_templates'] = plugin_templates

    # Grab a template reference and apply the template context
    tmpl = app_globals.genshi_loader.load(template)
    stream = tmpl.generate(**tmpl_vars)

    if method is None:
        return stream
    else:
        return render_stream(stream, method=method, template_name=template)
Beispiel #30
0
    def render_template():
        globs = extra_vars or {}
        globs.update(pylons_globals())
        globs['actions'] = model.Action
        # add the template name to the context to help us know where we are
        # used in depreciating functions etc
        c.__template_name = template_name

        # Using pylons.url() directly destroys the localisation stuff so
        # we remove it so any bad templates crash and burn
        del globs['url']

        template = globs['app_globals'].genshi_loader.load(template_name,
                                                           cls=loader_class)
        stream = template.generate(**globs)

        for item in PluginImplementations(IGenshiStreamFilter):
            stream = item.filter(stream)

        if loader_class == NewTextTemplate:
            return literal(stream.render(method="text", encoding=None))

        return literal(
            stream.render(method=method, encoding=None, strip_whitespace=True))
Beispiel #31
0
def make_template():
    globs = pylons_globals()

    template = Template(filename = globs['config']['pylons.paths']['templates'][0]
                        + "/base/signin.mako")
    return template.render(time = str(datetime.datetime.now()))