Access blog config variables in layouts

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>

The simplest thing would be to make a variable, “export” is via set and also use it in config. There is a way to get at this information, but it’s pretty ugly since blogs support being run multiple times with different options.

blog_prefix = "/test/"
set :blog_prefix, blog_prefix
activate :blog do |b|
  b.prefix = blog_prefix
end

Then use config[:blog_prefix] in your template.

This would work for one blog, but what if I have several on the same site? 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:

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

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

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

Ah, so the hard way :smile:

<%= blog_controller(blog_name).config[:prefix] %>

Where blog_name is a unique ID for a blog, which can be passed in at config time:

activate :blog do |b|
  b.name = "blog1"
end

@tdreyno :worried: hmmm…

undefined method `config' for #<Middleman::BlogExtension:0x007fb04b3907d0>

@tdreyno Turns out I really suck at describing my problem. You and others helped me figure out what I’m actually trying to do, and I started a new post about it here.

Hi,

as per @lyonsinbeta, I’m getting the same error message undefined method `config' for #<Middleman::BlogExtension:.....

Is that expected? Appreciate the other global setting workaround, but I was wondering whether you can look into this too?

Thanks

So, apparently there is an options collection that can be accessed as blog_controller("blogname").options[:page_link] but it turns out that is a copy and not directly overwriting the main object, which uses values defined in the config.rb during the initialisation process.