Skip to main content
Home

latest
Works with
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score47%
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>

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@candy/template

Import symbol

import * as template from "@candy/template";
or

Import directly with a jsr specifier

import * as template from "jsr:@candy/template";

Add Package

pnpm i jsr:@candy/template
or (using pnpm 10.8 or older)
pnpm dlx jsr add @candy/template

Import symbol

import * as template from "@candy/template";

Add Package

yarn add jsr:@candy/template
or (using Yarn 4.8 or older)
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";