LWC Formatter Online — Salesforce Lightning Web Components

Beautify Salesforce Lightning Web Component HTML templates with lwc:if, for:each, lightning- standard components, and event handlers — 100% in your browser.

What is an LWC Formatter?

An LWC formatter beautifies a Salesforce Lightning Web Component HTML template — the .html file in each component folder. LWC templates are HTML supersets with framework-specific directives: lwc:if, lwc:elseif, lwc:else, for:each, iterator, expression bindings {value}, and event handlers like onclick and onchange. The formatter follows ordinary HTML nesting rules and preserves all directives verbatim.

The OpenFormatter LWC tool runs entirely in your browser; nothing is uploaded. Standard components in the lightning- namespace (lightning-card, lightning-button, lightning-input, lightning-record-edit-form) and your own custom c- components format the same way as any other HTML element.

How to format LWC templates — 4 steps

  1. Paste your template. Drop the .html file body of your LWC into the Input panel. Click Load Sample to try a real Account List template.
  2. Click Format. The formatter indents tags and template directives in your browser.
  3. Review the output. Confirm for:each loops indent under their host <template> and lwc:if branches are aligned with their conditional.
  4. Copy the result. Click Copy to paste back into your component folder, a deployment package, or a code review.

Side-by-side: compact input vs formatted output

Compact LWC

<template><lightning-card title="Accounts"><template for:each={accounts} for:item="account"><p key={account.Id}>{account.Name}</p></template></lightning-card></template>

Formatted LWC

<template>
  <lightning-card title="Accounts">
    <template for:each={accounts} for:item="account">
      <p key={account.Id}>{account.Name}</p>
    </template>
  </lightning-card>
</template>

Directive-Aware

Handles lwc:if, lwc:elseif, lwc:else, for:each, for:item, for:index, iterator, key, lwc:dom, and the legacy if:true / if:false.

SLDS-Compatible

Long lists of slds- utility classes are kept on their host element. Bindings, attribute values, and event handlers are preserved exactly.

Browser-Only

Templates run through JavaScript on your machine. References to Apex controllers, field API names, or org-specific data stay on the device.

Common use cases

  • check_circleFormat an LWC template before pushing to a scratch org via sf project deploy
  • check_circleBeautify a template copied from the Salesforce LWC Recipes repo or a Trailhead module
  • check_circleReformat a template extracted from a managed package source for review
  • check_circleClean up a template after migrating from Aura to LWC
  • check_circleFormat a template with deeply nested lightning-record-edit-form fields
  • check_circleIndent a template using lwc:if / lwc:elseif / lwc:else after a Spring 23+ refactor
  • check_circleFormat a template for a code-review screenshot or Salesforce community blog
  • check_circleBeautify a template that uses Lightning Data Table dynamic columns

LWC formatter vs Prettier vs PMD for Lightning

A pretty printer is the lightweight option — it preserves token order and only adjusts whitespace between elements. Prettier with @prettier/plugin-xml or prettier-plugin-apex covers Apex (.cls / .trigger) and metadata XML, but the LWC HTML template is best handled by Prettier's built-in HTML printer. PMD with the Apex ruleset lints Apex and SOQL for code quality (avoid SOQL in loops, naming conventions, missing @TestVisible). ESLint with @salesforce/eslint-plugin-lwc lints LWC controllers (no-async-await, valid-wire, no-unknown-wire-adapters). Use this online tool for a one-off readability fix on a template.

More than LWC formatting

Format Angular, Vue, or Glimmer templates, or pretty-print HTML — all browser-side, all free.

Frequently Asked Questions

What are Lightning Web Components?

Lightning Web Components (LWC) is the modern Salesforce UI framework — a thin layer on top of standards-based Web Components (Custom Elements, Shadow DOM, ES Modules) plus a Salesforce-specific compiler. Each component is a folder of three files: a .html template, a .js controller class extending LightningElement, and a .css file. LWC replaced the older Aura framework as the recommended way to build Lightning Experience apps, Salesforce Mobile UI, and Experience Cloud sites since 2018. Templates use HTML extended with lwc:if, for:each, and {expressionBindings}.

How does this formatter differ from one for Vue or Angular?

Syntactically very little — all three are HTML supersets with framework-specific directives, and all indent the same way at the tag level. The differences are in the directive vocabulary and convention: LWC uses for:each / for:item / for:index instead of v-for or *ngFor; lwc:if / lwc:elseif / lwc:else (introduced in Spring 23) instead of v-if or *ngIf; expression bindings use {camelCase} braces (with no quotes around the value); event handlers use onclick / onchange / onkeyup directly on tags. The formatter handles all of these as ordinary HTML attributes.

Does it support both old (if:true) and new (lwc:if) directives?

Yes. The legacy if:true={isVisible} and if:false={isHidden} attributes are syntactically ordinary HTML, so they format the same way. The newer lwc:if / lwc:elseif / lwc:else directives also format normally. Salesforce recommends migrating to the new directives because they support else and elseif branches; the legacy if:true and if:false do not.

How are <template> tags handled when used as iteration scopes?

In LWC, <template for:each={items} for:item="item">...</template> is a virtual scope that does not render a wrapping element — it groups child templates for the iteration. The formatter treats it as an ordinary tag and indents children one level under it. The same goes for <template lwc:if={condition}>...</template> and the optional <template lwc:else>...</template> branch.

Does it preserve slot syntax for component composition?

Yes. <slot></slot>, named slots <slot name="header"></slot>, and slot content provided by a parent (<span slot="header">...</span>) are formatted as regular HTML elements with one tag per line and consistent indentation. The formatter never modifies attribute values or the slot name attribute.

Can it format the JavaScript controller (.js) of an LWC?

No — this tool focuses on the .html template. The component controller class extending LightningElement, with @api / @track / @wire decorators, getter methods, and event handlers, is plain JavaScript with decorators. Format it with the JavaScript Pretty Print or TypeScript Formatter (which handles decorators) instead.

Does it handle Salesforce Lightning Design System (SLDS) classes?

Yes. SLDS utility classes (slds-p-around_medium, slds-text-heading_small, slds-list_dotted) are ordinary CSS class names — the formatter preserves them in the class attribute exactly as written. Long class lists are kept on the same line as the host element; the formatter does not split attribute values across multiple lines.

Is the LWC template I paste sent to your servers?

No. Formatting runs entirely in your browser using JavaScript. Templates referencing internal Apex controllers, custom field API names, or org-specific record types never leave your device. Open DevTools → Network and click Format to verify no requests are made.

LWC Formatter — Lightning Web Components