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.html

How 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 relative to the templates/include/ directory.

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.json

  • Data passed from hooks.js via rw.setProps()

  • Built-in properties like id, edit, preview

  • Parameters 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

The Gallery component uses 10+ include files to organize different media types and UI elements:

Usage in templates/index.html:

The Navbar component organizes desktop and mobile variations:

Usage pattern:

Container Component

The Container component uses includes for different background types:

Usage with conditionals:

Accordion Component

Simple icon inclusion with fallback:

Organization Strategies

By Feature

Group related includes by feature or functionality:

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.

Last updated

Was this helpful?