edge.js

  • Version 6.3.0
  • Published
  • 121 kB
  • 12 dependencies
  • MIT license

Install

npm i edge.js
yarn add edge.js
pnpm add edge.js

Overview

Template engine

Index

Variables

variable edge

const edge: Edge;

    variable edgeGlobals

    const edgeGlobals: EdgeGlobals;
    • Inbuilt globals

    Classes

    class Edge

    class Edge {}
    • Exposes the API to render templates, register custom tags and globals

    constructor

    constructor(options?: EdgeOptions);

      property asyncCompiler

      asyncCompiler: Compiler;
      • The underlying compiler in use

      property compat

      compat: boolean;
      • A flag to know if using compat mode

      property compiler

      compiler: Compiler;
      • The underlying compiler in use

      property globals

      globals: EdgeGlobals;
      • Globals are shared with all rendered templates

      property loader

      loader: LoaderContract;
      • The loader to load templates. A loader can read and return templates from anywhere. The default loader reads files from the disk

      property processor

      processor: Processor;
      • Reference to the registered processor handlers

      property tags

      tags: { [name: string]: TagContract };
      • List of registered tags. Adding new tags will only impact this list

      method configure

      configure: (options: EdgeOptions) => void;
      • Re-configure an existing edge instance

      method create

      static create: (options?: EdgeOptions) => Edge;
      • Create an instance of edge with given options

      method createRenderer

      createRenderer: () => EdgeRenderer;
      • Returns a new instance of edge. The instance can be used to define locals.

      method global

      global: (name: string, value: any) => this;
      • Add a new global to the edge globals. The globals are available to all the templates.

        edge.global('username', 'virk')
        edge.global('time', () => new Date().getTime())

      method mount

      mount: {
      (viewsDirectory: string | URL): this;
      (diskName: string, viewsDirectory: string | URL): this;
      };
      • Mount named directory to use views. Later you can reference the views from a named disk as follows.

        edge.mount('admin', join(__dirname, 'admin'))
        edge.render('admin::filename')

      method onRender

      onRender: (callback: (renderer: EdgeRenderer) => void) => this;
      • Get access to the underlying template renderer. Each render call to edge results in creating an isolated renderer instance.

      method registerTag

      registerTag: (tag: TagContract) => this;
      • Add a new tag to the tags list.

        edge.registerTag('svg', {
        block: false,
        seekable: true,
        compile (parser, buffer, token) {
        const fileName = token.properties.jsArg.trim()
        buffer.writeRaw(fs.readFileSync(__dirname, 'assets', `${fileName}.svg`), 'utf-8')
        }
        })

      method registerTemplate

      registerTemplate: (templatePath: string, contents: LoaderTemplate) => this;
      • Register an in-memory template.

        edge.registerTemplate('button', {
        template: `<button class="{{ this.type || 'primary' }}">
        @!yield($slots.main())
        </button>`,
        })

        Later you can use this template

        @component('button', type = 'primary')
        Get started
        @endcomponent

      method removeTemplate

      removeTemplate: (templatePath: string) => this;
      • Remove the template registered using the "registerTemplate" method

      method render

      render: (templatePath: string, state?: Record<string, any>) => Promise<string>;
      • Render a template with optional state

        edge.render('welcome', { greeting: 'Hello world' })

      method renderRaw

      renderRaw: (
      contents: string,
      state?: Record<string, any>,
      templatePath?: string
      ) => Promise<string>;
      • Render a template with optional state

        edge.render('welcome', { greeting: 'Hello world' })

      method renderRawSync

      renderRawSync: (templatePath: string, state?: Record<string, any>) => string;
      • Render a template asynchronously with optional state

        edge.render('welcome', { greeting: 'Hello world' })

      method renderSync

      renderSync: (templatePath: string, state?: Record<string, any>) => string;
      • Render a template asynchronously with optional state

        edge.render('welcome', { greeting: 'Hello world' })

      method share

      share: (data: Record<string, any>) => EdgeRenderer;
      • Share locals with the current view context.

        const view = edge.createRenderer()
        // local state for the current render
        view.share({ foo: 'bar' })
        view.render('welcome')

      method unmount

      unmount: (diskName: string) => this;
      • Un Mount a disk from the loader.

        edge.unmount('admin')

      method use

      use: <T extends unknown>(pluginFn: PluginFn<T>, options?: T) => this;
      • Register a plugin. Plugins are called only once just before a rendering a view.

        You can invoke a plugin multiple times by marking it as a recurring plugin

      class Template

      class Template extends Macroable {}
      • The template is used to compile and run templates. Also the instance of template is passed during runtime to render dynamic partials and dynamic components.

      constructor

      constructor(compiler: Compiler, globals: any, locals: any, processor: Processor);

        property stacks

        stacks: Stacks;
        • Template stacks holds a collection of placeholders and their content to be filled before returning the output.

        method compileComponent

        compileComponent: (templatePath: string) => CompiledTemplate;
        • Render a component

          const componentFn = template.compileComponent('components/button')
          // render and use output
          componentFn(template, template.getComponentState(props, slots, caller), ctx)

        method compilePartial

        compilePartial: (
        templatePath: string,
        ...localVariables: string[]
        ) => CompiledTemplate;
        • Render a partial

          const partialFn = template.compilePartial('includes/user')
          // render and use output
          partialFn(template, state, ctx)

        method createError

        createError: (
        errorMessage: string,
        filename: string,
        lineNumber: number,
        column: number
        ) => EdgeError;
        • Creates an instance of the EdgeError

        method escape

        escape: (input: any) => string;
        • Escapes the value to be HTML safe. Only strings are escaped and rest all values will be returned as it is.

        method getComponentState

        getComponentState: (
        props: { [key: string]: any },
        slots: { [key: string]: any },
        caller: { filename: string; line: number; col: number }
        ) => {
        $slots: { [key: string]: any };
        $caller: { filename: string; line: number; col: number };
        $props: ComponentProps | Props;
        };
        • Returns the isolated state for a given component

        method newError

        newError: (
        errorMessage: string,
        filename: string,
        lineNumber: number,
        column: number
        ) => void;
        • Throws EdgeError. Use "createError" to create a new error instance

        method render

        render: <T extends string | Promise<string>>(template: string, state: any) => T;
        • Render a template with it's state.

          template.render('welcome', { key: 'value' })

        method renderRaw

        renderRaw: <T extends string | Promise<string>>(
        contents: string,
        state: any,
        templatePath?: string
        ) => T;
        • Render template from a raw string

          template.renderRaw('Hello {{ username }}', { username: 'virk' })

        method reThrow

        reThrow: (error: any, filename: string, lineNumber: number) => never;
        • Rethrows the runtime exception by re-constructing the error message to point back to the original filename

        Interfaces

        interface Template

        interface Template {}

          property loop

          loop: typeof each;

            property loopAsync

            loopAsync: typeof asyncEach;

              property size

              size: (typeof lodash)['size'];

                interface Template

                interface Template {}

                  property stackSources

                  stackSources: Record<string, boolean>;

                    method trackStackSource

                    trackStackSource: (
                    stack: string,
                    filename: string,
                    line: string,
                    col: string
                    ) => boolean;

                      interface Template

                      interface Template {}

                        property setValue

                        setValue: (typeof lodash)['set'];

                          Package Files (7)

                          Dependencies (12)

                          Dev Dependencies (20)

                          Peer Dependencies (0)

                          No peer dependencies.

                          Badge

                          To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                          You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/edge.js.

                          • Markdown
                            [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/edge.js)
                          • HTML
                            <a href="https://www.jsdocs.io/package/edge.js"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>