Skip to content

TemplateFile

A component that reads a template file and replaces variable placeholders with actual values.

Template files can contain variable placeholders in the format {{ variable_name }} which will be replaced with values from TemplateVariable children components. Whitespace around variable names is ignored, so {{ name }}, {{name}}, and {{ name }} are all equivalent.

import { TemplateFile } from "@alloy-js/core";
<TemplateFile path="string" src="string">
{children}
</TemplateFile>
childrenoptional ChildrenTemplate variable children components.
pathstringThe path to write the compiled template to.
srcstringThe file path of the template.

Basic usage with template variables:

// Template file content (greeting.txt):
// "Hello {{ name }}! You are {{ age }} years old."

<TemplateFile src="greeting.txt" path="output.txt">
  <TemplateVariable name="name" value="John" />
  <TemplateVariable name="age" value="25" />
</TemplateFile>

Using children instead of value prop:

// Template file content (welcome.txt):
// "Welcome {{ greeting }}!"

<TemplateFile src="welcome.txt" path="output.txt">
  <TemplateVariable name="greeting">Hello World</TemplateVariable>
</TemplateFile>

Complex template with multiple variables:

// Template file content (profile.txt):
// "Name: {{ name }}\nAge: {{ age }}\nLocation: {{ location }}"

<TemplateFile src="profile.txt" path="profile-output.txt">
  <TemplateVariable name="name" value="Alice" />
  <TemplateVariable name="age">30</TemplateVariable>
  <TemplateVariable name="location" value="New York" />
</TemplateFile>