@cmorales_/vite-plugin-html-template is a plugin for vite and mainly for 'Vanilla' (without specific framework, like React.js, Vue, Svelte, ...), with which you can define within the <template> tag as a component, then you can use the <x-template> tag (customizable) and also add HTML attributes (class, data-*, style, aria-*)
bun add @cmorales_/vite-plugin-html-template
# or npm install ... or pnpm add ... or yarn add ...
export default defineConfig({ plugins: [/*...plugins */ htmlTemplate({...})], //...vite config
})
## Usage
1. In your `HTML` define a `<template>` element with an unique id.
> [!WARNING]
> 1. The element `<template>` must have at least and at most *1* element.
> 2. The attributes should be added to the element within the `<template>` tag and not to the `<template>` element itself.
```html
<!-- You should add attributes to the element within the <template> element -->
<template id="unique-id">
<div class="..." ...other-attributes></div>
</template>
<x-template> tag.[!WARNING] You need add the
data-template-idattribute to the<x-template>element referencing the same id as the<template>element to be used.
<x-template data-template-id="template-id" class="..." style="..." ></x-template>
You can inject dynamic values into your templates using placeholders.
To define a placeholder, use curly braces inside the template:
<template id="card">
<div class="card">
<h2>{title}</h2>
<p>{description}</p>
</div>
</template>
Then, pass values using data-* attributes in <x-template>:
<x-template
data-template-id="card"
data-title="Hello world"
data-description="This is a description"
></x-template>
Result
<div class="card">
<h2>Hello world</h2>
<p>This is a description</p>
</div>
Attributes passed to <x-template> are merged into the root element of the template.
<x-template
data-template-id="card"
class="highlight"
style="color:red;"
></x-template>
Result
<div class="card highlight" style="color:red;">
You can use the special placeholder {children}
<x-template data-template-id="foo">
<div> <!-- some children --> </div>
</x-template>
<template id="foo">
<header>{children}</header>
</template>
Result
<header>
<div> <!-- some children --> </div>
</header>
<!-- You can use any html element -->
<x-template data-template-id="foo" data-text="Open Web" data-icon="<svg>...</svg>" ></x-template>
<template id="foo">
<button type="button">
{icon}
<span>{text}</span>
</button>
</template>
Result
<button>
<svg>...</svg>
<span>Open Web</span>
</button>
You can pass any valid HTML attribute:
classstyledata-*aria-*etc.[!NOTE]
- Placeholder names must match the data-* attributes without the data- prefix.
- If a placeholder is not provided, it will remain unchanged.
| Option | Type | Description | Default |
|---|---|---|---|
| tag | string | Custom tag name used to render template instances instead of <x-template> |
"x-template" |
[!NOTE] You need to have installed NodeJS
git clone https://github.com/Cristian-F-M/vite-plugin-html-template.git
npm install
# or bun install or pnpm install or ...
npm run test
# or bun run test or pnpm run test or ...
[!NOTE]
- You can add more test if your new features need it
- Ensure you pass all test before create a pull request
- Change the version in package.json file according to the changes (major, minor, patch)
npm version patch # minor or major # bun pm version patch # minor or major # ...
Created by Cristian Morales.
If you find this tool useful, you can support my work!