From b13c5d1d408777671c79d35443fc0ef6649f85f1 Mon Sep 17 00:00:00 2001 From: Paul Sponagl Date: Tue, 6 Mar 2012 22:55:45 +0100 Subject: [PATCH] added a snippet option to the liquid nav tag this is useful if you want to render more than just a page.title string within the nav list item. (page.menue_image, page.menue_teaser...) use nav snippet: "" to provide a string or nav snippet: "" to load the snippet from db. added page. for usage within liquid templates --- app/models/locomotive/extensions/page/tree.rb | 6 ++--- app/models/locomotive/page.rb | 2 +- lib/locomotive/liquid/drops/page.rb | 4 ++++ lib/locomotive/liquid/tags/nav.rb | 23 +++++++++++++++---- spec/lib/locomotive/liquid/drops/page_spec.rb | 15 ++++++++++++ spec/lib/locomotive/liquid/tags/nav_spec.rb | 4 ++++ 6 files changed, 45 insertions(+), 9 deletions(-) diff --git a/app/models/locomotive/extensions/page/tree.rb b/app/models/locomotive/extensions/page/tree.rb index 5f619658..41adf835 100644 --- a/app/models/locomotive/extensions/page/tree.rb +++ b/app/models/locomotive/extensions/page/tree.rb @@ -78,8 +78,8 @@ module Locomotive # # @return [ Array ] The children pages ordered by their position # - def children_with_minimal_attributes - self.children.minimal_attributes + def children_with_minimal_attributes( attrs = [] ) + self.children.minimal_attributes( attrs ) end # Assigns the new position of each child of this node. @@ -109,4 +109,4 @@ module Locomotive end end end -end \ No newline at end of file +end diff --git a/app/models/locomotive/page.rb b/app/models/locomotive/page.rb index 3a651224..7126e454 100644 --- a/app/models/locomotive/page.rb +++ b/app/models/locomotive/page.rb @@ -51,7 +51,7 @@ module Locomotive scope :published, :where => { :published => true } scope :fullpath, lambda { |fullpath| { :where => { :fullpath => fullpath } } } scope :handle, lambda { |handle| { :where => { :handle => handle } } } - scope :minimal_attributes, :only => %w(title slug fullpath position depth published templatized redirect listed parent_id created_at updated_at) + scope :minimal_attributes, lambda { |attrs=[]| {:only => attrs + %w(title slug fullpath position depth published templatized redirect listed parent_id created_at updated_at) } } ## methods ## diff --git a/lib/locomotive/liquid/drops/page.rb b/lib/locomotive/liquid/drops/page.rb index 5282fb31..1589a830 100644 --- a/lib/locomotive/liquid/drops/page.rb +++ b/lib/locomotive/liquid/drops/page.rb @@ -41,6 +41,10 @@ module Locomotive self._source.published? end + def before_method(meth) + self._source.editable_elements.where(:slug => meth).try(:first).try(:content) + end + end end end diff --git a/lib/locomotive/liquid/tags/nav.rb b/lib/locomotive/liquid/tags/nav.rb index 406fa7cd..169b97ab 100644 --- a/lib/locomotive/liquid/tags/nav.rb +++ b/lib/locomotive/liquid/tags/nav.rb @@ -23,6 +23,16 @@ module Locomotive markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') } @options[:exclude] = Regexp.new(@options[:exclude]) if @options[:exclude] + + @options[:add_attributes] = [] + if @options[:snippet] + template = @options[:snippet].include?('{') ? @options[:snippet] : context[:site].snippets.where(:slug => @options[:snippet] ).try(:first).try(:template) + unless template.blank? + @options[:liquid_render] = ::Liquid::Template.parse( template ) + @options[:add_attributes] = ['editable_elements'] + end + end + else raise ::Liquid::SyntaxError.new("Syntax Error in 'nav' - Valid syntax: nav ") end @@ -60,12 +70,12 @@ module Locomotive @site, @page = context.registers[:site], context.registers[:page] children = (case @source - when 'site' then @site.pages.root.minimal_attributes.first # start from home page + when 'site' then @site.pages.root.minimal_attributes( @options[:add_attributes] ).first # start from home page when 'parent' then @page.parent || @page when 'page' then @page else - @site.pages.fullpath(@source).minimal_attributes.first - end).children_with_minimal_attributes.to_a + @site.pages.fullpath(@source).minimal_attributes( @options[:add_attributes] ).first + end).children_with_minimal_attributes( @options[:add_attributes] ).to_a children.delete_if { |p| !include_page?(p) } end @@ -75,7 +85,10 @@ module Locomotive selected = @page.fullpath =~ /^#{page.fullpath}/ ? " #{@options[:active_class]}" : '' icon = @options[:icon] ? '' : '' - label = %{#{icon if @options[:icon] != 'after' }#{page.title}#{icon if @options[:icon] == 'after' }} + + title = @options[:liquid_render] ? @options[:liquid_render].render( 'page' => page ) : page.title + + label = %{#{icon if @options[:icon] != 'after' }#{title}#{icon if @options[:icon] == 'after' }} output = %{