@include
Include content from another template file
The @include directive allows you to insert the content of another template file within the current template. This promotes code reuse, keeps templates organized, and makes complex components more maintainable.
Syntax
@include("templateName")With parameters:
@include("templateName", property1: value1, property2: value2)Parameters
template
Yes
The name of the template file to include (without .html extension)
...properties
No
Additional properties to pass to the included template
File Location
Include files should be stored in the templates/include/ directory within your component:
com.yourcompany.component/
├── templates/
│ ├── index.html
│ └── include/
│ ├── header.html
│ ├── footer.html
│ └── item.html@include() resolves only files in templates/include/ — root-level template files can't be included by name, and a call that doesn't match a file in include/ fails silently. Root-level templates never need including: every file at the root of templates/ is processed automatically (see Templates).
Examples
Basic Include
Include a template file:
If header.html contains Hello and footer.html contains World, the output would be:
Passing Properties
Pass data to the included template:
In the button.html template, you can access these as {{label}} and {{style}}.
String and Variable Properties
Pass both literal strings and variable values:
Passing Loop Items
Pass the current loop item to an include:
Navigation with Nested Includes
Build complex navigation with recursive includes:
Recursive Includes
For hierarchical data like nested menus, templates can include themselves:
Gallery Thumbnails
Extract repeated item markup into includes:
Conditional Includes
Using @includeIf
Rather than wrapping your @include statement inside an @if statement, use the @includeIf helper for cleaner syntax:
Negating the Condition
You can negate the condition with !:
Real-World Examples
Include a lightbox only when enabled:
Include video templates based on video type:
Include icon only when no custom icon is set:
Include submenu indicators for pages with children:
Property Access in Includes
Included templates have access to:
All parent template properties - Properties from the main template are accessible
Passed properties - Properties explicitly passed in the include statement
Loop context - When inside an
@eachloop, the loop variable is accessible
Best Practices
Extract reusable patterns - If you find yourself repeating markup, extract it into an include.
Use meaningful file names - Name include files after their purpose:
button.html,menu_item.html,lightbox.html.Pass only what's needed - Pass specific properties rather than relying on parent scope when possible.
Use @includeIf for conditionals - It's cleaner than wrapping includes in if statements.
Organize complex includes - For components with many includes, group related partials with filename prefixes (e.g.
desktop_*,mobile_*), as the core Navbar component does.
Related
Last updated
Was this helpful?

