access children of a page in liquid templates (useful for building nav widgets)

This commit is contained in:
dinedine 2010-06-21 17:46:17 +02:00
parent b7e1cd0926
commit 074326095d
6 changed files with 75 additions and 4 deletions

View File

@ -35,7 +35,7 @@ class Page
named_scope :not_found, :where => { :slug => '404', :depth => 0, :published => true } named_scope :not_found, :where => { :slug => '404', :depth => 0, :published => true }
## behaviours ## ## behaviours ##
liquid_methods :title, :fullpath # liquid_methods :title, :fullpath
liquify_template :joined_parts liquify_template :joined_parts
## methods ## ## methods ##
@ -65,6 +65,10 @@ class Page
def url def url
"http://#{self.site.domains.first}/#{self.fullpath}.html" "http://#{self.site.domains.first}/#{self.fullpath}.html"
end end
def to_liquid(options = {})
Locomotive::Liquid::Drops::Page.new(self)
end
protected protected

View File

@ -1,5 +1,6 @@
BOARD: BOARD:
- access page children in liquid
- new custom field types - new custom field types
- boolean - boolean
@ -16,6 +17,7 @@ BACKLOG:
- new custom field types: - new custom field types:
- file - file
- date - date
- belongs_to => association
- refactoring admin crud (pages + layouts + snippets) - refactoring admin crud (pages + layouts + snippets)
- refactor slugify method (use parameterize + create a module) - refactor slugify method (use parameterize + create a module)
@ -23,6 +25,8 @@ BACKLOG:
- tiny mce or similar for custom field text type. - tiny mce or similar for custom field text type.
- sitemap
BUGS: BUGS:
- custom fields: accepts_nested_attributes weird behaviour when creating new content type + adding random fields - custom fields: accepts_nested_attributes weird behaviour when creating new content type + adding random fields

View File

@ -4,6 +4,8 @@ module Locomotive
module Drops module Drops
class Base < ::Liquid::Drop class Base < ::Liquid::Drop
@@forbidden_attributes = %w{_id _version _index}
class_inheritable_reader :liquid_attributes class_inheritable_reader :liquid_attributes
write_inheritable_attribute :liquid_attributes, [] write_inheritable_attribute :liquid_attributes, []
attr_reader :source attr_reader :source

View File

@ -2,9 +2,7 @@ module Locomotive
module Liquid module Liquid
module Drops module Drops
class Content < Base class Content < Base
@@forbidden_attributes = %w{_id _version _index}
def before_method(meth) def before_method(meth)
return '' if @source.nil? return '' if @source.nil?

View File

@ -0,0 +1,15 @@
module Locomotive
module Liquid
module Drops
class Page < Base
liquid_attributes << :title << :fullpath
def children
@children ||= liquify(*@source.children)
end
end
end
end
end

View File

@ -0,0 +1,48 @@
require 'spec_helper'
describe Locomotive::Liquid::Drops::Page do
before(:each) do
@home = Factory.build(:page)
@home.stubs(:children).returns([
Page.new(:title => 'Child #1'),
Page.new(:title => 'Child #2'),
Page.new(:title => 'Child #3')
])
@home.children.last.stubs(:children).returns([
Page.new(:title => 'Child #3.1'),
Page.new(:title => 'Child #3.2')
])
end
context '#rendering' do
context '#children' do
it 'renders title of all children pages' do
content = render_template '{% for child in home.children %}{{ child.title }},{% endfor %}'
content.should == 'Child #1,Child #2,Child #3,'
end
end
context '#sub children' do
it 'renders title of all sub children pages' do
content = render_template '{% for child in home.children.last.children %}{{ child.title }},{% endfor %}'
content.should == 'Child #3.1,Child #3.2,'
end
end
end
def render_template(template = '', assigns = {})
assigns = {
'home' => @home
}.merge(assigns)
Liquid::Template.parse(template).render assigns
end
end