Search code examples
rubymiddlemanstatic-site

Access blog config variables in layouts


Turns out the below is not a good description of what I needed. Through all this research I've figured out how to ask the question I should have been asking. New post: here. Leaving this question in case it benefits someone else.


I am building a site with multiple "blogs" activated and there are instances where it would be useful to be able to get things like the blog.prefix from config.rb. I know you can set variables and use instance variables via config.rb but that means in templates I have to know which blog I'm in which doesn't solve my problem. (I want one layout for all the blogs, not a duplicate one for each.)

Is there a way I can get the blog.prefix and other activation variables from config.rb and use it in a layout like:

<p>The blog prefix is: <%= blog.prefix %></p>

UPDATE

I've tried passing instance variables in the page do block, as well as using locals => { :variable = value } and both result in an Error: cannot find _auto_layout when rendering the page.

I original posted this on the middleman forums but Stackoverflow tends to have more eyes on it. :-D

UPDATE 2

If I have to have a separate variable (say blog1_prefix, blog2_prefix, and blog3_prefix) for each then I have to know which variable I'm calling from the template. I need the template to know which blog it's getting the prefix (or whatever) for.

In my config.rb I have a block that looks something like this:

my_blogs = ["blog1", "blog2", "blog3"]

my_blogs.each do |my_blog|
  activate :blog do |blog|
    blog.prefix = my_blog
  end
end

Is there a way I can set one variable that will allow the layout to know which blog it is being called on so I can do something like in my example above:

<p>The blog prefix is: <%= blog.prefix %></p>

That would render on each of the three blogs from a single layout:

<p>The blog prefix is: blog1</p>

<p>The blog prefix is: blog2</p>

<p>The blog prefix is: blog3</p>


Solution

  • I you use multiple blogs, the "blog" property of the middleman application is an array and you should use blog name to specify which blog you want to get.

    UPDATE:

    So if you activate blogs:

    my_blogs = ["blog1", "blog2", "blog3"]
    
    my_blogs.each do |my_blog|
       activate :blog do |blog|
         blog.name = my_blog
         blog.prefix = my_blog
       end
    end
    

    You can assess specific blog via

    blog("blog1")
    

    If you use template for a blog, you can specify blog name in the frontmatter:

    ---
    pageable: true
    per_page: 5
    blog: blog1
    ---
    

    and use the "blog" variable (which will be the "blog('blog1')" in this context) in the template, e.g. to fetch tags (if you set tag page template):

    <% blog.tags.each do |tag, articles| %>
    

    in this case articles will be fetched for "blog1" blog

    UPDATE 2 - FINALLY SOLVED

    In order to access blog options (blog config variables), e.g. prefix you need

    blog("blog1").options.prefix
    

    the same thing with other options.