Publii Developer Documentation
website·Indexed 38 days ago
https://getpublii.com/dev/Technical documentation for developing Publii themes, featuring guides on Handlebars helpers (such as {{#getPages}}, {{#getAuthor}}, and {{#isNot}}), global variables, theme configuration, and the rendering process for static websites.
What's inside Publii
- The Publii post editor is powered by TinyMCE. It supports modifications through theme-specific CSS, custom configuration overrides, and the definition of custom HTML elements via the theme configuration.
Understand the Publii theme rendering process
The Publii renderer operates in several modes depending on the context: Post preview, Tag page preview, Author page preview, Homepage preview, and Full page mode (used for website synchronization).
The full-page rendering sequence follows these steps:
- Theme validation
- Site config loading
- Translation loading
- Website data retrieval from database
- Theme config loading
- Theme files loading
- Registration of Publii Handlebars helpers
- Registration of custom theme helpers from
helpers.js(if present) - Content structure loading and caching
- Common data loading for all views
- Registration of standard and custom partials
- Rendering of Homepage and pagination
- Rendering of Post pages
- Rendering of Tag pages and pagination
- Rendering of Author pages and pagination
- Rendering of Error page (if used)
- Rendering of Search page (if used)
- CSS generation
- Copying config files, assets, and media to output directory
- Rendering of RSS and JSON feeds
- Sitemap creation
Understand how Publii renders Handlebars templates
Publii uses Handlebars to compile.hbsfiles. The final HTML output is generated by combining a.hbstemplate file with a context object containing data related to the current view.<!-- If context is { "message": "Hello World!" } --> <div>{{message}}</div> <!-- Output: <div>Hello World!</div> -->Manage Tag configuration options in Publii
Tag config options allow for customization of tag-related content. These options are managed in two places:
- Theme Settings: Under the 'Tag Options' section, where global default values are set for all tags.
- Edit Tag Screen: In the right-sidebar under 'Other Options', where defaults can be overridden for individual tags.
Configuration options are defined in the
tagConfigsection of theconfig.jsonfile.Understand Publii Handlebars template file roles
Publii uses specific Handlebars (.hbs) template files to render different sections of a website. Each file has access to a specific set of Handlebars tags, categorized into 'template-related tags' (available only in that specific template) and 'common tags' (available across multiple templates).
The primary template files and their purposes are:
index.hbs: Renders the homepage and its pagination.post.hbs: Renders single post pages.page.hbs: Renders single pages.tag.hbs: Renders tag post listings and pagination.tags.hbs: Renders the listing of all tags.author.hbs: Renders author post listings and pagination.feed-xml.hbsandfeed-json.hbs: Generates the XML and JSON feed files.search.hbs(optional): Renders the search page.404.hbs(optional): Renders the 404 error page.
Manage author configuration defaults and overrides
Author configuration options allow for tailoring author-related content. Global default values are set in the Theme Settings screen under the Author Options section. These defaults can be overridden for individual authors in the right-sidebar of the Edit Author screen under the Other Options section. All author config options are defined within theauthorConfigsection of theconfig.jsonfile.Understand Publii theme file types
Publii themes are composed of several key file types that determine structure, style, and logic:
- .hbs (Handlebars): Used for the HTML structure and templating.
- .css: Used for styling, positioning, and visual character.
- .js (JavaScript): Used for complex interactive features.
- .json: Used for storing configuration settings and theme translations.
Understand Handlebars helpers in Publii
Publii uses Handlebars helpers, which are custom functions used within theme templates. These are categorized into two types:
- Conditional Helpers: Used for logic and flow control. These are identified by a hash character (
#) at the start of the helper name (e.g.,{{#checkIf}}). - Inline Helpers: Used to output specific values or formatted data directly (e.g.,
{{date}}).
- Conditional Helpers: Used for logic and flow control. These are identified by a hash character (
Define supported features in a Publii theme
Since Publii v.0.37.0, theme developers can specify which features their theme supports by adding asupportedFeaturessection to thetheme.config.jsonfile. These flags inform the user and the system about the theme's capabilities. This is distinct from renderer settings; while renderer settings control whether a feature is active,supportedFeaturesdeclares if the theme is capable of supporting it at all."supportedFeatures": { "authorImages": true, "authorPages": true, "blockEditor": true, "customComments": true, "customSearch": true, "errorPage": true, "embedConsents": true, "searchPage": true, "tagImages": true, "tagPages": true, "tagsList": true, "customThemeOptionsCss": true, "pages": true, "postsPage": true }Understand Publii post content modification pipeline
Publii applies a series of automatic modifications to content generated in the post editor (TinyMCE or Markdown) before it is displayed in the theme. These operations are performed in a specific sequence by the Publii renderer to ensure optimal page speed and correct HTML structure.Implement a page selector in Publii theme settings
Use the
pages-dropdownfield type in your theme settings to allow users to select one or more specific pages from their site (e.g., designating a 'Privacy Policy' or 'About Us' page).- Return Value: The field returns the ID(s) of the selected page(s).
- Single Selection: When
multipleisfalse, a single page ID is returned. - Multiple Selection: When
multipleistrue, a comma-separated string of page IDs is returned. - Integration: Because this field returns IDs, it should be used in conjunction with the
getPageorgetPageshelpers to retrieve the actual page data.
{ "name": "homepageAboutUsPage", "label": "About Us Page", "group": "Homepage", "value": "", "type": "pages-dropdown", "multiple": false }Understand Publii image types
Publii categorizes images into six types, each serving a specific purpose and having different configuration methods:
- Featured Image: Added via the 'Edit Post' sidebar. Used for article headers and post previews (Home, Tag, Author pages).
- Tag Image: Used to visually represent tag categories.
- Author Image: Used for author page backgrounds or headers to represent a persona/brand (distinct from small avatars).
- Content Image: Images inserted directly into the post body via the editor or drag-and-drop.
- Option Images: Theme-specific images uploaded via 'Theme Options' (e.g., hero sections or sliders).
- Gallery Images: Images uploaded to a Gallery inserted into a post via the Post Editor.
Content and Gallery images are handled automatically by Publii, while Featured and Option images are more flexible and configurable via the theme's
config.json.