prevue
An HTML templating engine that uses Vue's template syntax. Parses HTML, evaluates inline JavaScript expressions, and returns rendered output.
Installation
[]
= "0.0.3"
API
Example
use render;
use json;
let html = r#"
<div>
<a :id="id">link</a>
<p v-if="user.age >= 18">{{ user.name }} is adult</p>
<ul>
<li v-for="item in list">{{ item }}</li>
</ul>
</div>
"#.to_string;
let data = json!;
let output = render.unwrap;
// <html><head></head><body><div>
// <a id="link-id">link</a>
// <p>James is adult</p>
// <ul>
// <li>a</li>
// <li>b</li>
// <li>c</li>
// </ul>
// </div>
// </body></html>
Features
| Syntax | Status | Notes |
|---|---|---|
{{ }} |
🟡 | Array/Object stringify without spacing (e.g., [1,2,3] not [ 1, 2, 3 ]) |
<template> |
✅ | |
v-bind, :attr |
🟡 | No class/style object binding |
v-if |
✅ | |
v-else |
✅ | |
v-else-if |
✅ | |
v-for |
🟡 | Array and Object only |
v-text, v-html |
❌ | |
v-pre |
✅ |
Important Notes
HTML5 Parsing
This library uses html5ever, which follows HTML5 spec strictly:
- Attribute names are lowercased (e.g.,
:MyAttr→:myattr) - Dynamic bindings are lowercased:
:[dynamicKey]looks updynamickeyvariable - Outputs complete HTML document with
<html>,<head>,<body>tags
JavaScript Evaluation
This library uses a Boa JavaScript engine to evaluate expressions.
- ⚠️ Security: Never use untrusted templates or data.
- Evaluation Behavior: Unlike Vue, which restricts each binding to a single expression, prevue currently allows both expressions and statements in all binding contexts (e.g.,
{{ let x = 1; x + 1 }}→2). This may change in future versions to match Vue's behavior. - Variable Access: Accessing undefined variables will cause the entire expression evaluation to fail, rather than returning
undefined. Always ensure that variables exist in the provided data. thisContext: Whilethisis accessible in the JavaScript engine context, its behavior may vary due to internal optimizations, and access is restricted in the template engine context. Therefore, usingthisis not recommended.
License
MIT