Class: Liquid::Include
Overview
Include allows templates to relate with other templates
Simply include another template:
{% include 'product' %}
Include a template with a local variable:
{% include 'product' with products[0] %}
Include a template for a collection:
{% include 'product' for products %}
Constant Summary collapse
- Syntax =
/(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/o
Instance Attribute Summary
Attributes inherited from Tag
#line, #nodelist, #options, #warnings
Instance Method Summary collapse
- #blank? ⇒ Boolean
-
#initialize(tag_name, markup, tokens, options) ⇒ Include
constructor
A new instance of Include.
- #parse(tokens) ⇒ Object
- #render(context) ⇒ Object
Methods inherited from Tag
#name, new_with_options, #parse_with_selected_parser
Constructor Details
#initialize(tag_name, markup, tokens, options) ⇒ Include
Returns a new instance of Include.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/liquid/tags/include.rb', line 20 def initialize(tag_name, markup, tokens, ) if markup =~ Syntax @template_name = $1 @variable_name = $3 @attributes = {} markup.scan(TagAttributes) do |key, value| @attributes[key] = value end else raise SyntaxError.new([:locale].t("errors.syntax.include"), [:line]) end super end |
Instance Method Details
#blank? ⇒ Boolean
41 42 43 |
# File 'lib/liquid/tags/include.rb', line 41 def blank? false end |
#parse(tokens) ⇒ Object
38 39 |
# File 'lib/liquid/tags/include.rb', line 38 def parse(tokens) end |
#render(context) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/liquid/tags/include.rb', line 45 def render(context) partial = load_cached_partial(context) variable = context[@variable_name || @template_name[1..-2]] context.stack do @attributes.each do |key, value| context[key] = context[value] end if variable.is_a?(Array) variable.collect do |var| context[@template_name[1..-2]] = var partial.render(context) end else context[@template_name[1..-2]] = variable partial.render(context) end end end |