def get_template_reference(template): lex = lexer.Lexer(template) node = lex.parse() # Dummy compiler. _Identifiers class requires one # but only interested in the reserved_names field compiler = lambda: None compiler.reserved_names = set() identifiers = codegen._Identifiers(compiler, node) return list(identifiers.undeclared)
def get_template_reference(template): lex = lexer.Lexer(template) try: node = lex.parse() except MakoException as e: logger.warning('pipeline get template[%s] reference error[%s]' % (template, e)) return [] # Dummy compiler. _Identifiers class requires one # but only interested in the reserved_names field def compiler(): return None compiler.reserved_names = set() identifiers = codegen._Identifiers(compiler, node) return list(identifiers.undeclared)
def _mako_template_names(template): """ Return all the used identifiers in the Mako template. From Igonato's code at https://stackoverflow.com/a/23577289/622408. """ from mako import lexer, codegen lexer = lexer.Lexer(template) node = lexer.parse() # ^ The node is the root element for the parse tree. # The tree contains all the data from a template # needed for the code generation process # Dummy compiler. _Identifiers class requires one # but only interested in the reserved_names field compiler = lambda: None compiler.reserved_names = set() identifiers = codegen._Identifiers(compiler, node) return identifiers.undeclared
""" from mako.template import Template bar = Template('Basic template with ${name}.') print('call: bar.render(name=\'...\')') print(bar.render(name='testing epta')) ################################################# # how to find identifiers in bar? from mako import lexer, codegen lexer = lexer.Lexer(bar.source) node = lexer.parse() compiler = lambda: None compiler.reserved_names = set() identifiers = codegen._Identifiers(compiler, node) # All template variables can be found found using this # object but you are probably interested in the # undeclared variables: print('\nIdentifiers in bar template:') print(identifiers.undeclared)