implement new liquid tag: fetch_page (#209)
did
committed Aug 13, 2014
commit f9687ccec02474c7e7204b494ce686d721e801f7
Showing 3
changed files with
51 additions
and 0 deletions
locomotive/wagon/liquid/tags/fetch_page.rb b/lib/locomotive/wagon/liquid/tags/fetch_page.rb
+41
-0
| @@ | @@ -0,0 +1,41 @@ |
| + | module Locomotive |
| + | module Wagon |
| + | module Liquid |
| + | module Tags |
| + | |
| + | # 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> |
| + | # |
| + | class FetchPage < ::Liquid::Tag |
| + | |
| + | Syntax = /(#{::Liquid::VariableSignature}+)\s+as\s+(#{::Liquid::VariableSignature}+)/ |
| + | |
| + | 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 |
| + | |
| + | def render(context) |
| + | mounting_point = context.registers[:mounting_point] |
| + | context.scopes.last[@var] = mounting_point.pages.values.find { |_page| _page.handle == @handle } |
| + | '' |
| + | end |
| + | |
| + | end |
| + | |
| + | ::Liquid::Template.register_tag('fetch_page', FetchPage) |
| + | end |
| + | |
| + | end |
| + | end |
| + | end |
| \ No newline at end of file | |
spec/fixtures/default/app/views/pages/all.liquid.haml
+3
-0
| @@ | @@ -10,4 +10,7 @@ published: false |
| <li>{{ page.title }}</li> | |
| {% endfor %} | |
| </ul> | |
| + | |
| + | {% fetch_page our-music as my_page %} |
| + | <p>Single page: {{ my_page.title }}</p> |
| {% endblock %} | |
spec/integration/server/liquid_spec.rb
+7
-0
| @@ | @@ -88,4 +88,11 @@ describe Locomotive::Wagon::Server do |
| end | |
| end | |
| + | describe 'fetch_page' do |
| + | it 'returns the title of a page' do |
| + | get '/all' |
| + | last_response.body.should =~ %r(<p>Single page: Music</p>) |
| + | end |
| + | end |
| + | |
| end | |
| \ No newline at end of file | |