笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册

API

发布者: 笨鸟自学网



classjinja2.ChoiceLoader(loaders)

This loader works like the PrefixLoader just that no prefix is specified. If a template could not be found by one loader the next one is tried.

>>> loader = ChoiceLoader([
...     FileSystemLoader('/path/to/user/templates'),
...     FileSystemLoader('/path/to/system/templates')
... ])

This is useful if you want to allow users to override builtin templates from a different location.

classjinja2.ModuleLoader(path)

This loader loads templates from precompiled templates.

Example usage:

>>> loader = ChoiceLoader([
...     ModuleLoader('/path/to/compiled/templates'),
...     FileSystemLoader('/path/to/templates')
... ])

Templates can be precompiled with Environment.compile_templates().

字节码缓存

Jinja 2.1 和更高的版本支持外部字节码缓存。字节码缓存使得在首次使用时把生成的字节码 存储到文件系统或其它位置来避免处理模板。

这在当你有一个在首个应用初始化的 web 应用, Jinja 一次性编译大量模板拖慢应用时尤其 有用。

要使用字节码缓存,把它实例化并传给 Environment 

classjinja2.BytecodeCache

To implement your own bytecode cache you have to subclass this class and override load_bytecode() and dump_bytecode(). Both of these methods are passed a Bucket.

A very basic bytecode cache that saves the bytecode on the file system:

from os import path

class MyCache(BytecodeCache):

    def __init__(self, directory):
        self.directory = directory

    def load_bytecode(self, bucket):
        filename = path.join(self.directory, bucket.key)
        if path.exists(filename):
            with open(filename, 'rb') as f:
                bucket.load_bytecode(f)

    def dump_bytecode(self, bucket):
        filename = path.join(self.directory, bucket.key)
        with open(filename, 'wb') as f:
            bucket.write_bytecode(f)

A more advanced version of a filesystem based bytecode cache is part of Jinja2.

clear()

Clears the cache. This method is not used by Jinja2 but should be implemented to allow applications to clear the bytecode cache used by a particular environment.

dump_bytecode(bucket)

Subclasses have to override this method to write the bytecode from a bucket back to the cache. If it unable to do so it must not fail silently but raise an exception.

load_bytecode(bucket)

Subclasses have to override this method to load bytecode into a bucket. If they are not able to find code in the cache for the bucket, it must not do anything.

classjinja2.bccache.Bucket(environmentkeychecksum)

Buckets are used to store the bytecode for one template. It’s created and initialized by the bytecode cache and passed to the loading functions.

The buckets get an internal checksum from the cache assigned and use this to automatically reject outdated cache material. Individual bytecode cache subclasses don’t have to care about cache invalidation.

environment

创建 bucket 的 Environment

key

该 bucket 的唯一键

code

如果已加载,则为字节码,否则为 None 。

bytecode_from_string(string)

Load bytecode from a string.

bytecode_to_string()

Return the bytecode as string.

load_bytecode(f)

Loads bytecode from a file or file like object.

reset()

Resets the bucket (unloads the bytecode).

write_bytecode(f)

Dump the bytecode into the file or file like object passed.


上一篇:介绍下一篇:沙箱

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-9-17 04:40 , Processed in 0.027711 second(s), 17 queries .

© 2001-2020

返回顶部