Class: Locomotive::Liquid::Tags::FetchPage

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/locomotive/liquid/tags/fetch_page.rb

Overview

Fetch a page from its handle and assign it to a liquid variable.

Usage:

fetch_page ‘about_us’ as a_page % <p>a_page.title }</p>

Constant Summary collapse

Syntax =
/(#{::Liquid::VariableSignature}+)\s+as\s+(#{::Liquid::VariableSignature}+)/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens, context) ⇒ FetchPage

Returns a new instance of FetchPage.



16
17
18
19
20
21
22
23
24
25
# File 'lib/locomotive/liquid/tags/fetch_page.rb', line 16

def initialize(tag_name, markup, tokens, context)
  if markup =~ Syntax
    @handle = $1
    @var = $2
  else
    raise SyntaxError.new("Syntax Error in 'fetch_page' - Valid syntax: fetch_page page_handle as variable")
  end

  super
end

Instance Method Details

#render(context) ⇒ Object



27
28
29
30
# File 'lib/locomotive/liquid/tags/fetch_page.rb', line 27

def render(context)
  context.scopes.last[@var] = context.registers[:site].pages.where(handle: @handle).first
  ''
end