/
set.cr
50 lines (46 loc) · 1.72 KB
/
set.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Inside code blocks, you can also assign values to variables. Assignments at top level (outside of
# blocks, macros or loops) are exported from the template like top level macros and can be imported
# by other templates.
#
# Assignments use the set tag and can have multiple targets:
#
# ```
# {% set navigation = [('index.html', 'Index'), ('about.html', 'About')] %}
# {% set key, value = call_something() %}
# ```
#
# It is also possible to use block assignments to capture the contents of a `set` block into a
# variable name. Instead of using an equals sign and a value, you just write the variable name and
# then everything until `{% endset %}` is captured.
#
# {% set navigation %}
# <li><a href="/">Index</a>
# <li><a href="/downloads">Downloads</a>
# {% endset %}
# ```
#
# See [Jinja2 Template Documentation](http://jinja.pocoo.org/docs/2.9/templates/#assignments) for details.
class Crinja::Tag::Set < Crinja::Tag
name "set", "endset"
private def interpret(io : IO, renderer : Crinja::Renderer, tag_node : TagNode)
env = renderer.env
args = ArgumentsParser.new(tag_node.arguments, renderer.env.config)
if tag_node.arguments.size == 2
# IDENTIFIER + EOF
name = args.current_token.value
args.next_token
value = renderer.render(tag_node.block).value
env.context[name] = SafeString.new(value)
args.close
else
args.parse_keyword_list.each do |identifier, expr|
env.context[identifier.name] = env.evaluate(expr)
end
# raise TemplateSyntaxError.new(tag_node, "Tag `set` requires either a single name argument (set block) or at least one assignment", exc)
args.close
end
end
def has_block?(node : TagNode)
node.arguments.size <= 2
end
end