Intro: readable HubL or a constant headache
Most HubSpot CMS installs do not die from one big mistake; they get worse one quick fix at a time. A conditional dropped into the wrong template, a duplicated module, some inline CSS jammed next to HubL, and suddenly nobody wants to open Design Manager.
HubL is not the villain here. The problem is how that HubL is spread around the portal and how it looks after months of edits from different people. If you want to be able to ship changes fast without dreading the code, you need a few simple rules about where HubL goes and how it is written.
1. Decide where HubL is allowed to live
HubSpot gives you a lot of places to put HubL: templates, modules, partials, even rich text fields. If you let logic live in all of them with no rules, things get messy fast.
A simple setup:
-
Templates handle layout and composition.
-
Modules handle the logic for one piece of UI.
-
Partials/includes handle patterns you reuse in multiple templates.
So a page template should mostly be HTML and module calls, not a wall of if and for. When logic starts to grow inside a template, move it into a module or a macro so the template stays readable and focused on structure.
2. Use names that are boring and obvious
If you cannot guess what something does from its name, you will not enjoy reading that code later. Clear, descriptive names save time for everyone who touches the project.
Use rules like:
-
Prefix modules:
sn_hero_primary,sn_feature_grid,sn_pricing_tableinstead ofcustom_module_24. -
Name fields for what they are:
heading_main,body_copy,cta_primary_label,cta_primary_url, notfield_1andfield_2. -
Use the same words in the editor labels and in the code so devs and marketers talk about the same thing.
If a new developer can open the module list and instantly see what each one does, you are on the right track. If they have to click into each module to guess its purpose, naming is the first thing to fix.
3. Keep HubL logic small and local
HubL gives you loops, conditionals, filters and macros, so it is easy to cram everything into one place. That is how you end up with a template that decides layout, content and edge cases all in a single block.
Treat logic like this:
-
One block does one job. A loop that shows blog posts should not also be in charge of complex funnel rules.
-
If you copy the same HubL snippet into more than two files, stop and move it into a macro with clear parameters.
-
Avoid giant do-everything modules that try to support every possible layout and state with flags; split them into smaller modules that each solve one real problem.
The moment you are scared to delete or change a small piece of HubL because you do not know what else it touches, you know the logic is too tangled. Break it apart until the connections are obvious again.
4. Treat formatting like part of the build, not a nice extra
You can have clean architecture and still hate working in the files if the formatting is all over the place. HTML and HubL in HubSpot are just text; if indenting drifts and blocks are not separated, your eyes will slide off the page.
Make a few simple rules:
-
Pick an indent (two or four spaces) and use it everywhere: templates, modules, partials.
-
Split long tags and module calls across lines so diffs are readable rather than one 200-character line.
-
Keep nested
if,forand similar blocks visually obvious; you should see where each block starts and ends just by scrolling.
This is exactly where your HubL Code Formatter extension fits in. Instead of relying on everyone to format by hand, you open the file in HubSpot’s code editor, hit Shift + Tab or click the Format button, and the extension cleans up HubL, HTML, CSS and JavaScript in one go. If you want the full breakdown of how it works inside HubSpot, the launch post on HubL Code Formatter for HubSpot CMS covers it in detail.
5. Let modules do the heavy lifting
HubSpot wants you to lean on modules and fields instead of stuffing everything into raw HTML or rich text, and for good reason. Modules are how you keep both marketers and developers from breaking things by accident.
Use modules to keep HubL sane:
-
Move repeatable patterns into modules: hero, feature rows, cards, testimonials, pricing and similar sections.
-
Group fields inside a module roughly how the HTML is structured, so the editor view matches the markup.
-
Keep serious HubL and HTML inside coded modules instead of hiding it inside rich text fields, where it is harder to spot and harder to format.
If you do this well, your templates become a clean list of module calls and a bit of layout markup. The complexity sits inside modules, which are easier to test and refactor without worrying about every single page.
6. Comment the intent, not every line
Comments are not decoration; they are there to explain decisions. You do not need to explain basic HubL syntax to any developer, but you do need to explain why a weird branch exists or why a template works in a certain way.
Good use of comments:
-
At the top of a template: what it is for, which modules it assumes, what should not be changed here.
-
Above strange or legacy logic: for example, a note that this section handles old URLs or a particular integration.
-
At the top of shared partials and macros: where they are used and which parameters matter.
If you removed all comments today and nobody could tell what is risky to touch, add them back where the intent is not obvious from the code alone.
7. Agree on rules while the site is still manageable
Almost every messy HubSpot portal has the same story: the site grew, the team changed, and standards never kept up. By the time people notice, every fix feels like surgery.
Put a few decisions in writing now:
-
When a new page gets its own template instead of reusing an old one.
-
When a requirement becomes its own module instead of more flags on a huge existing module.
-
Who is allowed to edit templates and HubL, and who should stick to module settings and content.
Treat these rules like code review checklists. If a change ignores them, it should be refactored before it goes live. That is how you keep HubL readable while still moving fast and shipping campaigns.
Readable HubL is not a nice-to-have; it is what lets you keep changing your site, funnels and campaigns without starting from scratch every year. A bit of structure, some naming discipline, consistent formatting, and the right guardrails in modules go a long way, especially when you pair them with a formatter that makes clean code the default instead of extra work.