Class: Liquid::Case
Defined Under Namespace
Classes: ParseTreeVisitor
Constant Summary collapse
- Syntax =
/(#{QuotedFragment})/o
- WhenSyntax =
/(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/om
Constants inherited from Block
Instance Attribute Summary collapse
-
#blocks ⇒ Object
readonly
Returns the value of attribute blocks.
-
#left ⇒ Object
readonly
Returns the value of attribute left.
Attributes inherited from Tag
#line_number, #parse_context, #tag_name
Instance Method Summary collapse
-
#initialize(tag_name, markup, options) ⇒ Case
constructor
A new instance of Case.
- #nodelist ⇒ Object
- #parse(tokens) ⇒ Object
- #render_to_output_buffer(context, output) ⇒ Object
- #unknown_tag(tag, markup, tokens) ⇒ Object
Methods inherited from Block
#blank?, #block_delimiter, #block_name, #raise_tag_never_closed, raise_unknown_tag, #render
Methods inherited from Tag
#blank?, disable_tags, #name, parse, #raw, #render
Methods included from ParserSwitching
#parse_with_selected_parser, #strict_parse_with_error_mode_fallback
Constructor Details
#initialize(tag_name, markup, options) ⇒ Case
Returns a new instance of Case.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/liquid/tags/case.rb', line 31 def initialize(tag_name, markup, ) super @blocks = [] if markup =~ Syntax @left = parse_expression(Regexp.last_match(1)) else raise SyntaxError, [:locale].t("errors.syntax.case") end end |
Instance Attribute Details
#blocks ⇒ Object (readonly)
Returns the value of attribute blocks.
29 30 31 |
# File 'lib/liquid/tags/case.rb', line 29 def blocks @blocks end |
#left ⇒ Object (readonly)
Returns the value of attribute left.
29 30 31 |
# File 'lib/liquid/tags/case.rb', line 29 def left @left end |
Instance Method Details
#nodelist ⇒ Object
55 56 57 |
# File 'lib/liquid/tags/case.rb', line 55 def nodelist @blocks.map(&:attachment) end |
#parse(tokens) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/liquid/tags/case.rb', line 42 def parse(tokens) body = case_body = new_body body = @blocks.last. while parse_body(body, tokens) @blocks.reverse_each do |condition| body = condition. unless body.frozen? body.remove_blank_strings if blank? body.freeze end end case_body.freeze end |
#render_to_output_buffer(context, output) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/liquid/tags/case.rb', line 70 def render_to_output_buffer(context, output) execute_else_block = true @blocks.each do |block| if block.else? block..render_to_output_buffer(context, output) if execute_else_block next end result = Liquid::Utils.to_liquid_value( block.evaluate(context), ) if result execute_else_block = false block..render_to_output_buffer(context, output) end end output end |
#unknown_tag(tag, markup, tokens) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/liquid/tags/case.rb', line 59 def unknown_tag(tag, markup, tokens) case tag when 'when' record_when_condition(markup) when 'else' record_else_condition(markup) else super end end |