Hierarchy

  • TemplatePowers

Index

Methods

Methods

Static render

  • render<T>(template: string, objectModel: T): Promise<string>
  • Renders a html string using a template and data. Templates are based on the Handlebars template language and currently support all the builtin aspects except the abilities to use Block and Custom Helpers.

    throws

    {TypeError} When template is missing.

    throws

    {TypeError} When objectModel is missing.

    throws

    {Error} When unable to render html

    example

    The following example uses the template expression {{report}} which generates an html table with headers and rows. It just requires that the data being fed to the power is shaped accordingly. If the root object property name is not passed the expression will assume that the header and row properties are in the root of the data model. In this example the parameter reportOne was passed in {{report reportOne}}. The render method returns a html string compiled using template and data.

    
    import { TemplatePowers } from "@andi/powers";
    
    export async function generateHtml(): Promise<string> {
         const htmlContent: string = await TemplatePowers.render(template, data);
         return htmlContent;
    }
    
    const data = {
        reportOne: {
             header: [{name: "Name"},
                      {name: "Order"},
                      {name: "Price"}],
             rows: [
                       ["Alice","4 apples","$10"],
                       ["Bob","5 bananas", "$5"],
                       ["Carl", "30 cherries", "$8"]
                  ]
         }
    };
    
    const template = `
    <html lang="en">
      <head>
          <meta charset="utf-8">
          <title>Print Test</title>
          <style>
            .h {
              border-bottom: 1px solid black;
              padding-bottom: 8px;
            }
            .s {
              margin-top: 4px;
              margin-bottom: 8px;
            }
            .p {
              border: 1px solid black;
              border-radius: 4px;
              padding: 4px;
            }
            .print-report {
              border-collapse: collapse;
            }
            td {
              border: 1px solid black;
              padding: 4px;
            }
            @media print {
              .h {
                display: none;
              }
            }
          </style>
    
      </head>
      <body>
        <div class="s">
          This is an example print out.
        </div>
        <div class="s">
           {{report "reportOne"}}
        </div>
      </body>
    `;
    

    Type parameters

    • T = unknown

    Parameters

    • template: string

      Template to transform objectModel utilizing handlebar.js format

    • objectModel: T

      Data being processed by the template

    Returns Promise<string>

    html string

Generated using TypeDoc