Python jinja2 模块,ext() 实例源码

我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用jinja2.ext()

项目:vj4    作者:vijos    | 项目源码 | 文件源码
def __init__(self):
    super(Environment, self).__init__(
        loader=jinja2.FileSystemLoader(path.join(path.dirname(__file__), 'ui/templates')),
        extensions=[jinja2.ext.with_],
        auto_reload=options.debug,
        autoescape=True,
        trim_blocks=True,
        undefined=Undefined)
    globals()[self.__class__.__name__] = lambda: self  # singleton

    self.globals['vj4'] = vj4
    self.globals['static_url'] = lambda s: options.cdn_prefix + staticmanifest.get(s)
    self.globals['paginate'] = misc.paginate

    self.filters['nl2br'] = misc.nl2br
    self.filters['markdown'] = misc.markdown
    self.filters['json'] = json.encode
    self.filters['gravatar_url'] = misc.gravatar_url
    self.filters['format_size'] = misc.format_size
    self.filters['format_seconds'] = misc.format_seconds
    self.filters['base64_encode'] = misc.base64_encode
项目:deb-python-django-compressor    作者:openstack    | 项目源码 | 文件源码
def _get_jinja2_env(self):
        import jinja2
        import jinja2.ext
        from compressor.offline.jinja2 import url_for, SpacelessExtension
        from compressor.contrib.jinja2ext import CompressorExtension

        # Extensions needed for the test cases only.
        extensions = [
            CompressorExtension,
            SpacelessExtension,
            jinja2.ext.with_,
            jinja2.ext.do,
        ]
        loader = self._get_jinja2_loader()
        env = jinja2.Environment(extensions=extensions, loader=loader)
        env.globals['url_for'] = url_for

        return env
项目:Anubis    作者:KawashiroNitori    | 项目源码 | 文件源码
def __init__(self):
        super(Environment, self).__init__(
            loader=jinja2.FileSystemLoader(path.join(path.dirname(__file__), 'ui/templates')),
            extensions=[jinja2.ext.with_],
            auto_reload=options.options.debug,
            autoescape=True,
            trim_blocks=True,
            undefined=Undefined
        )
        globals()[self.__class__.__name__] = lambda: self

        self.globals['anubis'] = anubis
        self.globals['static_url'] = lambda s: options.options.cdn_prefix + s
        self.globals['paginate'] = paginate

        self.filters['markdown'] = markdown
        self.filters['json'] = json.encode
        self.filters['gravatar_url'] = gravatar_url
        self.filters['to_size'] = to_size
项目:kamidana    作者:podhmo    | 项目源码 | 文件源码
def _make_environment(load, additionals, extensions):
    extensions.append(ext.with_)
    env = jinja2.Environment(
        loader=jinja2.FunctionLoader(load),
        undefined=jinja2.StrictUndefined,
        trim_blocks=True,
        lstrip_blocks=True,
        extensions=extensions,
    )
    for name, defs in additionals.items():
        getattr(env, name).update(defs)
    return env
项目:grow-ext-google-cloud-images    作者:grow    | 项目源码 | 文件源码
def cache(self):
        if self._cache is None:
            podcache = self.pod.podcache
            ident = 'ext-google-cloud-images'
            self._cache = podcache.get_object_cache(ident, write_to_file=True)
        return self._cache
项目:pytemplate    作者:krotos139    | 项目源码 | 文件源码
def main(options):
    logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

    global arguments
    arguments = ""
    if (options.arg):
        exec("%s = %s" % ("tmp",  options.arg))
        arguments = tmp

    logging.info("Load Environment")
    env = Environment(loader=FileSystemLoader(".", encoding='utf-8'), extensions=["jinja2.ext.do",])
    env.globals.update(load_csv = load_csv)
    env.globals.update(load_xml = load_xml)
    env.globals.update(load_sqlite = load_sqlite)
    env.globals.update(load_text = load_text)
    env.globals.update(le = le)
    env.globals.update(log = log)
    env.globals.update(file_md5 = file_md5)
    env.globals.update(file_stat = file_stat)
    env.globals.update(getarg = getarg)


    if (options.format == "odt"):
        logging.info("Read ODT template")
        engine = secretary.Renderer(environment=env)
        template = open(options.template, 'rb')
        logging.info("Template rendering...")
        output_data = engine.render(template)
        logging.info("Template rendering done")
        output_file = open(options.output, 'wb')
        output_file.write(output_data)
        output_file.close()
        logging.info("Done")

    elif (options.format == "text"):
        logging.info("Read TEXT template")
        template = env.get_template(options.template)
        logging.info("Template rendering...")
        output_data = template.render( )
        logging.info("Template rendering done")
        output_file = open(options.output, 'wb')
        output_file.write(output_data.encode('utf8'))
        output_file.close()
        logging.info("Done")
    else:
        logging.info("Error format")