__init__.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """
  2. This is the Django template system.
  3. How it works:
  4. The Lexer.tokenize() function converts a template string (i.e., a string containing
  5. markup with custom template tags) to tokens, which can be either plain text
  6. (TOKEN_TEXT), variables (TOKEN_VAR) or block statements (TOKEN_BLOCK).
  7. The Parser() class takes a list of tokens in its constructor, and its parse()
  8. method returns a compiled template -- which is, under the hood, a list of
  9. Node objects.
  10. Each Node is responsible for creating some sort of output -- e.g. simple text
  11. (TextNode), variable values in a given context (VariableNode), results of basic
  12. logic (IfNode), results of looping (ForNode), or anything else. The core Node
  13. types are TextNode, VariableNode, IfNode and ForNode, but plugin modules can
  14. define their own custom node types.
  15. Each Node has a render() method, which takes a Context and returns a string of
  16. the rendered node. For example, the render() method of a Variable Node returns
  17. the variable's value as a string. The render() method of an IfNode returns the
  18. rendered output of whatever was inside the loop, recursively.
  19. The Template class is a convenient wrapper that takes care of template
  20. compilation and rendering.
  21. Usage:
  22. The only thing you should ever use directly in this file is the Template class.
  23. Create a compiled template object with a template_string, then call render()
  24. with a context. In the compilation stage, the TemplateSyntaxError exception
  25. will be raised if the template doesn't have proper syntax.
  26. Sample code:
  27. >>> from django import template
  28. >>> s = u'<html>{% if test %}<h1>{{ varvalue }}</h1>{% endif %}</html>'
  29. >>> t = template.Template(s)
  30. (t is now a compiled template, and its render() method can be called multiple
  31. times with multiple contexts)
  32. >>> c = template.Context({'test':True, 'varvalue': 'Hello'})
  33. >>> t.render(c)
  34. u'<html><h1>Hello</h1></html>'
  35. >>> c = template.Context({'test':False, 'varvalue': 'Hello'})
  36. >>> t.render(c)
  37. u'<html></html>'
  38. """
  39. # Template lexing symbols
  40. from django.template.base import (ALLOWED_VARIABLE_CHARS, BLOCK_TAG_END, # NOQA
  41. BLOCK_TAG_START, COMMENT_TAG_END, COMMENT_TAG_START,
  42. FILTER_ARGUMENT_SEPARATOR, FILTER_SEPARATOR, SINGLE_BRACE_END,
  43. SINGLE_BRACE_START, TOKEN_BLOCK, TOKEN_COMMENT, TOKEN_TEXT, TOKEN_VAR,
  44. TRANSLATOR_COMMENT_MARK, UNKNOWN_SOURCE, VARIABLE_ATTRIBUTE_SEPARATOR,
  45. VARIABLE_TAG_END, VARIABLE_TAG_START, filter_re, tag_re)
  46. # Exceptions
  47. from django.template.base import (ContextPopException, InvalidTemplateLibrary, # NOQA
  48. TemplateDoesNotExist, TemplateEncodingError, TemplateSyntaxError,
  49. VariableDoesNotExist)
  50. # Template parts
  51. from django.template.base import (Context, FilterExpression, Lexer, Node, # NOQA
  52. NodeList, Parser, RequestContext, Origin, StringOrigin, Template,
  53. TextNode, Token, TokenParser, Variable, VariableNode, constant_string,
  54. filter_raw_string)
  55. # Compiling templates
  56. from django.template.base import (compile_string, resolve_variable, # NOQA
  57. unescape_string_literal, generic_tag_compiler)
  58. # Library management
  59. from django.template.base import (Library, add_to_builtins, builtins, # NOQA
  60. get_library, get_templatetags_modules, get_text_list, import_library,
  61. libraries)
  62. __all__ = ('Template', 'Context', 'RequestContext', 'compile_string')