Custom modules are reusable components you can use across your website. Build them following best practices for speed, user experience and accessibility.
module.css
and module.js
from that module once. By default, module.css
and module.js
do not load asynchronously, but you can change this by including css_render_options and js_render_options in the module’s meta.json.
Modules can be built within the design manager or locally using the HubSpot CLI. In the design manager, module files are displayed in a multi-pane editor.
module.css
file to add CSS to a module.
In general, module.css
supports a very limited subset of HubL. However, you can use module_asset_url("my-image.png")
for images added as module linked assets. This enables linking assets such as images, packaged with the module itself. For example:
module.htm
l file which correspond to CSS classes in your module.css
file.
For example, you may have an image and text module. You want content creators to be able to position the image to the right or left of the text based on a choice field. To do this, you could set your module.html
and module.css
files as follows:
require_css
blocks are the best option.
To give content creators direct control over specific properties without using classes, you can instead add styling to the module.html
file within require_css
tags. For example:
module.html
can render HubL, you can use module field values as CSS variables. When a content creator updates the field in the page editor, the CSS will update to match. These block move the <style>
tags into the <head>
of your page within the standard_header_includes
statement.
You can also set the CSS to be scoped to only the module instance by wrapping the CSS with scope_css
tags. For example, you could update the above module code as follows:
require_css
block method.
require_css
is a HubL function that you can add to module.html which tells HubSpot that a particular module or template requires a particular CSS file to display. A link tag pointing to the css file is added to the page’s <head>
inside of the standard_header_includes
.
The require_css
function will only load that CSS file once, regardless of how many times that same file is required by modules and templates on a particular page. This makes it great for situations where styles may be shared across multiple modules, but where adding the CSS directly to the main stylesheets used on every page for your site may not make sense.
require_css
and linked CSS files fill the same purpose, but require_css
can be used conditionally based on field values. This prevents loading unnecessary code.
module.js
file to add JavaScript to a module.
Like the module.css
file, the module.js
file does not support HubL.
class="yourClassName"
, all elements support data-your-attribute="yourValue"
.
require_js
block to provide variables you can access from your templating script.
require_js
is a HubL function that tells HubSpot that a particular module or template requires a particular JavaScript file to load properly. The function takes two parameters: the path to the file and the location the file is to be added to (“head” or “footer”).
In a module require_js
can only be added to the module.html. The JavaScript file referred to in the require_js
statement will only be loaded once per page, regardless of how many times it is required by modules and templates within the page. This reduces the number of HTTP requests and prevents duplicate code.
Some situations where this becomes handy:
require_js
to share that script across modules.require_js
, you can associate the JavaScript with your module.require_js
and linked javascript files serve the same purpose, but require_js
can be done conditionally based on field values. This prevents unnecessary code from being loaded. You also have the additional option of loading JavaScript in the head, should you need that.
require_js
places JavaScript is the “footer”. Learn more about optimizing for performance.