Search code examples
rubymiddleman

Define custom title for calendar pages in MiddleMan


I've just set up a small static blog using middleman 3 and middleman-blogging gems, and am currently polishing everything. I was looking at my pages from a SEO point of view, and noticed all my calendar pages display the same title (which is quite bad for SEO, afaik).

Currently, I use this to generate the title tag in my layout (Slim template engine) :

title
  | #{current_article.title unless current_article.nil?} 
  | #{ " | " unless current_article.nil?}
  | My blog name

For articles, it's quite easy as I just need to define the title in the frontmatter, but I obviously can't go this way for my dynamically generated calendar pages. Unless I can use variables in the frontmatter of calendar.slim, but I couldn't get anything working so far. Maybe through the config.rb file ?

Thanks for your help !


Solution

  • In the end, here's how I chose to proceed : in my calendar.slim and tag.slim templates, I simply defined a @title variable that I use in my default layout. In calendar.slim, I use the built-in variables year, month and day to build a title string, and in tag.slim, I use the built-in tagname variable.

    [calendar.slim]
    
    - case page_type
    - when 'month'
      - date = date_to_fr Date.new(year, month, 1).strftime('%B %Y')
    - when 'year'
      - date = year
    
    - @title = "#{date} - Archives"
    
    (...)
    

    And then in the layout, I then use the following code to display my @title variable followed by my blog name, unless @title does not exist (then I just use my blog name as the page title) :

    [layout.slim]
    
    title 
          = "#{@title} | " unless @title.nil?
          | Pierre-Adrien Buisson : Le Blog !