0

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>

Community
  • 1
  • 1
lyonsinbeta
  • 919
  • 6
  • 25
  • Do you want prefix to be constant for all blog objects? – Shweta Jan 25 '15 at 16:45
  • Maybe? Sorry for the ambivalence but I'm not 100% if that's what I'm after or not. What I want is to be able to get the `blog.prefix` and other `blog` settings for _each_ blog so when pages are generated from the one template the correct `blog.prefix` (or whatever) is shown. Presently that setting is inaccessible from layout files so the layout doesn't know which `blog` it's generating pages for. (At least, not that I know of...) – lyonsinbeta Jan 25 '15 at 16:48

1 Answers1

1

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.

TSV
  • 7,538
  • 1
  • 29
  • 37
  • This means a specific layout for each blog. I want to be able to access blog information from the layout so that `blog1` info automatically goes with blog1. If I declare which blog the layout applies to it can only work for one blog. – lyonsinbeta Jan 26 '15 at 06:30
  • I've seen a few people say you can call `blog.prefix` from layouts/content but I haven't actually seen it work. Have you tested that? Maybe I'm doing something wrong. – lyonsinbeta Jan 26 '15 at 06:31
  • This means exact what I've written: "if you use multiple blog instances, you сan not use simple 'blog', you should use 'blog()'". This does not related to single or multiple layouts for blogs. – TSV Jan 26 '15 at 06:37
  • @lyonsinbeta Why downvote? Have you already tried my advise and got any errors? – TSV Jan 26 '15 at 06:39
  • I'm not doing a great job explaining my issue and now I can't take away the downvote unless you edit your answer. If you can answer the question as I've updated it that'd be great, but if you just make a small edit I'll take the downvote away. This problem is causing me way too much grief. -_- – lyonsinbeta Jan 26 '15 at 06:53
  • I've changed my answer. Does it help you? – TSV Jan 26 '15 at 07:39
  • This is similar to an answer I got elsewhere but I have had zero luck getting layouts to let me pull anything from the `blog` variable other than when I call `blog.articles`. I declared it in the front matter but when I try to get `blog.prefix` the page errors out with: `undefined method `prefix' for #` – lyonsinbeta Jan 26 '15 at 14:02
  • 1
    I've checked https://github.com/middleman/middleman-blog/blob/master/lib/middleman-blog/blog_data.rb and figured out that you can try blog("blog1").options.prefix – TSV Jan 26 '15 at 15:33
  • It works! My God it works! Thank you so much for your help and patience. You might want to update your answer with your comment in case anyone else comes across this question. Thanks again! – lyonsinbeta Jan 26 '15 at 15:44