Creating a New Theme for SiteBuilder
A theme module in Siteglide is generally a module which does not add database functionality to the Siteglide Admin or the front-end, but does add files which are helpful for styling the front-end of a website. With the help of this documentation, you can go a step further by hooking into SiteBuilder functionality:
Page Templates
You'll be able to configure SiteBuilder to create a Siteglide Page Template, containing all the global CSS and JS your theme needs for its HTML to look and interact as expected.
Layouts
Any static or dynamic HTML or Liquid (respectively) layouts will not be installed straight away on a user's site, but will be available in the module UI. There are two main advantages of this: firstly, it avoids clutter; secondly, it allows you to make improvements to layouts without breaking any of the module-user's own changes- instead your improvements will be fetched next time the layout is installed.
Compatibility with Other Themes
Building a theme module lets you create a single theme per module. However, by configuring the settings, you can mark your theme as compatible with other themes which use a similar CSS framework. This allows module-users to use layouts from several simialr thems on the same site.
About our example theme
In this documentation, for an example, we'll be creating a theme for the Siteglide Studio Design System. As this theme is moving to being deprecated on Siteglide, it's an excellent opportunity to turn it into an open-source theme module to show you what is involved.
Step 1 - Registering the Module in the Siteglide Marketplace
You'll need to register the new module with Siteglide to receive the "vanity_id". We'll use this as a unique ID to identify your theme and module within SiteBuilder.
First in the Siteglide Portal, go to the marketplace tab and select "add new".
Secondly, fill out the name, type and description. For type, select "theme".
Thirdly, make a note of your "vanity ID". We'll refer to this as <module_vanity_id> where it needs to be substituted in the examples.
Step 2 - Registering the Module with SiteBuilder
Most of the files in your module will be in the private folder so SiteBuilder won't know they're there unless it knows where to look. You'll next need to create the following file at the public path: modules/module_<module_vanity_id>/public/views/partials/sitebuilder/module_registry.liquid
to make your module available to SiteBuilder.
The placeholder <module_vanity_id> should be replaced by the vanity ID that Siteglide gave you earlier.
Step 3 - File Structure and Security
While most of your module files will sit in the private folder, Liquid allows developers to access private files if they know their filepaths.
To give an optional extra level of protection to your intellectual property, you can register your module with Sitegurus in return for a secret key. We'll give you this secret key and associate it with your module vanity ID so that SiteBuilder can find your files, but your users can't. When you're given your secret key, the /sitebuilder/ folder will be replaced in your file structure with /sitebuilder_<secret_key>/
Do bear in mind that this system isn't perfect and we cannot guarantee that Liquid errors will not expose this key. We recommend careful testing to avoid errors with each new version.
If your module is open-source or you want to keep things simple, feel free to skip this section.
Step 4 - Configuring your Theme
You'll need to add a theme_config file at the following path: modules/module_<module_vanity_id>/private/views/partials/sitebuilder<secret_key_preceded_by_underscore>/theme_config.liquid
. This adds the metadata SiteBuilder needs to understand your theme.
For the content, add the following, adapting it to your module where suitable:
Step 5 - Adding CSS Files to be used on Page Templates
In the previous step, when editing the theme_config file, there was a CSS preferences object. Here we will look at this, and the alternative and add the files that are referenced in the config.
CSS File Structure
This is an example CSS file structure for the same theme.
Default CSS
Firstly, ../css/default/css.liquid
contains a Liquid snippet which will be added to all templates created using your theme. This happens regardless of whether or not a CSS preference is chosen (see next sub-section).
This file will contain Liquid/HTML. For example, it can be useful to include tags to files which are hosted on an external CDN and don't need to be hosted on Siteglide or edited. It can also contain inline style tags, should you so wish.
It is okay to include a small amount of JS here, for example, Flowbite includes a few lines of render-blocking JS so that dark mode is supported immediately on page load. As in this example, it's recommended that JS is only included here where it relates to styling. Another file exists for the main JS includes.
If you don't need to use this, you must include the file, but please leave the contents completely blank.
CSS Preferences
As well as default CSS, you can choose to have some CSS files which are copied onto the module user's site and hosted there. The benefit of this is that it allows the module-user to edit that CSS.
This feature is very flexible. For example, you can cater to both module-users who prefer writing native CSS or users who use SCSS; you configure the options and then let the user decide which to install when they create a template.
We hope the flexibility is enough to enable a wide range of CSS frameworks.
Create a folder for each option you'd like to give the user. In the example file structure above, there is
min_css
andcss
. These folder names must match the keys in the theme_config'scss_preference_options
object.Within each folder, create a liquid file for each file you'd like to install. The file should be named the same as the destination file, but you must replace any special characters with their URL encoded equivalents. For example, an underscore should be replaced with
%5F
- see the example.In each file, add YAML at the top of the Liquid file to add the following metadata- the role of this is to set what the extension of the final destination file will be.
Below this add the body of the file. e.g. the actual CSS/SCSS code.
When a user selects this option while building a template, all the files will be created on their site- but not all will have HTML
<link>
tags added to the template. - Remember to add thelink_in_template: true
property in the theme_config for each option where this is needed- e.g. for.min.css
files but not for.scss
files.
If you don't need the CSS preferences feature, leave the "css_preference_options" property as an empty object {}
in the theme_config.\
Step 6 - JavaScript
To add JavaScript (which you don't expect to be edited by the ordinary module-user) to any templates which need to be installed, you can add any script tags to the path modules/module_<module_vanity_id>/private/views/partials/sitebuilder/theme_<module_vanity_id>/js.liquid
. These will be added to the template, as is, when the template is created.
The JavaScript itself, where necessary, should be added to .js
files in your module's private folder, or you may choose to link to scripts in a CDN.
Editable JavaScript
If there is some JavaScript you expect to be edited by the user, it's often best to add this as an inline <script></script>
tag, either in the main js.liquid file, or in an individual layout.
Inline JS for capturing Liquid variables
Another situation where it might be useful to write inline JS is if you need to capture a variable from the Liquid. In this case, it's useful to write a short inline script to assign the variables to the global scope and then use a longer external script to use those variables. You can also write an API request to a Liquid page to fetch Liquid variables into your JS.
Performance
All JavaScript will be added to the head of the page, for simplicity.
To improve performance, it is recommended therefore that you write as much JavaScript as possible so that it can be deferred or loaded asynchronously.
Tips:
Write event listeners instead of using events which are stored in HTML attributes like
onclick=""
. This means that if a button is clicked before the JS is loaded, no "function is not defined" error will occur. The button only becomes interactive once the JS has loaded.If using defer, your JS can normally use the DOMContentLoaded event to run your main JavaScript.
HTML
JS
For async code, you won't be able to control the order in which scripts load. To handle any dependencies, you can either use a bundler like Webpack to load JavaScript modules from a registry like npm or use the following pattern to load dependencies efficiently from a CDN:
HTML
JS
Last updated
Was this helpful?