Include Directory
Organize reusable template partials in the include directory
The templates/include/ directory is where you store reusable template partials that can be included in other template files using the @include() directive. This promotes code reuse, improves maintainability, and helps organize complex components.
Overview
Include files allow you to:
Break complex templates into smaller, manageable pieces
Reuse common markup across multiple templates
Keep your main templates clean and focused
Organize related functionality into logical groups
Directory Location
com.yourcompany.component/
├── templates/
│ ├── index.html
│ └── include/ # Include directory
│ ├── header.html
│ ├── footer.html
│ ├── icon.html
│ └── card.htmlHow It Works
Include files are referenced using the @include() directive from any template file:
The include filename is specified without the .html extension and is resolved relative to the templates/include/ directory.
@include() only resolves files in templates/include/ — it does not resolve root-level template files, and a call that doesn't match a file fails silently. Root-level templates never need including: every file at the root of templates/ is processed automatically. (You may spot @include("alpine") in some core components — those calls are silent no-ops; the root-level alpine.html is included by the automatic processing.)
Creating Include Files
Include files are standard HTML template files that can use the full Elements Language syntax:
Include files have access to:
All component properties from
properties.jsonData passed from
hooks.jsviarw.setProps()Built-in properties like
id,edit,previewParameters passed from the
@include()call
Passing Parameters
You can pass additional parameters to includes:
Parameters override properties with the same name within the include's scope.
Conditional Includes
Use @includeIf() to conditionally include a template:
See the @include directive documentation for complete syntax details.
Examples from Core Components
Gallery Component
The Gallery component uses 10+ include files to organize different media types and UI elements:
Usage in templates/index.html:
Navbar Component
The Navbar component organizes desktop and mobile variations:
Usage in templates/index.html — the logo area picks an include based on the inspector settings, and the mobile menu branches per page type:
Container Component
The Container component uses includes for different background types:
Usage with conditionals. Template @if accepts a single condition only, so the Container computes a bgVideo object with format booleans in hooks.js and uses @includeIf for each branch (the hooks below are simplified — the real Container derives these from its background controls). See Combining Conditions.
From templates/index.html:
Note that @includeIf conditions can test nested properties like bgVideo.isYoutube — only the top-level combining of conditions needs to happen in hooks.js.
Accordion Component
Simple icon inclusion with fallback:
Nav Tree Component (Recursive Includes)
An include can include itself, which is how the Nav Tree component renders nested pages to any depth. The loop variable is passed down explicitly as a parameter each time:
Each level of nesting re-enters item.html with the child page bound to page, so the same markup handles the whole tree. See Recursive Includes in the directive reference.
Organization Strategies
The core components keep the include/ directory flat and group related partials with filename prefixes, as the Navbar does with its desktop_* and mobile_* files:
By Component Part
Organize by component sections:
By Variation
Create variations of similar elements:
Best Practices
Keep Includes Focused
Each include should have a single, clear purpose:
Good:
Less ideal:
Use Descriptive Names
Choose names that clearly indicate the include's purpose:
Pass Explicit Parameters
When reusing includes with different values, pass parameters explicitly:
This makes the template more readable and maintainable.
Avoid Deep Nesting
While includes can include other includes, avoid deep nesting chains:
Too many levels make the template flow hard to follow.
Document Complex Includes
Add comments for includes that expect specific parameters:
Include vs Inline Template
Choose between includes and inline templates based on your needs:
Use Includes When:
The template is reused across multiple files
You want to keep files organized in separate files
The template is complex and benefits from separation
Use Inline Templates When:
The template is only used once in a single file
You want to keep related code together
The template is simple and short
See the @template directive documentation for inline templates.
Related Documentation
@includeDirective - Complete@includesyntax@includeIfDirective - Conditional includes@templateDirective - Inline templatesElements Language - Template syntax reference
index.html - Main template file
Last updated
Was this helpful?

