You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. """
  2. This is the Django template system.
  3. How it works:
  4. The Lexer.tokenize() method converts a template string (i.e., a string
  5. containing markup with custom template tags) to tokens, which can be either
  6. plain text (TokenType.TEXT), variables (TokenType.VAR), or block statements
  7. (TokenType.BLOCK).
  8. The Parser() class takes a list of tokens in its constructor, and its parse()
  9. method returns a compiled template -- which is, under the hood, a list of
  10. Node objects.
  11. Each Node is responsible for creating some sort of output -- e.g. simple text
  12. (TextNode), variable values in a given context (VariableNode), results of basic
  13. logic (IfNode), results of looping (ForNode), or anything else. The core Node
  14. types are TextNode, VariableNode, IfNode and ForNode, but plugin modules can
  15. define their own custom node types.
  16. Each Node has a render() method, which takes a Context and returns a string of
  17. the rendered node. For example, the render() method of a Variable Node returns
  18. the variable's value as a string. The render() method of a ForNode returns the
  19. rendered output of whatever was inside the loop, recursively.
  20. The Template class is a convenient wrapper that takes care of template
  21. compilation and rendering.
  22. Usage:
  23. The only thing you should ever use directly in this file is the Template class.
  24. Create a compiled template object with a template_string, then call render()
  25. with a context. In the compilation stage, the TemplateSyntaxError exception
  26. will be raised if the template doesn't have proper syntax.
  27. Sample code:
  28. >>> from django import template
  29. >>> s = '<html>{% if test %}<h1>{{ varvalue }}</h1>{% endif %}</html>'
  30. >>> t = template.Template(s)
  31. (t is now a compiled template, and its render() method can be called multiple
  32. times with multiple contexts)
  33. >>> c = template.Context({'test':True, 'varvalue': 'Hello'})
  34. >>> t.render(c)
  35. '<html><h1>Hello</h1></html>'
  36. >>> c = template.Context({'test':False, 'varvalue': 'Hello'})
  37. >>> t.render(c)
  38. '<html></html>'
  39. """
  40. import logging
  41. import re
  42. from enum import Enum
  43. from inspect import getcallargs, getfullargspec, unwrap
  44. from django.template.context import ( # NOQA: imported for backwards compatibility
  45. BaseContext, Context, ContextPopException, RequestContext,
  46. )
  47. from django.utils.formats import localize
  48. from django.utils.html import conditional_escape, escape
  49. from django.utils.safestring import SafeData, mark_safe
  50. from django.utils.text import (
  51. get_text_list, smart_split, unescape_string_literal,
  52. )
  53. from django.utils.timezone import template_localtime
  54. from django.utils.translation import gettext_lazy, pgettext_lazy
  55. from .exceptions import TemplateSyntaxError
  56. # template syntax constants
  57. FILTER_SEPARATOR = '|'
  58. FILTER_ARGUMENT_SEPARATOR = ':'
  59. VARIABLE_ATTRIBUTE_SEPARATOR = '.'
  60. BLOCK_TAG_START = '{%'
  61. BLOCK_TAG_END = '%}'
  62. VARIABLE_TAG_START = '{{'
  63. VARIABLE_TAG_END = '}}'
  64. COMMENT_TAG_START = '{#'
  65. COMMENT_TAG_END = '#}'
  66. TRANSLATOR_COMMENT_MARK = 'Translators'
  67. SINGLE_BRACE_START = '{'
  68. SINGLE_BRACE_END = '}'
  69. # what to report as the origin for templates that come from non-loader sources
  70. # (e.g. strings)
  71. UNKNOWN_SOURCE = '<unknown source>'
  72. # match a variable or block tag and capture the entire tag, including start/end
  73. # delimiters
  74. tag_re = (re.compile('(%s.*?%s|%s.*?%s|%s.*?%s)' %
  75. (re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END),
  76. re.escape(VARIABLE_TAG_START), re.escape(VARIABLE_TAG_END),
  77. re.escape(COMMENT_TAG_START), re.escape(COMMENT_TAG_END))))
  78. logger = logging.getLogger('django.template')
  79. class TokenType(Enum):
  80. TEXT = 0
  81. VAR = 1
  82. BLOCK = 2
  83. COMMENT = 3
  84. class VariableDoesNotExist(Exception):
  85. def __init__(self, msg, params=()):
  86. self.msg = msg
  87. self.params = params
  88. def __str__(self):
  89. return self.msg % self.params
  90. class Origin:
  91. def __init__(self, name, template_name=None, loader=None):
  92. self.name = name
  93. self.template_name = template_name
  94. self.loader = loader
  95. def __str__(self):
  96. return self.name
  97. def __eq__(self, other):
  98. return (
  99. isinstance(other, Origin) and
  100. self.name == other.name and
  101. self.loader == other.loader
  102. )
  103. @property
  104. def loader_name(self):
  105. if self.loader:
  106. return '%s.%s' % (
  107. self.loader.__module__, self.loader.__class__.__name__,
  108. )
  109. class Template:
  110. def __init__(self, template_string, origin=None, name=None, engine=None):
  111. # If Template is instantiated directly rather than from an Engine and
  112. # exactly one Django template engine is configured, use that engine.
  113. # This is required to preserve backwards-compatibility for direct use
  114. # e.g. Template('...').render(Context({...}))
  115. if engine is None:
  116. from .engine import Engine
  117. engine = Engine.get_default()
  118. if origin is None:
  119. origin = Origin(UNKNOWN_SOURCE)
  120. self.name = name
  121. self.origin = origin
  122. self.engine = engine
  123. self.source = str(template_string) # May be lazy.
  124. self.nodelist = self.compile_nodelist()
  125. def __iter__(self):
  126. for node in self.nodelist:
  127. yield from node
  128. def _render(self, context):
  129. return self.nodelist.render(context)
  130. def render(self, context):
  131. "Display stage -- can be called many times"
  132. with context.render_context.push_state(self):
  133. if context.template is None:
  134. with context.bind_template(self):
  135. context.template_name = self.name
  136. return self._render(context)
  137. else:
  138. return self._render(context)
  139. def compile_nodelist(self):
  140. """
  141. Parse and compile the template source into a nodelist. If debug
  142. is True and an exception occurs during parsing, the exception is
  143. is annotated with contextual line information where it occurred in the
  144. template source.
  145. """
  146. if self.engine.debug:
  147. lexer = DebugLexer(self.source)
  148. else:
  149. lexer = Lexer(self.source)
  150. tokens = lexer.tokenize()
  151. parser = Parser(
  152. tokens, self.engine.template_libraries, self.engine.template_builtins,
  153. self.origin,
  154. )
  155. try:
  156. return parser.parse()
  157. except Exception as e:
  158. if self.engine.debug:
  159. e.template_debug = self.get_exception_info(e, e.token)
  160. raise
  161. def get_exception_info(self, exception, token):
  162. """
  163. Return a dictionary containing contextual line information of where
  164. the exception occurred in the template. The following information is
  165. provided:
  166. message
  167. The message of the exception raised.
  168. source_lines
  169. The lines before, after, and including the line the exception
  170. occurred on.
  171. line
  172. The line number the exception occurred on.
  173. before, during, after
  174. The line the exception occurred on split into three parts:
  175. 1. The content before the token that raised the error.
  176. 2. The token that raised the error.
  177. 3. The content after the token that raised the error.
  178. total
  179. The number of lines in source_lines.
  180. top
  181. The line number where source_lines starts.
  182. bottom
  183. The line number where source_lines ends.
  184. start
  185. The start position of the token in the template source.
  186. end
  187. The end position of the token in the template source.
  188. """
  189. start, end = token.position
  190. context_lines = 10
  191. line = 0
  192. upto = 0
  193. source_lines = []
  194. before = during = after = ""
  195. for num, next in enumerate(linebreak_iter(self.source)):
  196. if start >= upto and end <= next:
  197. line = num
  198. before = escape(self.source[upto:start])
  199. during = escape(self.source[start:end])
  200. after = escape(self.source[end:next])
  201. source_lines.append((num, escape(self.source[upto:next])))
  202. upto = next
  203. total = len(source_lines)
  204. top = max(1, line - context_lines)
  205. bottom = min(total, line + 1 + context_lines)
  206. # In some rare cases exc_value.args can be empty or an invalid
  207. # string.
  208. try:
  209. message = str(exception.args[0])
  210. except (IndexError, UnicodeDecodeError):
  211. message = '(Could not get exception message)'
  212. return {
  213. 'message': message,
  214. 'source_lines': source_lines[top:bottom],
  215. 'before': before,
  216. 'during': during,
  217. 'after': after,
  218. 'top': top,
  219. 'bottom': bottom,
  220. 'total': total,
  221. 'line': line,
  222. 'name': self.origin.name,
  223. 'start': start,
  224. 'end': end,
  225. }
  226. def linebreak_iter(template_source):
  227. yield 0
  228. p = template_source.find('\n')
  229. while p >= 0:
  230. yield p + 1
  231. p = template_source.find('\n', p + 1)
  232. yield len(template_source) + 1
  233. class Token:
  234. def __init__(self, token_type, contents, position=None, lineno=None):
  235. """
  236. A token representing a string from the template.
  237. token_type
  238. A TokenType, either .TEXT, .VAR, .BLOCK, or .COMMENT.
  239. contents
  240. The token source string.
  241. position
  242. An optional tuple containing the start and end index of the token
  243. in the template source. This is used for traceback information
  244. when debug is on.
  245. lineno
  246. The line number the token appears on in the template source.
  247. This is used for traceback information and gettext files.
  248. """
  249. self.token_type, self.contents = token_type, contents
  250. self.lineno = lineno
  251. self.position = position
  252. def __str__(self):
  253. token_name = self.token_type.name.capitalize()
  254. return ('<%s token: "%s...">' %
  255. (token_name, self.contents[:20].replace('\n', '')))
  256. def split_contents(self):
  257. split = []
  258. bits = smart_split(self.contents)
  259. for bit in bits:
  260. # Handle translation-marked template pieces
  261. if bit.startswith(('_("', "_('")):
  262. sentinel = bit[2] + ')'
  263. trans_bit = [bit]
  264. while not bit.endswith(sentinel):
  265. bit = next(bits)
  266. trans_bit.append(bit)
  267. bit = ' '.join(trans_bit)
  268. split.append(bit)
  269. return split
  270. class Lexer:
  271. def __init__(self, template_string):
  272. self.template_string = template_string
  273. self.verbatim = False
  274. def tokenize(self):
  275. """
  276. Return a list of tokens from a given template_string.
  277. """
  278. in_tag = False
  279. lineno = 1
  280. result = []
  281. for bit in tag_re.split(self.template_string):
  282. if bit:
  283. result.append(self.create_token(bit, None, lineno, in_tag))
  284. in_tag = not in_tag
  285. lineno += bit.count('\n')
  286. return result
  287. def create_token(self, token_string, position, lineno, in_tag):
  288. """
  289. Convert the given token string into a new Token object and return it.
  290. If in_tag is True, we are processing something that matched a tag,
  291. otherwise it should be treated as a literal string.
  292. """
  293. if in_tag and token_string.startswith(BLOCK_TAG_START):
  294. # The [2:-2] ranges below strip off *_TAG_START and *_TAG_END.
  295. # We could do len(BLOCK_TAG_START) to be more "correct", but we've
  296. # hard-coded the 2s here for performance. And it's not like
  297. # the TAG_START values are going to change anytime, anyway.
  298. block_content = token_string[2:-2].strip()
  299. if self.verbatim and block_content == self.verbatim:
  300. self.verbatim = False
  301. if in_tag and not self.verbatim:
  302. if token_string.startswith(VARIABLE_TAG_START):
  303. return Token(TokenType.VAR, token_string[2:-2].strip(), position, lineno)
  304. elif token_string.startswith(BLOCK_TAG_START):
  305. if block_content[:9] in ('verbatim', 'verbatim '):
  306. self.verbatim = 'end%s' % block_content
  307. return Token(TokenType.BLOCK, block_content, position, lineno)
  308. elif token_string.startswith(COMMENT_TAG_START):
  309. content = ''
  310. if token_string.find(TRANSLATOR_COMMENT_MARK):
  311. content = token_string[2:-2].strip()
  312. return Token(TokenType.COMMENT, content, position, lineno)
  313. else:
  314. return Token(TokenType.TEXT, token_string, position, lineno)
  315. class DebugLexer(Lexer):
  316. def tokenize(self):
  317. """
  318. Split a template string into tokens and annotates each token with its
  319. start and end position in the source. This is slower than the default
  320. lexer so only use it when debug is True.
  321. """
  322. lineno = 1
  323. result = []
  324. upto = 0
  325. for match in tag_re.finditer(self.template_string):
  326. start, end = match.span()
  327. if start > upto:
  328. token_string = self.template_string[upto:start]
  329. result.append(self.create_token(token_string, (upto, start), lineno, in_tag=False))
  330. lineno += token_string.count('\n')
  331. upto = start
  332. token_string = self.template_string[start:end]
  333. result.append(self.create_token(token_string, (start, end), lineno, in_tag=True))
  334. lineno += token_string.count('\n')
  335. upto = end
  336. last_bit = self.template_string[upto:]
  337. if last_bit:
  338. result.append(self.create_token(last_bit, (upto, upto + len(last_bit)), lineno, in_tag=False))
  339. return result
  340. class Parser:
  341. def __init__(self, tokens, libraries=None, builtins=None, origin=None):
  342. self.tokens = tokens
  343. self.tags = {}
  344. self.filters = {}
  345. self.command_stack = []
  346. if libraries is None:
  347. libraries = {}
  348. if builtins is None:
  349. builtins = []
  350. self.libraries = libraries
  351. for builtin in builtins:
  352. self.add_library(builtin)
  353. self.origin = origin
  354. def parse(self, parse_until=None):
  355. """
  356. Iterate through the parser tokens and compiles each one into a node.
  357. If parse_until is provided, parsing will stop once one of the
  358. specified tokens has been reached. This is formatted as a list of
  359. tokens, e.g. ['elif', 'else', 'endif']. If no matching token is
  360. reached, raise an exception with the unclosed block tag details.
  361. """
  362. if parse_until is None:
  363. parse_until = []
  364. nodelist = NodeList()
  365. while self.tokens:
  366. token = self.next_token()
  367. # Use the raw values here for TokenType.* for a tiny performance boost.
  368. if token.token_type.value == 0: # TokenType.TEXT
  369. self.extend_nodelist(nodelist, TextNode(token.contents), token)
  370. elif token.token_type.value == 1: # TokenType.VAR
  371. if not token.contents:
  372. raise self.error(token, 'Empty variable tag on line %d' % token.lineno)
  373. try:
  374. filter_expression = self.compile_filter(token.contents)
  375. except TemplateSyntaxError as e:
  376. raise self.error(token, e)
  377. var_node = VariableNode(filter_expression)
  378. self.extend_nodelist(nodelist, var_node, token)
  379. elif token.token_type.value == 2: # TokenType.BLOCK
  380. try:
  381. command = token.contents.split()[0]
  382. except IndexError:
  383. raise self.error(token, 'Empty block tag on line %d' % token.lineno)
  384. if command in parse_until:
  385. # A matching token has been reached. Return control to
  386. # the caller. Put the token back on the token list so the
  387. # caller knows where it terminated.
  388. self.prepend_token(token)
  389. return nodelist
  390. # Add the token to the command stack. This is used for error
  391. # messages if further parsing fails due to an unclosed block
  392. # tag.
  393. self.command_stack.append((command, token))
  394. # Get the tag callback function from the ones registered with
  395. # the parser.
  396. try:
  397. compile_func = self.tags[command]
  398. except KeyError:
  399. self.invalid_block_tag(token, command, parse_until)
  400. # Compile the callback into a node object and add it to
  401. # the node list.
  402. try:
  403. compiled_result = compile_func(self, token)
  404. except Exception as e:
  405. raise self.error(token, e)
  406. self.extend_nodelist(nodelist, compiled_result, token)
  407. # Compile success. Remove the token from the command stack.
  408. self.command_stack.pop()
  409. if parse_until:
  410. self.unclosed_block_tag(parse_until)
  411. return nodelist
  412. def skip_past(self, endtag):
  413. while self.tokens:
  414. token = self.next_token()
  415. if token.token_type == TokenType.BLOCK and token.contents == endtag:
  416. return
  417. self.unclosed_block_tag([endtag])
  418. def extend_nodelist(self, nodelist, node, token):
  419. # Check that non-text nodes don't appear before an extends tag.
  420. if node.must_be_first and nodelist.contains_nontext:
  421. raise self.error(
  422. token, '%r must be the first tag in the template.' % node,
  423. )
  424. if isinstance(nodelist, NodeList) and not isinstance(node, TextNode):
  425. nodelist.contains_nontext = True
  426. # Set origin and token here since we can't modify the node __init__()
  427. # method.
  428. node.token = token
  429. node.origin = self.origin
  430. nodelist.append(node)
  431. def error(self, token, e):
  432. """
  433. Return an exception annotated with the originating token. Since the
  434. parser can be called recursively, check if a token is already set. This
  435. ensures the innermost token is highlighted if an exception occurs,
  436. e.g. a compile error within the body of an if statement.
  437. """
  438. if not isinstance(e, Exception):
  439. e = TemplateSyntaxError(e)
  440. if not hasattr(e, 'token'):
  441. e.token = token
  442. return e
  443. def invalid_block_tag(self, token, command, parse_until=None):
  444. if parse_until:
  445. raise self.error(
  446. token,
  447. "Invalid block tag on line %d: '%s', expected %s. Did you "
  448. "forget to register or load this tag?" % (
  449. token.lineno,
  450. command,
  451. get_text_list(["'%s'" % p for p in parse_until], 'or'),
  452. ),
  453. )
  454. raise self.error(
  455. token,
  456. "Invalid block tag on line %d: '%s'. Did you forget to register "
  457. "or load this tag?" % (token.lineno, command)
  458. )
  459. def unclosed_block_tag(self, parse_until):
  460. command, token = self.command_stack.pop()
  461. msg = "Unclosed tag on line %d: '%s'. Looking for one of: %s." % (
  462. token.lineno,
  463. command,
  464. ', '.join(parse_until),
  465. )
  466. raise self.error(token, msg)
  467. def next_token(self):
  468. return self.tokens.pop(0)
  469. def prepend_token(self, token):
  470. self.tokens.insert(0, token)
  471. def delete_first_token(self):
  472. del self.tokens[0]
  473. def add_library(self, lib):
  474. self.tags.update(lib.tags)
  475. self.filters.update(lib.filters)
  476. def compile_filter(self, token):
  477. """
  478. Convenient wrapper for FilterExpression
  479. """
  480. return FilterExpression(token, self)
  481. def find_filter(self, filter_name):
  482. if filter_name in self.filters:
  483. return self.filters[filter_name]
  484. else:
  485. raise TemplateSyntaxError("Invalid filter: '%s'" % filter_name)
  486. # This only matches constant *strings* (things in quotes or marked for
  487. # translation). Numbers are treated as variables for implementation reasons
  488. # (so that they retain their type when passed to filters).
  489. constant_string = r"""
  490. (?:%(i18n_open)s%(strdq)s%(i18n_close)s|
  491. %(i18n_open)s%(strsq)s%(i18n_close)s|
  492. %(strdq)s|
  493. %(strsq)s)
  494. """ % {
  495. 'strdq': r'"[^"\\]*(?:\\.[^"\\]*)*"', # double-quoted string
  496. 'strsq': r"'[^'\\]*(?:\\.[^'\\]*)*'", # single-quoted string
  497. 'i18n_open': re.escape("_("),
  498. 'i18n_close': re.escape(")"),
  499. }
  500. constant_string = constant_string.replace("\n", "")
  501. filter_raw_string = r"""
  502. ^(?P<constant>%(constant)s)|
  503. ^(?P<var>[%(var_chars)s]+|%(num)s)|
  504. (?:\s*%(filter_sep)s\s*
  505. (?P<filter_name>\w+)
  506. (?:%(arg_sep)s
  507. (?:
  508. (?P<constant_arg>%(constant)s)|
  509. (?P<var_arg>[%(var_chars)s]+|%(num)s)
  510. )
  511. )?
  512. )""" % {
  513. 'constant': constant_string,
  514. 'num': r'[-+\.]?\d[\d\.e]*',
  515. 'var_chars': r'\w\.',
  516. 'filter_sep': re.escape(FILTER_SEPARATOR),
  517. 'arg_sep': re.escape(FILTER_ARGUMENT_SEPARATOR),
  518. }
  519. filter_re = re.compile(filter_raw_string, re.VERBOSE)
  520. class FilterExpression:
  521. """
  522. Parse a variable token and its optional filters (all as a single string),
  523. and return a list of tuples of the filter name and arguments.
  524. Sample::
  525. >>> token = 'variable|default:"Default value"|date:"Y-m-d"'
  526. >>> p = Parser('')
  527. >>> fe = FilterExpression(token, p)
  528. >>> len(fe.filters)
  529. 2
  530. >>> fe.var
  531. <Variable: 'variable'>
  532. """
  533. def __init__(self, token, parser):
  534. self.token = token
  535. matches = filter_re.finditer(token)
  536. var_obj = None
  537. filters = []
  538. upto = 0
  539. for match in matches:
  540. start = match.start()
  541. if upto != start:
  542. raise TemplateSyntaxError("Could not parse some characters: "
  543. "%s|%s|%s" %
  544. (token[:upto], token[upto:start],
  545. token[start:]))
  546. if var_obj is None:
  547. var, constant = match.group("var", "constant")
  548. if constant:
  549. try:
  550. var_obj = Variable(constant).resolve({})
  551. except VariableDoesNotExist:
  552. var_obj = None
  553. elif var is None:
  554. raise TemplateSyntaxError("Could not find variable at "
  555. "start of %s." % token)
  556. else:
  557. var_obj = Variable(var)
  558. else:
  559. filter_name = match.group("filter_name")
  560. args = []
  561. constant_arg, var_arg = match.group("constant_arg", "var_arg")
  562. if constant_arg:
  563. args.append((False, Variable(constant_arg).resolve({})))
  564. elif var_arg:
  565. args.append((True, Variable(var_arg)))
  566. filter_func = parser.find_filter(filter_name)
  567. self.args_check(filter_name, filter_func, args)
  568. filters.append((filter_func, args))
  569. upto = match.end()
  570. if upto != len(token):
  571. raise TemplateSyntaxError("Could not parse the remainder: '%s' "
  572. "from '%s'" % (token[upto:], token))
  573. self.filters = filters
  574. self.var = var_obj
  575. def resolve(self, context, ignore_failures=False):
  576. if isinstance(self.var, Variable):
  577. try:
  578. obj = self.var.resolve(context)
  579. except VariableDoesNotExist:
  580. if ignore_failures:
  581. obj = None
  582. else:
  583. string_if_invalid = context.template.engine.string_if_invalid
  584. if string_if_invalid:
  585. if '%s' in string_if_invalid:
  586. return string_if_invalid % self.var
  587. else:
  588. return string_if_invalid
  589. else:
  590. obj = string_if_invalid
  591. else:
  592. obj = self.var
  593. for func, args in self.filters:
  594. arg_vals = []
  595. for lookup, arg in args:
  596. if not lookup:
  597. arg_vals.append(mark_safe(arg))
  598. else:
  599. arg_vals.append(arg.resolve(context))
  600. if getattr(func, 'expects_localtime', False):
  601. obj = template_localtime(obj, context.use_tz)
  602. if getattr(func, 'needs_autoescape', False):
  603. new_obj = func(obj, autoescape=context.autoescape, *arg_vals)
  604. else:
  605. new_obj = func(obj, *arg_vals)
  606. if getattr(func, 'is_safe', False) and isinstance(obj, SafeData):
  607. obj = mark_safe(new_obj)
  608. else:
  609. obj = new_obj
  610. return obj
  611. def args_check(name, func, provided):
  612. provided = list(provided)
  613. # First argument, filter input, is implied.
  614. plen = len(provided) + 1
  615. # Check to see if a decorator is providing the real function.
  616. func = unwrap(func)
  617. args, _, _, defaults, _, _, _ = getfullargspec(func)
  618. alen = len(args)
  619. dlen = len(defaults or [])
  620. # Not enough OR Too many
  621. if plen < (alen - dlen) or plen > alen:
  622. raise TemplateSyntaxError("%s requires %d arguments, %d provided" %
  623. (name, alen - dlen, plen))
  624. return True
  625. args_check = staticmethod(args_check)
  626. def __str__(self):
  627. return self.token
  628. class Variable:
  629. """
  630. A template variable, resolvable against a given context. The variable may
  631. be a hard-coded string (if it begins and ends with single or double quote
  632. marks)::
  633. >>> c = {'article': {'section':'News'}}
  634. >>> Variable('article.section').resolve(c)
  635. 'News'
  636. >>> Variable('article').resolve(c)
  637. {'section': 'News'}
  638. >>> class AClass: pass
  639. >>> c = AClass()
  640. >>> c.article = AClass()
  641. >>> c.article.section = 'News'
  642. (The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.')
  643. """
  644. def __init__(self, var):
  645. self.var = var
  646. self.literal = None
  647. self.lookups = None
  648. self.translate = False
  649. self.message_context = None
  650. if not isinstance(var, str):
  651. raise TypeError(
  652. "Variable must be a string or number, got %s" % type(var))
  653. try:
  654. # First try to treat this variable as a number.
  655. #
  656. # Note that this could cause an OverflowError here that we're not
  657. # catching. Since this should only happen at compile time, that's
  658. # probably OK.
  659. # Try to interpret values containing a period or an 'e'/'E'
  660. # (possibly scientific notation) as a float; otherwise, try int.
  661. if '.' in var or 'e' in var.lower():
  662. self.literal = float(var)
  663. # "2." is invalid
  664. if var.endswith('.'):
  665. raise ValueError
  666. else:
  667. self.literal = int(var)
  668. except ValueError:
  669. # A ValueError means that the variable isn't a number.
  670. if var.startswith('_(') and var.endswith(')'):
  671. # The result of the lookup should be translated at rendering
  672. # time.
  673. self.translate = True
  674. var = var[2:-1]
  675. # If it's wrapped with quotes (single or double), then
  676. # we're also dealing with a literal.
  677. try:
  678. self.literal = mark_safe(unescape_string_literal(var))
  679. except ValueError:
  680. # Otherwise we'll set self.lookups so that resolve() knows we're
  681. # dealing with a bonafide variable
  682. if var.find(VARIABLE_ATTRIBUTE_SEPARATOR + '_') > -1 or var[0] == '_':
  683. raise TemplateSyntaxError("Variables and attributes may "
  684. "not begin with underscores: '%s'" %
  685. var)
  686. self.lookups = tuple(var.split(VARIABLE_ATTRIBUTE_SEPARATOR))
  687. def resolve(self, context):
  688. """Resolve this variable against a given context."""
  689. if self.lookups is not None:
  690. # We're dealing with a variable that needs to be resolved
  691. value = self._resolve_lookup(context)
  692. else:
  693. # We're dealing with a literal, so it's already been "resolved"
  694. value = self.literal
  695. if self.translate:
  696. is_safe = isinstance(value, SafeData)
  697. msgid = value.replace('%', '%%')
  698. msgid = mark_safe(msgid) if is_safe else msgid
  699. if self.message_context:
  700. return pgettext_lazy(self.message_context, msgid)
  701. else:
  702. return gettext_lazy(msgid)
  703. return value
  704. def __repr__(self):
  705. return "<%s: %r>" % (self.__class__.__name__, self.var)
  706. def __str__(self):
  707. return self.var
  708. def _resolve_lookup(self, context):
  709. """
  710. Perform resolution of a real variable (i.e. not a literal) against the
  711. given context.
  712. As indicated by the method's name, this method is an implementation
  713. detail and shouldn't be called by external code. Use Variable.resolve()
  714. instead.
  715. """
  716. current = context
  717. try: # catch-all for silent variable failures
  718. for bit in self.lookups:
  719. try: # dictionary lookup
  720. current = current[bit]
  721. # ValueError/IndexError are for numpy.array lookup on
  722. # numpy < 1.9 and 1.9+ respectively
  723. except (TypeError, AttributeError, KeyError, ValueError, IndexError):
  724. try: # attribute lookup
  725. # Don't return class attributes if the class is the context:
  726. if isinstance(current, BaseContext) and getattr(type(current), bit):
  727. raise AttributeError
  728. current = getattr(current, bit)
  729. except (TypeError, AttributeError):
  730. # Reraise if the exception was raised by a @property
  731. if not isinstance(current, BaseContext) and bit in dir(current):
  732. raise
  733. try: # list-index lookup
  734. current = current[int(bit)]
  735. except (IndexError, # list index out of range
  736. ValueError, # invalid literal for int()
  737. KeyError, # current is a dict without `int(bit)` key
  738. TypeError): # unsubscriptable object
  739. raise VariableDoesNotExist("Failed lookup for key "
  740. "[%s] in %r",
  741. (bit, current)) # missing attribute
  742. if callable(current):
  743. if getattr(current, 'do_not_call_in_templates', False):
  744. pass
  745. elif getattr(current, 'alters_data', False):
  746. current = context.template.engine.string_if_invalid
  747. else:
  748. try: # method call (assuming no args required)
  749. current = current()
  750. except TypeError:
  751. try:
  752. getcallargs(current)
  753. except TypeError: # arguments *were* required
  754. current = context.template.engine.string_if_invalid # invalid method call
  755. else:
  756. raise
  757. except Exception as e:
  758. template_name = getattr(context, 'template_name', None) or 'unknown'
  759. logger.debug(
  760. "Exception while resolving variable '%s' in template '%s'.",
  761. bit,
  762. template_name,
  763. exc_info=True,
  764. )
  765. if getattr(e, 'silent_variable_failure', False):
  766. current = context.template.engine.string_if_invalid
  767. else:
  768. raise
  769. return current
  770. class Node:
  771. # Set this to True for nodes that must be first in the template (although
  772. # they can be preceded by text nodes.
  773. must_be_first = False
  774. child_nodelists = ('nodelist',)
  775. token = None
  776. def render(self, context):
  777. """
  778. Return the node rendered as a string.
  779. """
  780. pass
  781. def render_annotated(self, context):
  782. """
  783. Render the node. If debug is True and an exception occurs during
  784. rendering, the exception is annotated with contextual line information
  785. where it occurred in the template. For internal usage this method is
  786. preferred over using the render method directly.
  787. """
  788. try:
  789. return self.render(context)
  790. except Exception as e:
  791. if context.template.engine.debug and not hasattr(e, 'template_debug'):
  792. e.template_debug = context.render_context.template.get_exception_info(e, self.token)
  793. raise
  794. def __iter__(self):
  795. yield self
  796. def get_nodes_by_type(self, nodetype):
  797. """
  798. Return a list of all nodes (within this node and its nodelist)
  799. of the given type
  800. """
  801. nodes = []
  802. if isinstance(self, nodetype):
  803. nodes.append(self)
  804. for attr in self.child_nodelists:
  805. nodelist = getattr(self, attr, None)
  806. if nodelist:
  807. nodes.extend(nodelist.get_nodes_by_type(nodetype))
  808. return nodes
  809. class NodeList(list):
  810. # Set to True the first time a non-TextNode is inserted by
  811. # extend_nodelist().
  812. contains_nontext = False
  813. def render(self, context):
  814. bits = []
  815. for node in self:
  816. if isinstance(node, Node):
  817. bit = node.render_annotated(context)
  818. else:
  819. bit = node
  820. bits.append(str(bit))
  821. return mark_safe(''.join(bits))
  822. def get_nodes_by_type(self, nodetype):
  823. "Return a list of all nodes of the given type"
  824. nodes = []
  825. for node in self:
  826. nodes.extend(node.get_nodes_by_type(nodetype))
  827. return nodes
  828. class TextNode(Node):
  829. def __init__(self, s):
  830. self.s = s
  831. def __repr__(self):
  832. return "<%s: %r>" % (self.__class__.__name__, self.s[:25])
  833. def render(self, context):
  834. return self.s
  835. def render_value_in_context(value, context):
  836. """
  837. Convert any value to a string to become part of a rendered template. This
  838. means escaping, if required, and conversion to a string. If value is a
  839. string, it's expected to already be translated.
  840. """
  841. value = template_localtime(value, use_tz=context.use_tz)
  842. value = localize(value, use_l10n=context.use_l10n)
  843. if context.autoescape:
  844. if not issubclass(type(value), str):
  845. value = str(value)
  846. return conditional_escape(value)
  847. else:
  848. return str(value)
  849. class VariableNode(Node):
  850. def __init__(self, filter_expression):
  851. self.filter_expression = filter_expression
  852. def __repr__(self):
  853. return "<Variable Node: %s>" % self.filter_expression
  854. def render(self, context):
  855. try:
  856. output = self.filter_expression.resolve(context)
  857. except UnicodeDecodeError:
  858. # Unicode conversion can fail sometimes for reasons out of our
  859. # control (e.g. exception rendering). In that case, we fail
  860. # quietly.
  861. return ''
  862. return render_value_in_context(output, context)
  863. # Regex for token keyword arguments
  864. kwarg_re = re.compile(r"(?:(\w+)=)?(.+)")
  865. def token_kwargs(bits, parser, support_legacy=False):
  866. """
  867. Parse token keyword arguments and return a dictionary of the arguments
  868. retrieved from the ``bits`` token list.
  869. `bits` is a list containing the remainder of the token (split by spaces)
  870. that is to be checked for arguments. Valid arguments are removed from this
  871. list.
  872. `support_legacy` - if True, the legacy format ``1 as foo`` is accepted.
  873. Otherwise, only the standard ``foo=1`` format is allowed.
  874. There is no requirement for all remaining token ``bits`` to be keyword
  875. arguments, so return the dictionary as soon as an invalid argument format
  876. is reached.
  877. """
  878. if not bits:
  879. return {}
  880. match = kwarg_re.match(bits[0])
  881. kwarg_format = match and match.group(1)
  882. if not kwarg_format:
  883. if not support_legacy:
  884. return {}
  885. if len(bits) < 3 or bits[1] != 'as':
  886. return {}
  887. kwargs = {}
  888. while bits:
  889. if kwarg_format:
  890. match = kwarg_re.match(bits[0])
  891. if not match or not match.group(1):
  892. return kwargs
  893. key, value = match.groups()
  894. del bits[:1]
  895. else:
  896. if len(bits) < 3 or bits[1] != 'as':
  897. return kwargs
  898. key, value = bits[2], bits[0]
  899. del bits[:3]
  900. kwargs[key] = parser.compile_filter(value)
  901. if bits and not kwarg_format:
  902. if bits[0] != 'and':
  903. return kwargs
  904. del bits[:1]
  905. return kwargs