FormatterHQ

LWC Formatter

Format Salesforce Lightning Web Components HTML templates without breaking unquoted {property} bindings, for:each loops or lwc:if directives.

Input HTML
Output HTML
Ready.

How to use the LWC Formatter

  1. Paste your LWC template (the component .html file) into the Input editor, or use Sample, Upload file or Load URL.
  2. Set Indent and Line width.
  3. Press Format to reprint the template with the LWC parser.
  4. Use Copy or Download above the output panel to take the formatted template with you.

Why LWC templates need their own formatter

An LWC formatter must understand one small but important difference from plain HTML: Lightning Web Components bind data with unquoted curly-brace expressions, as in value={searchKey}. A regular HTML formatter treats {searchKey} as a normal attribute value and wraps it in quotes, which turns the binding into a literal string and breaks the component. Prettier's lwc parser, used here, knows that braces in attribute values are expressions and leaves them unquoted.

Plain HTML formatting of the same snippet produces value="{searchKey}" and even tries to format the handler name as code; the LWC parser avoids both.

Example

<template><lightning-input label="Search" value={searchKey} onchange={handleChange}></lightning-input><template lwc:if={accounts.data}><template for:each={accounts.data} for:item="acc"><p key={acc.Id}>{acc.Name}</p></template></template></template>
<template
  ><lightning-input
    label="Search"
    value={searchKey}
    onchange={handleChange}
  ></lightning-input
  ><template lwc:if={accounts.data}
    ><template for:each={accounts.data} for:item="acc"
      ><p key={acc.Id}>{acc.Name}</p></template
    ></template
  ></template
>

The output looks unusual because the input had no whitespace between tags. Prettier treats custom elements as inline, so it will not add visible spaces between them; instead it breaks lines inside the tags. Put line breaks between elements in your source and the output becomes the familiar nested layout.

LWC syntax that is preserved

  • Unquoted bindings: title={cardTitle}, onclick={handleClick}.
  • Iteration: for:each with for:item and for:index, and iterator:it.
  • Conditionals: the legacy if:true / if:false and the newer lwc:if, lwc:elseif, lwc:else.
  • key attributes, lwc:ref, lwc:spread and slots.
  • Classic LWC bindings are simple property references such as {acc.Name} with no operators or function calls, so the formatter never needs to reflow them; computed values belong in getters in the JavaScript class.

Template errors formatting will not fix

The formatter reprints valid markup; it does not run the LWC compiler. These mistakes survive formatting and only show up when you deploy:

  • Quoted bindings. value="{searchKey}" is kept exactly as written. The compiler rejects it as an ambiguous attribute value: remove the quotes for a binding, or the braces for a literal string.
  • Missing keys in loops. Every element directly inside for:each or iterator needs a key bound to a unique value, such as key={acc.Id}.
  • Mixed conditionals. lwc:if and the legacy if:true cannot be combined on the same element, and lwc:elseif or lwc:else must directly follow an element with lwc:if.
  • No root template. The file must start with a single <template> element that wraps everything else.

Using the LWC formatter on a whole component bundle

A Lightning Web Component is a folder with several files: myComponent.html (the template), myComponent.js (the class that extends LightningElement), an optional myComponent.css, and myComponent.js-meta.xml, which declares targets such as lightning__RecordPage. Only the template needs the LWC parser. The meta file is plain XML and can be tidied with the XML Formatter; the stylesheet is ordinary CSS.

Tips

Salesforce DX project templates ship with a Prettier configuration, and teams differ between 2- and 4-space indentation; pick whichever your repository's .prettierrc uses so the output matches your commits. The component's JavaScript controller can be formatted with JavaScript Pretty Print. For Aura or Visualforce markup, which use different expression syntax ({!v.value}), the HTML Formatter is a safer choice. Your markup is processed in the browser only.

Frequently asked questions

Why can’t I use a normal HTML formatter for LWC?
HTML formatters quote attribute values, turning value={name} into value="{name}". In LWC that makes the binding a plain string, so the component stops working. The LWC parser keeps braces unquoted.
Does it support lwc:if, lwc:elseif and lwc:else?
Yes. These directives are ordinary attributes to the parser, and their brace expressions are kept unquoted like any other binding.
Why does the output have closing brackets at the start of lines?
Custom elements are treated as whitespace-sensitive inline elements. When your input has no spaces between tags, Prettier breaks lines inside the tags to avoid adding visible whitespace.
Is this the same setup as the Salesforce Prettier configuration?
Salesforce DX projects configure Prettier with the lwc parser for HTML files in lwc folders, which is the parser this tool uses. Your project may set additional options in .prettierrc.

Last updated September 26, 2026

Related tools