Structure
A Ghost theme contains static HTML templates that make use of helpers to output data from your site, and custom CSS for styling.
The recommended file structure for a Ghost theme is:
# Structure
.
βββ /assets
| βββ /css
| βββ screen.css
| βββ /fonts
| βββ /images
| βββ /js
βββ default.hbs
βββ index.hbs [required]
βββ post.hbs [required]
βββ package.json [required]
An optional /partials
directory allows you to use partial templates across your site to share blocks of HTML between multiple templates and reduce code duplication.
# Structure
.
βββ /assets
βββ /css
βββ screen.css
βββ /fonts
βββ /images
βββ /js
βββ /partials
βββ list-post.hbs
βββ default.hbs
βββ index.hbs [required]
βββ post.hbs [required]
βββ package.json [required]
Templatesβ
Two template files are required: index.hbs
and post.hbs
. All other templates are optional.
Itβs recommended using a default.hbs
file as a base layout for your theme. If you have significantly different layouts for different pages or content types, use the dynamic routing configuration layer, or use partials to encapsulate common parts of your theme.
Theme templates are hierarchical, so one template can extend another template. This prevents base HTML from being repeated. Here are some commonly used templates and their uses:
default.hbsβ
default.hbs
is a base template that contains the boring bits of HTML that exist on every page such as <html>
, <head>
or <body>
as well as the required {{ghost_head}}
and {{ghost_foot}}
and any HTML for the header and footer.
index.hbsβ
This is the standard required template for a list of posts. It is also used if your theme does not have a tag.hbs
, author.hbs
or index.hbs
template. The index.hbs
template usually extends default.hbs
and is passed a list of posts using the {{#foreach}}
helper.
home.hbsβ
An optional template to provide special content for the home page. This template is only used to render /
.
post.hbsβ
The required template for a single post which extends default.hbs
and uses the {{#post}}
helper to output the post details. Custom templates for individual posts can be created using post-:slug.hbs
.
page.hbsβ
An optional template for static pages. If this is not specified then post.hbs
will be used. Custom templates for individual pages can be mapped to the page using page-:slug.hbs
.
customβ
Optional custom templates that can be selected in the admin interface on a per-post basis. They can be used for both posts and pages.
tag.hbsβ
An optional template for tag archive pages. If not specifed the index.hbs
template is used. Custom templates for individual tags can be created using tag-:slug
.
author.hbsβ
An optional template for author archive pages. If not specified the index.hbs
template is used. Custom templates for individual authors can be created using author{{slug}}
.
private.hbsβ
An optional template for the password form page on password protected publications.
error.hbsβ
An optional theme template for any 404
or 500
errors that are not otherwise handled by error- or class-specific templates. If one is not specified Ghost will use the default.
errorβ
An optional theme template for errors belonging to a specific class (e.g. error-4xx.hbs
for 400
-level errors). A matching error class template is prioritized over both error.hbs
and the Ghost default template for rendering the error.
errorβ
An optional theme template for status code-specific errors (e.g. error-404.hbs
). A matching error code template is prioritized over all other error templates for rendering the error.
amp.hbsβ
An optional theme template for AMP (Accelerated Mobile Pages). If your theme doesnβt provide an amp.hbs
file, Ghost will use its default.
robots.txtβ
Themes can include a robots.txt which overrides the default robots.txt provided by Ghost.
The development version of the default theme Casper can be used to explore how Ghost themes work, or you can customise Casper and make it your own!
Helpersβ
Ghost templates are constructed from HTML and handlebars helpers. There are a few requirements:
In order for a Ghost theme to work, you must make use of the required helpers: {{asset}}
, {{body_class}}
, {{post_class}}
, {{ghost_head}}
, {{ghost_foot}}
.
Contextsβ
Each page in a Ghost theme belongs to a context which is determined by the URL. The context will decide what template will be used, what data is available and what is output by the {{body_class}}
helper.
Stylingβ
When building themes it is important to consider the scope of classes and IDs to avoid clashes between your main styling and your post styling. IDs are automatically generated for headings and used inside a post, so itβs best practice to scope things to a particular part of the page. For example: #themename-my-id
is preferrable to #my-id
.
Development modeβ
It is recommended to use a local install to build a custom theme using development mode β review the local install guide to get started with your own local install for development.
In production mode, template files are loaded and cached by the server. For any changes in a hbs
file to be reflected, use the ghost restart
command.
Ghost will automatically check for fatal errors when you upload your theme into Ghost admin. For a full validation report during development, use the GScan tool.
Package.jsonβ
The package.json
file is required, and sets some information about your theme, so itβs important to keep it up to date with relevant information.
To reference a working example of a package.json
file, review the Casper file, and for further information about specific details of package.json
handling, read the npm docs.
// package.json
{
"name": "your-theme-name",
"description": "A brief explanation of your theme",
"version": "0.5.0",
"license": "MIT",
"author": {
"email": "your@email.here"
},
"screenshots": {
"desktop": "assets/screenshot-desktop.jpg",
"mobile": "assets/screenshot-mobile.jpg"
},
"config": {
"posts_per_page": 10,
"image_sizes": {},
"card_assets": true
}
}
The data in the file must be valid JSON, including double quotes around all property names. Every property except the last one should be separated by a comma.
Additional propertiesβ
Here are some of the most common optional properties that can be used in the package.json
file:
config.posts_per_page
β the default number of posts per page is 5config.image_sizes
β read more about using image sizes guide for more detailsconfig.card_assets
β configure the card CSS and JS that Ghost automatically includesconfig.custom
- add custom settings to your themedescription
β provides a short description about your theme and what makes it uniquedocs
- include a URL to docs about how to use the theme. The link to the docs will be available in Ghost Admin on the Design pagelicense
β use a valid licence string, we recommendMIT
π
Changes to the package.json
require a restart using the ghost restart
command.
Next stepsβ
The rest of the theme documentation explores how contexts and helpers work, and serves as a useful reference list for your theme development.
For community led support about theme development, visit the forum.