def define_block(self, name, block): """Adds a new BLOCK definition to the local BLOCKS cache. block may be specified as a callable or template.document.Document object or as text which is compiled into a template. Returns a true value ('block' or the compiled block reference) if successful, or raises a TemplateException on failure. """ # NOTE: This function is entirely untested by the test suite. if isinstance(block, str): block = self.template(util.Literal(block)) self.__blocks[name] = block
def __init__(self, config): self.__load_templates = util.listify( config.get("LOAD_TEMPLATES") or Provider(config)) self.__load_plugins = util.listify( config.get("LOAD_PLUGINS") or Plugins(config)) self.__load_filters = util.listify( config.get("LOAD_FILTERS") or Filters(config)) prefix_map = config.get("PREFIX_MAP") or {} self.__filter_cache = {} self.__prefix_map = {} for key, value in prefix_map.items(): if isinstance(value, str): self.__prefix_map[key] = util.slice( self.__load_templates, [int(x) for x in re.split(r"\D+", value)]) else: self.__prefix_map[key] = value if "STASH" in config: self.__stash = config["STASH"] else: predefs = config.get("VARIABLES") or config.get("PRE_DEFINE") or {} predefs.setdefault("_DEBUG", int(bool(config.get("DEBUG", 0) & DEBUG_UNDEF))) self.__stash = Stash(predefs) # compile any template BLOCKS specified as text blocks = config.get("BLOCKS") or {} b = {} for key, block in blocks.items(): if isinstance(block, str): block = self.template(util.Literal(block)) b[key] = block self.__init_blocks = self.__blocks = b self.__recursion = config.get("RECURSION", False) self.__eval_python = config.get("EVAL_PYTHON", False) self.__trim = config.get("TRIM", False) self.__blkstack = [] self.__config = config if config.get("EXPOSE_BLOCKS") is not None: self.__expose_blocks = config.get("EXPOSE_BLOCKS") else: self.__expose_blocks = False self.__debug_format = config.get("DEBUG_FORMAT") self.__debug_dirs = config.get("DEBUG", 0) & DEBUG_DIRS if config.get("DEBUG") is not None: self.__debug = config["DEBUG"] & (DEBUG_CONTEXT | DEBUG_FLAGS) else: self.__debug = self.DEBUG
def eval_filter(text=""): return context.process(util.Literal(str(text)))