Works with
•JSR Score47%•It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers




Publisheda year ago (0.1.6)
Simple Template Engine
Usage
1. Change the defaultView to @candy/template/View
// main.ts import App from '@candy/framework/web/Application'; import Main from '@candy/framework'; import View from '@candy/template/View'; const app = new App({ id: 'demo', appPath: Deno.cwd() + '/app', defaultView: View, });
2. Render a template in controller
// app/controllers/index/IndexController.ts import type HttpRequest from '@candy/framework/http/HttpRequest'; import HttpResponse from '@candy/framework/http/HttpResponse'; import Controller from '@candy/framework/web/Controller'; export default class IndexController extends Controller { public override async run(_request: HttpRequest): Promise<HttpResponse> { const posts = [ { id: 1, title: 'Hello' }, { id: 2, title: 'Candy' }, ]; const html = await this.render('home', { posts, }); return HttpResponse.fromHTML(html); } }
3. Write the template file
// app/views/index/home.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Demo</title> </head> <body> <!-- $data is the second parameter passed to render() method --> <% for(const v of $template.$data.posts){ %> <h2><%= v.title %></h2> <% } %> </body> </html>
Add Package
deno add jsr:@candy/template
Import symbol
import * as template from "@candy/template";
Import directly with a jsr specifier
import * as template from "jsr:@candy/template";
Add Package
pnpm i jsr:@candy/template
pnpm dlx jsr add @candy/template
Import symbol
import * as template from "@candy/template";
Add Package
yarn add jsr:@candy/template
yarn dlx jsr add @candy/template
Import symbol
import * as template from "@candy/template";
Add Package
vlt install jsr:@candy/template
Import symbol
import * as template from "@candy/template";
Add Package
npx jsr add @candy/template
Import symbol
import * as template from "@candy/template";
Add Package
bunx jsr add @candy/template
Import symbol
import * as template from "@candy/template";