Working With Ghost Authors / Author Context
This post will discuss Ghost Author’s context and how Ghost decides to select which theme file to show author data with some examples.
Every Ghost author has its page to show the author’s information like name, photo, and a list of posts written by that author. So, when you visit your author page on your website, for example, /author/ahmad
, you are now in the author context.
The index.hbs
theme file is used as the default author context template.
So, how can we know if we’re on the blog home page or the author page? We can use the {{#is 'author'}}{{/is}}
helper to know that.
So, in the index.hbs
file, we can do.
{{#is 'author'}}
{{/is}}
Between these two lines, we can show the author’s information, like name and photo. But first, we need to use the {{#author}}{{/author}}
block helper. Within that helper, we can access the author properties we need.
{{#is 'author'}}
{{#author}}
{{ name }}
<img src='{{ profile_image }}' alt='{{ name }}'>
{{/author}}
{{/is}}
You might say that you don’t want to use the index.hbs
file for the author page. Is there another solution? Yes, we can create a new template file and call it author.hbs
.
How does Ghost choose between index.hbs
and author.hbs
? Well, Ghost will look for the author.hbs
first and select it if it exists. If not, Ghost will choose index.hbs
. That’s why the index.hbs
file is necessary for any Ghost theme.
So, in the author.hbs
file, the previous example will be.
{{#author}}
{{ name }}
<img src='{{ profile_image }}' alt='{{ name }}'>
{{/author}}
Note that I removed the {{#is 'author'}}{{/is}}
helper as we are now in a dedicated author context page.
You can access more author attributes; besides the name
and profile_image
we used above, see a list of all the author attributes at Author object attributes
Suppose you want to go deeper and create a dedicated page for a single author. For example, if you want to design a unique page for every author. You can name the template file as author-:slug.hbs
where the :slug
part will be the author slug. So, if your user URL is /author/ahmad/
, create the author-ahmad.hbs
file.
After we talked about this custom author template, let’s review how Ghost will choose the template file. First, it will check if a template matches the author slug, then looks for the author.hbs
file, and then ends up using the index.hbs
file if both of them did not exist.
That’s all that I wanted to share today, and I hope you find this post helpful.