Class: Liquid::Variable
- Inherits:
-
Object
- Object
- Liquid::Variable
- Includes:
- ParserSwitching
- Defined in:
- lib/liquid/variable.rb
Overview
Holds variables. Variables are only loaded “just in time” and are not evaluated as part of the render stage
{{ monkey }}
{{ user.name }}
Variables can be combined with filters:
{{ user | link }}
Constant Summary collapse
- FilterParser =
/(?:\s+|#{QuotedFragment}|#{ArgumentSeparator})+/o
- EasyParse =
/\A *(\w+(?:\.\w+)*) *\z/
Instance Attribute Summary collapse
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#line_number ⇒ Object
Returns the value of attribute line_number.
-
#name ⇒ Object
Returns the value of attribute name.
-
#warnings ⇒ Object
Returns the value of attribute warnings.
Instance Method Summary collapse
-
#initialize(markup, options = {}) ⇒ Variable
constructor
A new instance of Variable.
- #lax_parse(markup) ⇒ Object
- #markup_context(markup) ⇒ Object
- #parse_filterargs(p) ⇒ Object
- #raw ⇒ Object
- #render(context) ⇒ Object
- #strict_parse(markup) ⇒ Object
Methods included from ParserSwitching
Constructor Details
#initialize(markup, options = {}) ⇒ Variable
Returns a new instance of Variable.
20 21 22 23 24 25 26 |
# File 'lib/liquid/variable.rb', line 20 def initialize(markup, = {}) @markup = markup @name = nil @options = || {} parse_with_selected_parser(markup) end |
Instance Attribute Details
#filters ⇒ Object
Returns the value of attribute filters.
16 17 18 |
# File 'lib/liquid/variable.rb', line 16 def filters @filters end |
#line_number ⇒ Object
Returns the value of attribute line_number.
17 18 19 |
# File 'lib/liquid/variable.rb', line 17 def line_number @line_number end |
#name ⇒ Object
Returns the value of attribute name.
16 17 18 |
# File 'lib/liquid/variable.rb', line 16 def name @name end |
#warnings ⇒ Object
Returns the value of attribute warnings.
16 17 18 |
# File 'lib/liquid/variable.rb', line 16 def warnings @warnings end |
Instance Method Details
#lax_parse(markup) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/liquid/variable.rb', line 36 def lax_parse(markup) @filters = [] if markup =~ /(#{QuotedFragment})(.*)/om name_markup = $1 filter_markup = $2 @name = Expression.parse(name_markup) if filter_markup =~ /#{FilterSeparator}\s*(.*)/om filters = $1.scan(FilterParser) filters.each do |f| if f =~ /\w+/ filtername = Regexp.last_match(0) filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o).flatten @filters << parse_filter_expressions(filtername, filterargs) end end end end end |
#markup_context(markup) ⇒ Object
32 33 34 |
# File 'lib/liquid/variable.rb', line 32 def markup_context(markup) "in \"{{#{markup}}}\"" end |
#parse_filterargs(p) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/liquid/variable.rb', line 75 def parse_filterargs(p) # first argument filterargs = [p.argument] # followed by comma separated others while p.consume?(:comma) filterargs << p.argument end filterargs end |
#raw ⇒ Object
28 29 30 |
# File 'lib/liquid/variable.rb', line 28 def raw @markup end |
#render(context) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/liquid/variable.rb', line 85 def render(context) @filters.inject(context.evaluate(@name)) do |output, (filter_name, filter_args, filter_kwargs)| filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs) output = context.invoke(filter_name, output, *filter_args) end.tap{ |obj| taint_check(obj) } end |
#strict_parse(markup) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/liquid/variable.rb', line 55 def strict_parse(markup) # Very simple valid cases if markup =~ EasyParse @name = Expression.parse($1) @filters = [] return end @filters = [] p = Parser.new(markup) # Could be just filters with no input @name = p.look(:pipe) ? nil : Expression.parse(p.expression) while p.consume?(:pipe) filtername = p.consume(:id) filterargs = p.consume?(:colon) ? parse_filterargs(p) : [] @filters << parse_filter_expressions(filtername, filterargs) end p.consume(:end_of_string) end |