Class: Liquid::Rails::ContentForTag

Inherits:
Block
  • Object
show all
Defined in:
lib/liquid-rails/tags/content_for_tag.rb

Constant Summary collapse

Syntax =
/(#{::Liquid::QuotedFragment}+)\s*(flush\s*(true|false))?/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, context) ⇒ ContentForTag

Returns a new instance of ContentForTag.



21
22
23
24
25
26
27
28
29
30
# File 'lib/liquid-rails/tags/content_for_tag.rb', line 21

def initialize(tag_name, markup, context)
  super

  if markup =~ Syntax
    @flush = $3
    @identifier = $1.gsub('\'', '')
  else
    raise SyntaxError.new("Syntax Error - Valid syntax: {% content_for [name] %}")
  end
end

Instance Method Details

#render(context) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/liquid-rails/tags/content_for_tag.rb', line 32

def render(context)
  @context = context
  content  = super.html_safe

  if ::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR == 2
    if @flush == 'true'
      @context.registers[:view].view_flow.set(@identifier, content) if content
    else
      @context.registers[:view].view_flow.append(@identifier, content) if content
    end
  else
    options = @flush == 'true' ? { flush: true } : {}
    @context.registers[:view].content_for(@identifier, content, options)
  end
  ''
end