@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

Parameter
Required
Description

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

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:

Build complex navigation with recursive includes:

Recursive Includes

For hierarchical data like nested menus, templates can include themselves:

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:

  1. All parent template properties - Properties from the main template are accessible

  2. Passed properties - Properties explicitly passed in the include statement

  3. Loop context - When inside an @each loop, the loop variable is accessible

Best Practices

  1. Extract reusable patterns - If you find yourself repeating markup, extract it into an include.

  2. Use meaningful file names - Name include files after their purpose: button.html, menu_item.html, lightbox.html.

  3. Pass only what's needed - Pass specific properties rather than relying on parent scope when possible.

  4. Use @includeIf for conditionals - It's cleaner than wrapping includes in if statements.

  5. Organize complex includes - For components with many includes, consider subdirectories within include/.

  • @template - For inline template definitions

  • @if - For conditional rendering

  • @each - For iterating over collections

Last updated

Was this helpful?