Search code examples
javascriptrubystaticyamlmiddleman

Using global variables in middleman? i.e. Website title


I'm trying to find a way to use global variables within middleman, i.e. set website name and use it as a fallback if current page doesn't have a title or when passing app title parameter inside metas like <meta name="application-name" content="Site name"> so I don't have to write this manually every time. Issue is that at present I am only able to get current page title like <%= current_page.data.title || "Site Name" %> where it is different for every layout, instead of "Site Name" I'd like to pass a global variable that is stored somewhere in one file and can be accessed by every page.


Solution

  • You could use data files. Simply create a file called site.json in the data folder at the root level of your project. You can then access it using the data path.

    For example:

    data/site.json:

    {
      "title": "Foo Test"
    }
    

    partials/head.html.erb (or any other partial/layout/page)

    <title><%= current_page.data.title || data.site.title %></title>
    

    I just tested this out and it works for me using Middleman 3.4.