1 # -*- coding: utf-8 -*-
6 Provides a class that holds runtime and parsing time options.
8 :copyright: 2006 by Armin Ronacher.
9 :license: BSD, see LICENSE for more details.
11 from jinja.lexer import Lexer
12 from jinja.parser import Parser
13 from jinja.datastructure import LoopContext, Undefined
14 from jinja.exceptions import FilterNotFound
15 from jinja.defaults import DEFAULT_FILTERS
18 class Environment(object):
20 The jinja environment.
24 block_start_string='{%',
25 block_end_string='%}',
26 variable_start_string='{{',
27 variable_end_string='}}',
28 comment_start_string='{#',
29 comment_end_string='#}',
30 template_charset='utf-8',
35 # lexer / parser information
36 self.block_start_string = block_start_string
37 self.block_end_string = block_end_string
38 self.variable_start_string = variable_start_string
39 self.variable_end_string = variable_end_string
40 self.comment_start_string = comment_start_string
41 self.comment_end_string = comment_end_string
44 self.template_charset = template_charset
45 self.charset = charset
47 self.filters = filters or DEFAULT_FILTERS.copy()
50 self.lexer = Lexer(self)
52 def parse(self, source):
53 """Function that creates a new parser and parses the source."""
54 parser = Parser(self, source)
55 return parser.parse_page()
57 def to_unicode(self, value):
59 Convert a value to unicode with the rules defined on the environment.
61 if isinstance(value, unicode):
67 return str(value).decode(self.charset, 'ignore')
69 def iterate(self, seq):
71 Helper function used by the python translator runtime code to
72 iterate over a sequence.
79 loop_data = LoopContext(0, length)
84 def prepare_filter(self, name, *args):
89 return self.filters[name](*args)
91 raise FilterNotFound(name)
93 def apply_filters(self, value, context, filters):
95 Apply a list of filters on the variable.
98 value = f(self, context, value)
101 def get_attribute(self, obj, name):
103 Get the attribute name from obj.
106 return getattr(obj, name)
107 except AttributeError: