Created
May 15, 2012 22:53
-
-
Save plusjade/2705778 to your computer and use it in GitHub Desktop.
Sample ruhoh plugin to extend mustache with "posts_limit_n" block helpers
This file contains hidden or 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
# 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 |
This file contains hidden or 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
<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