Class: Twig::TokenParser::If
Overview
Marks a section of a template as being reusable.
if condition % Something else % Something else endif %
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Instance Method Details
#parse(token) ⇒ Object
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 |
# File 'lib/twig/token_parser/if.rb', line 13 def parse(token) lineno = token.lineno expr = parser.parse_expression stream = parser.stream stream.expect(Token::BLOCK_END_TYPE) body = parser.subparse(method(:decide_if_fork)) tests = [expr, body] else_node = nil if_ended = false until if_ended case stream.next.value when 'else' stream.expect(Token::BLOCK_END_TYPE) else_node = parser.subparse(method(:decide_if_end)) when 'elsif', 'elseif' expr = parser.parse_expression stream.expect(Token::BLOCK_END_TYPE) body = parser.subparse(method(:decide_if_fork)) tests.push(expr, body) when 'endif' if_ended = true else raise 'Unexpected end of template, expect else, elseif, or endif' end end stream.expect(Token::BLOCK_END_TYPE) tests_hash = tests. each_with_index. to_h { |t, i| [i, t] } Node::If.new(Node::Nodes.new(tests_hash), else_node, lineno) end |
#tag ⇒ Object
49 50 51 |
# File 'lib/twig/token_parser/if.rb', line 49 def tag 'if' end |