UNPKG

@athenna/view

Version:

The Athenna template engine. Built on top of Edge.js.

294 lines (293 loc) 8.18 kB
/** * @athenna/view * * (c) João Lenon <lenon@athenna.io> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ import { Edge } from 'edge.js'; import type { TagContract } from '#src/types/TagContract'; import { Macroable } from '@athenna/common'; export declare class ViewImpl extends Macroable { /** * Edge instance that is handling all the views. */ edge: Edge; constructor(); /** * Render some view with optional data included. * * @example * ```ts * View.render('welcome', { greeting: 'Hello world' }) * ``` */ render(template: string, data?: any): Promise<string>; /** * Render some view asynchronously with optional data included. * * @example * ```ts * View.renderSync('welcome', { greeting: 'Hello world' }) * ``` */ renderSync(template: string, data?: any): string; /** * Render some raw-edge content with optional data included. * * @example * ```ts * View.renderRaw('Hello {{ value }}', { value: 'World!' }) * ``` */ renderRaw(content: string, data?: any): Promise<string>; /** * Render some raw-edge content asynchronously with optional * data included. * * @example * ```ts * View.renderRawSync('Hello {{ value }}', { value: 'World!' }) * ``` */ renderRawSync(content: string, data?: any): string; /** * Render some raw-edge file content with optional data included. * * @example * ```ts * View.renderRawByPath(Path.views('hello.edge'), { value: 'World!' }) * ``` */ renderRawByPath(path: string, data?: any): Promise<string>; /** * Render some raw-edge file content asynchronously with optional * data included. * * @example * ```ts * View.renderRawByPathSync(Path.views('hello.edge'), { value: 'World!' }) * ``` */ renderRawByPathSync(path: string, data?: any): string; /** * Add a new property to templates. The properties registered * here will be available to all the templates. You can use * this method to update properties too. * * @example * ```ts * View * .addProperty('usernameOne', 'txsoura') * .addProperty('usernameTwo', 'jlenon7') * .addProperty('time', () => new Date().getTime()) * ``` */ addProperty(key: string, value: any): ViewImpl; /** * Remove some property from views registered using * "addProperty" method. * * @example * ```ts * View * .addProperty('testing', '') * .removeProperty('testing') * ``` */ removeProperty(key: string): ViewImpl; /** * Add a new tag to templates. Just like @component * @if, etc. * * @example * ```ts * import type { TagContract } from '@athenna/view' * * const reverseTagOptions: TagContract = { * block: false, * seekable: true, * compile(parser, buffer, token) { * buffer.outputRaw('Hello from reverse tag') * } * } * * View.addTag('reverse', reverseTagOptions) * * const output = await View.renderRaw('@reverse()') // 'Hello from reverse tag' * ``` */ addTag(name: string, options: TagContract): this; /** * Remove some tag from views registered using * "addTag" method. * * @example * ```ts * View * .addTag('reverse', { ... }) * .removeTag('reverse') * ``` */ removeTag(name: string): this; /** * Create a new view disk. View disks can be used * to register multiple views at the same time. * * Imagine these three paths: * * resources/views/admin/listUsers.edge\ * resources/views/admin/createUser.edge\ * resources/views/admin/details/listUserDetails.edge * * @example * ```ts * View.createViewDisk(Path.views()) * View.createViewDisk('admin', Path.views('admin')) * * const users = [...] * * View.render('admin/listUsers', { users }) * View.render('admin::listUsers', { users }) * * View.render('admin/createUser') * View.render('admin::createUser') * * View.render('admin/details/listUserDetails', { users }) * View.render('admin::details/listUserDetails', { users }) * ``` */ createViewDisk(name: string, path?: string): ViewImpl; /** * Delete a view disk that was registered using * the "createViewDisk" method. * * @example * ```ts * View * .createViewDisk('admin', Path.views('admin')) * .removeViewDisk('admin') * ``` */ removeViewDisk(name: string): ViewImpl; /** * Verify if some view disk exists. * * @example * View.createViewDisk('testing', Path.views('testing')) * * View.hasViewDisk('testing') // true * View.hasViewDisk('testing::subTesting') // true * View.hasViewDisk('testing::subTesting/notFound') // false */ hasViewDisk(name: string): boolean; /** * Create an in-memory component. * * @example * ```ts * View.createComponent('button', '<button class="{{ this.type }}">@!yield($slots.main())</button>') * ``` * * In-memory components could be used this * way (Ignore the "\\" value in the example): * * @example * ```edge * \@component('button', type = 'primary') * Get started * \@endcomponent * ``` */ createComponent(name: string, component: string): ViewImpl; /** * Same as "createComponent" method but create the template by the path * instead. If the file path does not exist, an error will throw. * * @example * ```ts * const path = Path.resources('views/myTemplate.edge') * * View.createComponentByPath('myTemplate', path) * ``` */ createComponentByPath(name: string, path: string): ViewImpl; /** * Verify if some component exists. * * @example * View * .createComponent('testing', '') * .hasComponent('testing') // true */ hasComponent(name: string): boolean; /** * Delete the component created using the "createComponent" * method. * * @example * View * .createComponent('testing', '') * .removeComponent('testing') */ removeComponent(name: string): ViewImpl; /** * Create a in-memory template. If the template name already exists, it * will be replaced. * * @example * ```ts * View.createTemplate('artisan::command', 'export class {{ namePascal }}') * * View.render('artisan::command', { namePascal: 'MyCommand' }) * ``` * * In-memory template could be used as components also * (Ignore the "\\" value in the example): * * @example * ```edge * \@component('artisan::command') * Hello * \@endcomponent * ``` */ createTemplate(name: string, template: string): ViewImpl; /** * Same as "createTemplate" method but create the template by the path * instead. If the file path does not exist, the registration is ignored * (no errors). * * @example * ```ts * const path = Path.resources('views/myTemplate.edge') * * View.createTemplateByPath('myTemplate', path) * ``` */ createTemplateByPath(name: string, path: string): ViewImpl; /** * Verify if some template exists. * * @example * View * .createTemplate('testing', '') * .hasTemplate('testing') // true */ hasTemplate(name: string): boolean; /** * Delete the template created using the "createTemplate" * method. * * @example * View * .createTemplate('testing', '') * .removeTemplate('testing') */ removeTemplate(name: string): ViewImpl; /** * Verify if Edge has the template name loaded or mounted. */ private isMountedOrIsTemplate; }