Created
February 25, 2011 14:07
-
-
Save did/843825 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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