Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@
<header>
<h1>micro-template.js</h1>
<p class="subtitle">A minimal, blazing fast JavaScript template engine for hackers.</p>
<div style="margin:1em 0 0.5em 0;">
<img src="https://img.shields.io/npm/v/micro-template.svg?style=flat-square" alt="npm version"
style="vertical-align:middle; margin-right:8px;">
<img src="https://img.shields.io/github/stars/cho45/micro-template.js?style=flat-square&label=GitHub+Stars"
alt="GitHub stars" style="vertical-align:middle; margin-right:8px;">
<img src="https://img.shields.io/npm/l/micro-template.svg?style=flat-square" alt="MIT License"
style="vertical-align:middle;">
</div>
</header>
<section class="features">
<h2>Features</h2>
Expand All @@ -177,16 +185,8 @@ <h2>Features</h2>
</ul>
</section>
<section class="usage">
<h2>Usage Example</h2>
<h2>Getting Started</h2>
<div class="usage-blocks">
<div class="usage-block">
<h3>Basic</h3>
<pre><code class="language-js">import { template } from 'micro-template';

const result = template('&lt;div&gt;&lt;%= message %&gt;&lt;/div&gt;', { message: 'Hello, inline!' });
console.log(result); // &lt;div&gt;Hello, inline!&lt;/div&gt;
</code></pre>
</div>
<div class="usage-block">
<h3>In HTML</h3>
<pre><code class="language-html">&lt;script type="application/x-template" id="tmpl1"&gt;
Expand All @@ -200,6 +200,7 @@ <h3>In HTML</h3>
</div>
<div class="usage-block">
<h3>In Node.js</h3>
<pre><code>npm install micro-template</code></pre>
<pre><code class="language-js">import fs from 'node:fs';
import { template } from 'micro-template';

Expand Down
14 changes: 7 additions & 7 deletions lib/micro-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ const template = function (id, data) {
let line = 1;
const body = (
`try {` +
(me.variable ? `let ${me.variable}=__this.stash;` : ``) +
(me.variable ? `let ${me.variable}=__this.stash;__this.ret+=\`` : `__this.ret+=\``) +
string.trim().split(/(<%.+?%>)/g).map(part =>
part.startsWith('<%=raw') && part.endsWith('%>') ? `/*raw*/__this.ret+=(${part.slice(6, -2)});` :
part.startsWith('<%=') && part.endsWith('%>') ? `/*=*/__this.ret+=__this.escapeHTML(${part.slice(3, -2)});` :
part.startsWith('<%') && part.endsWith('%>') ? `/* */${part.slice(2, -2)};` :
part.startsWith('<%=raw') && part.endsWith('%>') ? `\${/*raw*/(${part.slice(6, -2)})}` :
part.startsWith('<%=') && part.endsWith('%>') ? `\${/*=*/__this.escapeHTML(${part.slice(3, -2)})}` :
part.startsWith('<%') && part.endsWith('%>') ? `\`;/* */${part.slice(2, -2)};__this.ret+=\`` :
(
part = part.replace(/\\/g, "\\\\")
.replace(/'/g, "\\'")
.replace(/\n|\r\n/g, () => `';\n__this.line=${++line};__this.ret+='\\n`),
part ? `/*+*/__this.ret+='${part}';` : ''
.replace(/\n|\r\n/g, () => `\n\${__this.line=${++line},''}`),
part ? part : ''
)
).join('') +
`/*end*/ return __this.ret;` +
`\`;/*end*/ return __this.ret;` +
`} catch (e) { throw new Error('TemplateError: '+e+' (on ${name} line ' + __this.line + ')',{cause:e}); }` +
`//# sourceURL=${name}\n` +
`//# sourceMappingURL=data:application/json,${encodeURIComponent(JSON.stringify({version:3, file:name, sources:[`${name}.ejs`], sourcesContent:[string], mappings:";;AAAA;"+Array(line-1).fill('AACA').join(';')}))}`
Expand Down