From 04b0849dc5619f73e91879409f8f24055cc4f183 Mon Sep 17 00:00:00 2001 From: Dirk Kelly Date: Wed, 2 Mar 2011 08:14:58 +0800 Subject: [PATCH] Added the liquid tag for content, retrieves an editable element from self or parent if inherited: true --- lib/locomotive/liquid/tags/editable/ouput.rb | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 lib/locomotive/liquid/tags/editable/ouput.rb diff --git a/lib/locomotive/liquid/tags/editable/ouput.rb b/lib/locomotive/liquid/tags/editable/ouput.rb new file mode 100644 index 00000000..1964f92a --- /dev/null +++ b/lib/locomotive/liquid/tags/editable/ouput.rb @@ -0,0 +1,49 @@ +module Locomotive + module Liquid + module Tags + module Editable + class Content < ::Liquid::Tag + + Syntax = /(#{::Liquid::Expression}+)?/ + + def initialize(tag_name, markup, tokens, context) + if markup =~ Syntax + @slug = $1 + @options = { :inherit => false } + markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') } + else + raise ::Liquid::SyntaxError.new("Syntax Error in 'content' - Valid syntax: slug") + end + + super + end + + def render(context) + page = context.registers[:page] + element = find_element(page) + + if element.nil? && @options[:inherit] != false + while page.parent.present? && element.nil? + page = page.parent + element = find_element(page) + end + end + + if element.present? + return element.content + else + raise ::Liquid::SyntaxError.new("Error in 'content' - Can't find editable element called `#{@slug}`") + end + end + + def find_element(page) + page.editable_elements.where(:slug => @slug).first + end + + end + + ::Liquid::Template.register_tag('content', Content) + end + end + end +end \ No newline at end of file