Skip to content

Instantly share code, notes, and snippets.

@plusjade
Created May 15, 2012 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plusjade/2705778 to your computer and use it in GitHub Desktop.
Save plusjade/2705778 to your computer and use it in GitHub Desktop.
Sample ruhoh plugin to extend mustache with "posts_limit_n" block helpers
# Sample ruhoh plugin to extend mustache with "posts_limit_n" block helpers
# See sample.html for sample usage.
# Installation: place this ruby file in the _plugins directory in the root of your blog.
class Ruhoh
module Templaters
module Helpers
LimitRegex = /^(\w+)_limit_(\d+)/
def limit_to(method, limit)
return false unless self.respond_to?(method.to_sym)
self.__send__(method)[0, limit.to_i]
end
def method_missing(name, *args, &block)
if name.to_s =~ LimitRegex
self.limit_to($1, $2) || super
else
super
end
end
def respond_to?(method)
if method.to_s =~ LimitRegex
true
else
super
end
end
end
end
end
<h3>Posts</h3>
<ul>
{{#posts_limit_2}}
<li>{{title}}</li>
{{/posts_limit_2}}
</ul>
<h3>Categories</h3>
<ul>
{{#categories_limit_2}}
<li>{{name}}</li>
{{/categories_limit_2}}
</ul>
<h3>Tags</h3>
<ul>
{{#tags_limit_2}}
<li>{{name}}</li>
{{/tags_limit_2}}
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment