Skip to content

Instantly share code, notes, and snippets.

@did
Created February 25, 2011 14:07
Show Gist options
  • Save did/843825 to your computer and use it in GitHub Desktop.
Save did/843825 to your computer and use it in GitHub Desktop.
module Liquid::Tags
class PageBanners < ::Liquid::Tag
Syntax = /(#{::Liquid::Expression}+)\s+max\s+([0-9]+)/
def initialize(tag_name, markup, tokens, context)
if markup =~ Syntax
@collection_name = $1
@max_number = $2
else
raise ::Liquid::SyntaxError.new("Syntax Error in 'pages_banners' - Valid syntax: page_banners <collection> max <number>")
end
super
end
def render(context)
collection = context[@collection_name]
segment = collection.slice(0, @max_number)
segment.inject('') do |html, banner|
html += %{<div class="banner"><img src="#{banner.url}" alt="#{banner.title}" /></div>}
end
end
::Liquid::Template.register_tag('page_banners', PageBanners)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment