tobi / liquid

Liquid markup language. Save, customer facing template language for flexible web apps.

This URL has Read+Write access

liquid / lib / liquid / tags / capture.rb
100644 36 lines (30 sloc) 0.746 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module Liquid
 
  # Capture stores the result of a block into a variable without rendering it inplace.
  #
  # {% capture heading %}
  # Monkeys!
  # {% endcapture %}
  # ...
  # <h1>{{ monkeys }}</h1>
  #
  # Capture is useful for saving content for use later in your template, such as
  # in a sidebar or footer.
  #
  class Capture < Block
    Syntax = /(\w+)/
 
    def initialize(tag_name, markup, tokens)
      if markup =~ Syntax
        @to = $1
      else
        raise SyntaxError.new("Syntax Error in 'capture' - Valid syntax: capture [var]")
      end
 
      super
    end
 
    def render(context)
      output = super
      context[@to] = output.join
      ''
    end
  end
 
  Template.register_tag('capture', Capture)
end