Module: Middleman::Blog::Helpers
- Defined in:
- lib/middleman-blog/helpers.rb
Overview
Blog-related helpers that are available to the Middleman application in config.rb
and in templates.
Instance Method Summary collapse
-
#blog(blog_name = nil) ⇒ BlogData
Get a BlogData instance for the given blog.
-
#blog_controller(blog_name = nil) ⇒ BlogExtension
Retrieve a Middleman::BlogExtension instance.
-
#blog_day_path(year, month, day, blog_name = nil) ⇒ String
Get a path to the given day-based calendar page, based on the
day_link
blog setting. -
#blog_instances ⇒ Hash<Symbol,BlogExtension>
All the blog instances known to this Middleman app, keyed by name.
-
#blog_month_path(year, month, blog_name = nil) ⇒ String
Get a path to the given month-based calendar page, based on the
month_link
blog setting. -
#blog_year_path(year, blog_name = nil) ⇒ String
Get a path to the given year-based calendar page, based on the
year_link
blog setting. -
#current_article ⇒ BlogArticle
Get a BlogArticle representing the current article.
-
#is_blog_article? ⇒ Boolean
Determine whether the currently rendering template is a BlogArticle.
-
#page_articles(blog_name = nil) ⇒ Array<Middleman::Sitemap::Resource>
Returns the list of articles to display on this particular page (when using pagination).
-
#paginate ⇒ Boolean
Whether or not pagination is enabled for this template.
-
#tag_path(tag, blog_name = nil) ⇒ String
Get a path to the given tag page, based on the
taglink
blog setting.
Instance Method Details
#blog(blog_name = nil) ⇒ BlogData
Get a BlogData instance for the given blog. Follows the same rules as #blog_controller.
49 50 51 |
# File 'lib/middleman-blog/helpers.rb', line 49 def blog(blog_name=nil) blog_controller(blog_name).data end |
#blog_controller(blog_name = nil) ⇒ BlogExtension
Retrieve a Middleman::BlogExtension instance. If blog_name
is provided, the instance with that name will be returned. Otherwise, an attempt is made to find the appropriate blog controller for the current resource. For articles this is always available, but for other pages it may be necessary to name the blog in frontmatter using the “blog” blog_name. If there is only one blog, this method will always return that blog.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/middleman-blog/helpers.rb', line 24 def blog_controller(blog_name=nil) if !blog_name && current_resource blog_name = current_resource.[:page]["blog"] if !blog_name blog_controller = current_resource.blog_controller if current_resource.respond_to?(:blog_controller) return blog_controller if blog_controller end end # In multiblog situations, force people to specify the blog if !blog_name && blog_instances.size > 1 raise "You must either specify the blog name in calling this method or in your page frontmatter (using the 'blog' blog_name)" end blog_name ||= blog_instances.keys.first blog_instances[blog_name.to_sym] end |
#blog_day_path(year, month, day, blog_name = nil) ⇒ String
Get a path to the given day-based calendar page, based on the day_link
blog setting.
102 103 104 |
# File 'lib/middleman-blog/helpers.rb', line 102 def blog_day_path(year, month, day, blog_name=nil) build_url blog_controller(blog_name).calendar_pages.link(year, month, day) end |
#blog_instances ⇒ Hash<Symbol,BlogExtension>
All the blog instances known to this Middleman app, keyed by name. A new blog is added every time the blog extension is activated. Name them by setting the :name
option when activating - otherwise they get an automatic name like ‘blog0’, ‘blog1’, etc.
10 11 12 |
# File 'lib/middleman-blog/helpers.rb', line 10 def blog_instances @blog_instances ||= {} end |
#blog_month_path(year, month, blog_name = nil) ⇒ String
Get a path to the given month-based calendar page, based on the month_link
blog setting.
92 93 94 |
# File 'lib/middleman-blog/helpers.rb', line 92 def blog_month_path(year, month, blog_name=nil) build_url blog_controller(blog_name).calendar_pages.link(year, month) end |
#blog_year_path(year, blog_name = nil) ⇒ String
Get a path to the given year-based calendar page, based on the year_link
blog setting.
83 84 85 |
# File 'lib/middleman-blog/helpers.rb', line 83 def blog_year_path(year, blog_name=nil) build_url blog_controller(blog_name).calendar_pages.link(year) end |
#current_article ⇒ BlogArticle
Get a BlogArticle representing the current article.
62 63 64 65 66 67 68 69 |
# File 'lib/middleman-blog/helpers.rb', line 62 def current_article article = current_resource if article && article.is_a?(BlogArticle) article else nil end end |
#is_blog_article? ⇒ Boolean
Determine whether the currently rendering template is a BlogArticle. This can be useful in layouts and helpers.
56 57 58 |
# File 'lib/middleman-blog/helpers.rb', line 56 def is_blog_article? !current_article.nil? end |
#page_articles(blog_name = nil) ⇒ Array<Middleman::Sitemap::Resource>
Returns the list of articles to display on this particular page (when using pagination).
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/middleman-blog/helpers.rb', line 116 def page_articles(blog_name=nil) = current_resource. limit = [:page]["per_page"] # "articles" local variable is populated by Calendar and Tag page generators # If it's not set then use the complete list of articles articles = [:locals]["articles"] || blog(blog_name).articles limit ? articles.first(limit) : articles end |
#paginate ⇒ Boolean
Whether or not pagination is enabled for this template. This can be used to allow a single template to work in both paginating and non-paginating modes.
109 110 111 |
# File 'lib/middleman-blog/helpers.rb', line 109 def paginate false end |
#tag_path(tag, blog_name = nil) ⇒ String
Get a path to the given tag page, based on the taglink
blog setting.
75 76 77 |
# File 'lib/middleman-blog/helpers.rb', line 75 def tag_path(tag, blog_name=nil) build_url blog_controller(blog_name).tag_pages.link(tag) end |