agentem
agentem

Reputation: 811

blogging on middleman: how to configure site with blog not on homepage?

I have a site I'm creating, here with Middleman. I'd like to have a blog, accessible on the site via the "updates" nav button, but I can't seem to figure it out. I know how to set up the middleman blog site, but that puts the blog itself at index.html. So how do I set up the config and other files so that I can go to newsite.com/updates.html and see a blog?

I tried setting up the blog in my existing project by running middleman init --template=blog inside the directory, that worked to get the files, but it created the separate index file I don't want. I tried changing the config.rb to say:

activate :blog do |blog|
  blog.prefix = "updates" (also tried "updates.html")
end

but that didn't do anything, so maybe I misunderstand what that's for. Any help much appreciated.

Upvotes: 2

Views: 361

Answers (1)

agentem
agentem

Reputation: 811

Alright, got it working! For reference, here's what I did:

  1. I created a separate project folder, called testblog. In it, I initialized middleman using the blog type command:

    middleman init --template=blog
    
  2. in my already created, non-blog type middleman site, I set up my config.rb and Gemfile to include all of the blog stuff that I now had in testblog. Specifically, to my Gemfile I added:

    gem 'middleman-blog'
    
    gem "builder", "~> 3.0"
    
    gem 'redcarpet', '~> 3.3', '>= 3.3.3'
    

    and to my config.rb I added:

    activate :blog do |blog|
        blog.tag_template = "tag.html"
        blog.calendar_template = "calendar.html"
    end
    
  3. I created all the requisite new blog files in /source except for index.html.erb:

    • tag.html.erb
    • layout.erb
    • feed.xml.builder
    • calendar.html.erb
  4. I copied the contents of tag.html.erb, feed.xml.builder, and calendar.html.erb from testblog over to their respective files in my personal site blog.

  5. In layout.erb, I copied and pasted the contents from the testblog project to my real project like the others, but then I removed all of the foundational html tags, leaving this:

    <div id="main" role="main">
      <%= yield %>
    </div>
    
    <aside>
      <h2>Recent Articles</h2>
      <ol>
        <% blog.articles[0...10].each do |article| %>
          <li><%= link_to article.title, article %> <span><%= article.date.strftime('%b %e') %></span></li>
        <% end %>
      </ol>
    
      <h2>Tags</h2>
      <ol>
        <% blog.tags.each do |tag, articles| %>
          <li><%= link_to "#{tag} (#{articles.size})", tag_path(tag) %></li>
        <% end %>
      </ol>
    
      <h2>By Year</h2>
      <ol>
        <% blog.articles.group_by {|a| a.date.year }.each do |year, articles| %>
          <li><%= link_to "#{year} (#{articles.size})", blog_year_path(year) %></li>
        <% end %>
      </ol>
    </aside>
    
  6. Then, I took the contents of source/index.html.erb from the testblog project, and I pasted it in the page I wanted the blog to appear in, in this case my updates.html.erb file.

  7. Bundle install, and in my case I had to update to get it all working, but that's it!

Upvotes: 1

Related Questions