Skip to content

Include

The include directive makes it easy to fetch and insert an external HTML fragment inside an element. The request is restricted to the same domain, protocol and port as the application.

WARNING

  • Only use on trusted content and never on dynamic/user-provided content!
  • Dynamically rendering HTML from third parties can easily lead to XSS vulnerabilities.
  • Executing untrusted JavaScript code poses significant security risks and should be strictly avoided.

INFO

The directive executes before any binding.

API Reference

Component attribute(s)

x-h-include

Arguments

AttributeTypeRequiredDescription
selfstringtrueRelative path to the HTML fragment.

Attributes

AttributeTypeRequiredDescription
data-jsbooleanfalseBy default, the directive does not execute any JavaScript code.
If the fragment includes a script that should execute, set this to true.

Modifiers

ModifierDescription
jsReplaced by data-js

Events

EventBubblesDetailDescription
fragment:loadedNo{ url }Dispatched on the element after the fragment is inserted into the DOM and Alpine has initialized the new tree.

Examples

html
<div x-h-include="'/harmonia/components/include/fragment.html'"></div>

Reacting after load

Because fragment:loaded does not bubble, attach the listener directly to the element:

html
<div x-h-include="'/harmonia/components/include/fragment.html'" @fragment:loaded="onFragmentLoaded($event.detail.url)"></div>

Or in plain JavaScript:

js
const el = document.querySelector('#my-include');
el.addEventListener('fragment:loaded', (e) => {
  console.log('Loaded:', e.detail.url);
});