Template directives are a special kind of HTML attribute available inside of any Astro component template (.astro files), and some can also be used in .mdx files.
Template directives are used to control an element or component’s behavior in some way. A template directive could enable some compiler feature that makes your life easier (like using class:list instead of class). Or, a directive could tell the Astro compiler to do something special with that component (like hydrating with client:load).
This page describes all of the template directives available to you in Astro, and how they work.
For a template directive to be valid, it must:
: in its name, using the form X:Y (ex: client:load).<X {...attr}> would not work if attr contained a directive).Some template directives, but not all, can take a custom value:
<X client:load /> (takes no value)<X class:list={['some-css-class']} /> (takes an array)A template directive is never included directly in the final HTML output of a component.
class:listclass:list={...} takes an array of class values and converts them into a class string. This is inspired by @lukeed’s popular clsx helper library, but built directly into Astro itself.
class:list takes an array of several different possible value kinds:
string: Added to the element class
Object: All truthy keys are added to the element class
Array: flattenedSet: flattenedfalse or null: skippedDuplicate values are removed automatically.
set:htmlset:html={string} injects an HTML string into an element, similar to setting el.innerHTML.
The value is not automatically escaped by Astro! Be sure that you trust the value, or that you have escaped it manually before passing it to the template. Forgetting to do this will open you up to Cross Site Scripting (XSS) attacks.
You can also use set:html on a <Fragment> to avoid adding an unnecessary wrapper element. This can be especially useful when fetching HTML from a CMS.
set:html={Promise<string>} injects an HTML string into an element that is wrapped in a Promise.
This can be used to inject HTML stored externally, such as in a database.
set:html={Promise<Response>} injects a Response into an element.
This is most helpful when using fetch(). For example, fetching old posts from a previous static-site generator.
set:html can be used on any tag and does not have to include HTML. For example, use with JSON.stringify() on a <script> tag to add a JSON-LD schema to your page.
set:textset:text={string} injects a text string into an element, similar to setting el.innerText. Unlike set:html, the string value that is passed is automatically escaped by Astro.
This is equivalent to just passing a variable into a template expression directly (ex: <div>{someText}</div>) and therefore this directive is not commonly used.
These directives control how UI Framework components are hydrated on the page.
By default, a UI Framework component is not hydrated in the client. If no client:* directive is provided, its HTML is rendered onto the page without JavaScript.
A client directive can only be used on a UI framework component that is directly imported into a .astro component. Hydration directives are not supported when using dynamic tags and custom components passed via the components prop.
client:loadLoad and hydrate the component JavaScript immediately on page load.
client:idleLoad and hydrate the component JavaScript once the page is done with its initial load and the requestIdleCallback event has fired. If you are in a browser that doesn’t support requestIdleCallback, then the document load event is used.
client:visibleLoad and hydrate the component JavaScript once the component has entered the user’s viewport. This uses an IntersectionObserver internally to keep track of visibility.
client:mediaclient:media={string} loads and hydrates the component JavaScript once a certain CSS media query is met.
client:onlyclient:only={string} skips HTML server-rendering, and renders only on the client. It acts similar to client:load in that it loads, renders and hydrates the component immediately on page load.
You must pass the component’s correct framework as a value! Because Astro doesn’t run the component during your build / on the server, Astro doesn’t know what framework your component uses unless you tell it explicitly.
Since Astro 2.6.0, integrations can also add custom client:* directives to change how and when components should be hydrated.
Visit the addClientDirective API page to learn more about creating a custom client directive.
These directives can only be used on HTML <script> and <style> tags, to control how your client-side JavaScript and CSS are handled on the page.
is:globalBy default, Astro automatically scopes <style> CSS rules to the component. You can opt-out of this behavior with the is:global directive.
is:global makes the contents of a <style> tag apply globally on the page when the component is included. This disables Astro’s CSS scoping system. This is equivalent to wrapping all of the selectors within a <style> tag with :global().
You can combine <style> and <style is:global> together in the same component, to create some global style rules while still scoping most of your component CSS.
📚 See the Styling & CSS page for more details about how global styles work.
is:inlineBy default, Astro will process, optimize, and bundle any <script> and <style> tags that it sees on the page. You can opt-out of this behavior with the is:inline directive.
is:inline tells Astro to leave the <script> or <style> tag as-is in the final output HTML. The contents will not be processed, optimized, or bundled. This limits some Astro features, like importing an npm package or using a compile-to-CSS language like Sass.
The is:inline directive means that <style> and <script> tags:
defer which control the loading of an external file will have no effect.import/@import/url() references resolved relative to the .astro file.📚 See how client-side scripts work in Astro components.
define:varsdefine:vars={...} can pass server-side variables from your component frontmatter into the client <script> or <style> tags. Any JSON-serializable frontmatter variable is supported, including props passed to your component through Astro.props. Values are serialized with JSON.stringify().
is:rawis:raw instructs the Astro compiler to treat any children of that element as text. This means that all special Astro templating syntax will be ignored inside of this component.
For example, if you had a custom Katex component that converted some text to HTML, you could have users do this:
© 2021 Fred K. Schott
Licensed under the MIT License.
https://docs.astro.build/en/reference/directives-reference/