Last Updated: February 25, 2016
· cleechtech

Create a custom blog theme with Hexo.js

Hexo.js is an awesome blogging platform. If you have ever used Ruby's Jekyl it is like that but made of javascript! It is also open source and free to use.

Generate a blog

$ npm install -g hexo-cli
$ hexo init hexo-personal-site
$ cd hexo-personal-site
$ npm install
$ hexo server

Go to localhost:4000 and you will see the starter blog template! Very easy to get up and running. If you want to use a community build theme go to github/hexo/wiki/themes and choose one from the list. Change line 66 of _config.yml to the name of the theme you want (default is 'landscape').

Create your own template

Hexo themes docs

We can create our own custom theme by making a new folder in the themes/ directory. Change line ~68 in _config.yml to theme: customTheme, then create a customTheme folder in themes.

$ mkdir themes/customTheme

Now let's create the most basic theme possible. We are using the EJS template engine, so make three files in the themes/customTheme/layout folder. One is called layout.ejs:

<!DOCTYPE html>
    <meta charset="utf-8">
    <!-- Use <%= page.title %> for dynamic title -->
    <title><%= config.title %></title>

    <!-- Twitter bootstrap (via cdn) -->
    <link rel="stylesheet" href="" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

    <div class='container'>
        <%- body %>

So here we're grabbing twitter bootstrap and setting the page title. The blog will render in the body tag.

The next file is index.ejs and it will be the home page:

<div class='row text-center'>
    <h1><%= %></h1>
    <p><%= config.subtitle %></p>

<div class='row'>
    <% page.posts.each(function(post){ %>
                <a href="<%- url_for(post.path) %>">
                    <%= post.title %>
            <div class='text-muted'>
                <%= date(, config.date_format) %> 

            <% if(post.excerpt) { %>
                    <%- post.excerpt %>
            <% } %>
    <% }) %>

The config variable comes from everything we set in config.yml. The <%- syntax comes directly from EJS syntax. To see a post.excerpt add <!-- more --> to line 5 (or anywhere) in source/_posts/ To create a new post do $ hexo new "My Post Title".

Finally there is the page where the post must render. Create themes/customTheme/layout/post.ejs:

<div class='row'>
    <a class='btn btn-default' href='/'>< Home</a>
<div class='row'>
    <h1><%- page.title %></h1>
    <div class='text-muted'>
        <%= date(, config.date_format) %> 
    <%- page.content %>

Here we make use of the page variable, which contains our post.


This is a very basic template. There is a lot to learn with Hexo but it is a very powerful, customizable platform. You can pick from an existing theme or continue to develop off this one. The source code for this article is on github.