### Setup Hapi Server with Vision Plugin
Source: https://github.com/hapijs/vision/blob/master/API.md
Demonstrates the basic setup for integrating the Vision plugin into a Hapi server. This involves registering the plugin and starting the server to handle templated views.
```javascript
const Hapi = require('@hapi/hapi');
const Vision = require('@hapi/vision');
const server = Hapi.Server({
port: 3000
});
const provision = async () => {
await server.register(Vision);
await server.start();
console.log('Server running at:', server.info.uri);
};
provision();
```
--------------------------------
### Hapi Vision Basic Setup and Usage Example
Source: https://github.com/hapijs/vision/blob/master/API.md
This JavaScript example demonstrates how to set up the Hapi Vision plugin, configure it to use Handlebars as the templating engine, and render a view within a route handler using h.view.
```js
const Hapi = require('@hapi/hapi');
const server = Hapi.Server({ port: 3000 });
const internals = {};
internals.provision = async () => {
await server.register(require('@hapi/vision'));
server.views({
engines: { html: require('handlebars') },
path: __dirname + '/templates'
});
const rootHandler = function (request, h) {
const context = {
title: 'Views Example',
message: 'Hello, World'
};
return h.view('hello', context);
};
server.route({ method: 'GET', path: '/', handler: rootHandler });
};
internals.provision();
```
--------------------------------
### Basic Hapi Vision Setup and Rendering
Source: https://github.com/hapijs/vision/blob/master/API.md
Demonstrates how to register the Vision plugin, configure view engines and paths, and render a template using `server.render` in Hapi.js.
```js
const Hapi = require('@hapi/hapi');
const server = Hapi.Server({ port: 3000 });
const internals = {};
internals.provision = async () => {
await server.register(require('@hapi/vision'));
server.views({
engines: { html: require('handlebars') },
path: __dirname + '/templates'
});
const context = {
title: 'Views Example',
message: 'Hello, World'
};
return server.render('hello', context);
};
internals.provision();
```
--------------------------------
### Hapi Vision Example HTML Template
Source: https://github.com/hapijs/vision/blob/master/API.md
This is an example of an HTML template file used with Hapi Vision and Handlebars. It demonstrates how to use context variables like {{title}} and {{message}} to dynamically render content.
```html
{{title}}
{{message}}
```
--------------------------------
### Hapi Vision Plugin Registration Example
Source: https://github.com/hapijs/vision/blob/master/API.md
Illustrates registering the Hapi Vision plugin with a different template engine, Handlebars, and demonstrates rendering a view. This example shows how registration options, including engines and paths, are passed to the plugin.
```js
internals.provision = async () => {
await server.register({
plugin: require('@hapi/vision'),
options: {
engines: { html: require('handlebars') },
path: __dirname + '/templates'
}
})
const context = {
title: 'Registration Example',
message: 'Hello, World'
};
return server.render('hello', context);
};
internals.provision();
```
--------------------------------
### Configure and Use Handlebars Templating with Vision
Source: https://github.com/hapijs/vision/blob/master/API.md
Illustrates setting up Hapi Vision with the Handlebars templating engine. This example configures view paths and uses `h.view` to render a Handlebars template, passing data for dynamic content.
```javascript
const Handlebars = require('handlebars');
const Hapi = require('@hapi/hapi');
const Vision = require('@hapi/vision');
const server = Hapi.Server({
port: 3000
});
const rootHandler = (request, h) => {
return h.view('index', {
title: 'examples/handlebars/templates/basic | hapi ' + request.server.version,
message: 'Hello Handlebars!'
});
};
const provision = async () => {
await server.register(Vision);
server.views({
engines: { html: Handlebars },
relativeTo: __dirname,
path: 'examples/handlebars/templates/basic'
});
server.route({
method: 'GET',
path: '/',
handler: rootHandler
});
await server.start();
console.log('Server running at:', server.info.uri);
};
provision();
```
--------------------------------
### Templating Data Transformation Helpers
Source: https://github.com/hapijs/vision/blob/master/test/templates/valid/testHelpersAndPartials.html
Illustrates the application of helper functions to transform data during rendering. Examples include converting text case and applying custom formatting functions.
```templating
This is all {{uppercaseToMutate this.mutateToUppercase}} and this is {{long "how"}} we like it!
```
--------------------------------
### Using the View Handler in a Route
Source: https://github.com/hapijs/vision/blob/master/API.md
Example demonstrating how to configure a Hapi.js route to use the 'view' handler, specifying the template and context directly in the route definition.
```javascript
const Hapi = require('@hapi/hapi');
const server = Hapi.Server({
port: 3000
});
const internals = {};
internals.provision = async () => {
await server.register(require('@hapi/vision'));
server.views({
engines: {
html: require('handlebars')
},
path: __dirname + '/templates'
});
server.route({
method: 'GET',
path: '/',
handler: {
view: {
template: 'hello',
context: {
title: 'Views Example',
message: 'Hello, World'
}
}
}
});
};
internals.provision();
```
--------------------------------
### Using request.render in a Handler
Source: https://github.com/hapijs/vision/blob/master/API.md
Example demonstrating how to use the `request.render()` method within a Hapi.js route handler to render a template with a given context.
```javascript
const Hapi = require('@hapi/hapi');
const server = Hapi.Server({
port: 3000
});
const internals = {};
internals.provision = async () => {
await server.register(require('@hapi/vision'));
server.views({
engines: {
html: require('handlebars')
},
path: __dirname + '/templates'
});
server.route({
method: 'GET',
path: '/view',
handler: function (request, h) {
return request.render('test', {
message: 'hello'
});
}
});
};
internals.provision();
```
--------------------------------
### Hapi Vision TypeScript Route Handler Example
Source: https://github.com/hapijs/vision/blob/master/API.md
Illustrates how to use the defined TypeScript interfaces for Vision within a Hapi route configuration. It shows autocompletion for template names and layout options when defining the handler's view properties.
```typescript
const route = {
...
handler: {
view: {
template: 'test', // should autocomplete
options: { layout: 'auth' } // should autocomplete
}
}
}
```
--------------------------------
### Hapi.js Nunjucks Template Integration
Source: https://github.com/hapijs/vision/blob/master/API.md
Illustrates the integration of the Nunjucks templating engine with Hapi.js via the @hapi/vision plugin. This setup involves configuring Vision to compile and render Nunjucks templates, including a prepare step for environment configuration.
```javascript
const Hapi = require('@hapi/hapi');
const Nunjucks = require('nunjucks');
const Vision = require('@hapi/vision');
const server = Hapi.Server({ port: 3000 });
const rootHandler = (request, h) => {
return h.view('index', {
title: 'examples/nunjucks/templates | Hapi ' + request.server.version,
message: 'Hello Nunjucks!'
});
};
const provision = async () => {
await server.register(Vision);
server.views({
engines: {
html: {
compile: (src, options) => {
const template = Nunjucks.compile(src, options.environment);
return (context) => {
return template.render(context);
};
},
prepare: (options, next) => {
options.compileOptions.environment = Nunjucks.configure(options.path, { watch : false });
return next();
}
}
},
relativeTo: __dirname,
path: 'examples/nunjucks/templates'
});
server.route({ method: 'GET', path: '/', handler: rootHandler });
await server.start();
console.log('Server running at:', server.info.uri);
};
provision();
```
--------------------------------
### Templating: Uppercase Variable
Source: https://github.com/hapijs/vision/blob/master/test/templates/valid/testHelpers.html
This snippet demonstrates how to transform a variable's value to uppercase within the Hapi.js Vision templating engine. It takes a property from the context object and applies an uppercase transformation.
```templating
{{uppercase this.something}}
```
--------------------------------
### Templating: Custom Helper Function
Source: https://github.com/hapijs/vision/blob/master/test/templates/valid/testHelpers.txt
This snippet illustrates the usage of a custom helper function named 'long' within the Hapi.js Vision templating engine. It shows how to pass arguments to helper functions for custom logic execution.
```templating
{{long "how"}}
```
--------------------------------
### Templating: Uppercase Variable
Source: https://github.com/hapijs/vision/blob/master/test/templates/valid/testHelpers.txt
This snippet demonstrates how to transform a variable's value to uppercase within the Hapi.js Vision templating engine. It takes a property from the context object and applies an uppercase transformation.
```templating
{{uppercase this.something}}
```
--------------------------------
### Hapi.js Twig Template Integration
Source: https://github.com/hapijs/vision/blob/master/API.md
Provides an example of integrating the Twig templating engine with Hapi.js using the @hapi/vision plugin. The code configures Vision to handle Twig templates, setting up a server route to render a Twig-based view.
```javascript
const Hapi = require('@hapi/hapi');
const Twig = require('twig');
const Vision = require('@hapi/vision');
const server = Hapi.Server({ port: 3000 });
const rootHandler = (request, h) => {
return h.view('index', {
title: 'examples/twig/templates | Hapi ' + request.server.version,
message: 'Hello Twig!'
});
};
const provision = async () => {
await server.register(Vision);
server.views({
engines: {
twig: {
compile: (src, options) => {
const template = Twig.twig({ id: options.filename, data: src });
return (context) => {
return template.render(context);
};
}
}
},
relativeTo: __dirname,
path: 'examples/twig/templates'
});
server.route({ method: 'GET', path: '/', handler: rootHandler });
await server.start();
console.log('Server running at:', server.info.uri);
};
provision();
```
--------------------------------
### Templating: Custom Helper Function
Source: https://github.com/hapijs/vision/blob/master/test/templates/valid/testHelpers.html
This snippet illustrates the usage of a custom helper function named 'long' within the Hapi.js Vision templating engine. It shows how to pass arguments to helper functions for custom logic execution.
```templating
{{long "how"}}
```
--------------------------------
### Hapi.js Mustache Template Integration
Source: https://github.com/hapijs/vision/blob/master/API.md
Shows how to integrate the Mustache templating engine with Hapi.js using the @hapi/vision plugin. This example configures Vision to use Mustache for HTML rendering, setting up a server route to display a Mustache-rendered view.
```javascript
const Hapi = require('@hapi/hapi');
const Mustache = require('mustache');
const Vision = require('@hapi/vision');
const server = Hapi.Server({ port: 3000 });
const rootHandler = (request, h) => {
return h.view('index', {
title: 'examples/mustache/templates/basic | Hapi ' + request.server.version,
message: 'Hello Mustache!'
});
};
const provision = async () => {
await server.register(Vision);
server.views({
engines: {
html: {
compile: (template) => {
Mustache.parse(template);
return (context) => {
return Mustache.render(template, context);
};
}
}
},
relativeTo: __dirname,
path: 'examples/mustache/templates/basic'
});
server.route({ method: 'GET', path: '/', handler: rootHandler });
await server.start();
console.log('Server running at:', server.info.uri);
};
provision();
```
--------------------------------
### Initialize nicEditor Rich Text Editor
Source: https://github.com/hapijs/vision/blob/master/examples/cms/views/create.html
This snippet shows how to initialize the nicEditor rich text editor. It uses the `onDomLoaded` function to ensure the DOM is ready before creating the editor instance. The `buttonList` parameter configures the available formatting options for the editor, and `panelInstance` attaches it to a specific HTML element with the ID 'contents'.
```javascript
bkLib.onDomLoaded(function() {
new nicEditor({
buttonList : [
'fontSize',
'bold',
'italic',
'underline',
'strikeThrough',
'subscript',
'superscript',
'html'
]
}).panelInstance('contents');
});
```
--------------------------------
### Hapi Vision with Eta Template Engine
Source: https://github.com/hapijs/vision/blob/master/API.md
Demonstrates integrating the Hapi Vision plugin with the Eta template engine. It shows how to register Vision, configure Eta compilation and rendering, set view paths, and define compile options for template rendering within a Hapi server.
```js
const Hapi = require('@hapi/hapi');
const Eta = require('eta');
const Vision = require('@hapi/vision');
const server = Hapi.Server({ port: 3000 });
const rootHandler = (request, h) => {
return h.view('index', {
title: 'examples/eta/templates | Hapi ' + request.server.version,
message: 'Hello Eta!'
});
};
const provision = async () => {
await server.register(Vision);
server.views({
engines: {
eta: {
compile: (src, options) => {
const compiled = Eta.compile(src, options);
return (context) => {
return Eta.render(compiled, context);
};
}
}
},
/**
* This is the config object that gets passed to the compile function
* defined above. This should contain the eta configuration object
* described at https://eta.js.org/docs/api/configuration Only some of
* the configuration are relevant when using with hapijs.
*/
compileOptions: {
autoEscape: true,
tags: ['{{', '}}']
},
relativeTo: __dirname,
path: 'examples/eta/templates'
});
server.route({ method: 'GET', path: '/', handler: rootHandler });
await server.start();
console.log('Server running at:', server.info.uri);
};
provision();
```
--------------------------------
### Navigation Templating
Source: https://github.com/hapijs/vision/blob/master/test/templates/valid/testPartialsName.html
Illustrates the use of templating syntax to include navigation partials within the hapi/vision project. This pattern is common for rendering dynamic navigation menus or reusable content blocks.
```Handlebars
Nav:{{> nav}}|{{> nested/deep/nav}}
```
--------------------------------
### Hapi Vision Views Manager Configuration
Source: https://github.com/hapijs/vision/blob/master/API.md
Details the configuration options for the Hapi Vision Views Manager. This includes setting up template engines, specifying template paths, and defining compile-time options for rendering.
```APIDOC
server.register(plugin, [options])
- Registers the Vision plugin with the Hapi server.
- Options object can be passed to configure the plugin.
server.views(options)
- Configures the Views Manager directly.
- Can be used as an alternative or in addition to registration options.
Views Manager Options:
- engines: An object mapping file extensions to template engine configurations.
- Example: { eta: require('eta'), html: require('handlebars') }
- path: The directory path where template files are located.
- Can be a string or an array of strings.
- relativeTo: The module from which to resolve the path.
- compileOptions: An object containing options to be passed to the template engine's compile function.
- Example for Eta: { autoEscape: true, tags: ['{{', '}}'] }
- isCached: Boolean, whether to cache compiled templates (defaults to true).
- layout: Boolean or string, specifies the default layout file to use.
- layoutPath: String, the directory path for layout files.
- partialsPath: String, the directory path for partial template files.
- helpersPath: String, the directory path for helper functions.
- context: Object, default context data to be passed to all views.
- defaultExtension: String, the default file extension for views if not specified.
```
--------------------------------
### nicEditor Initialization
Source: https://github.com/hapijs/vision/blob/master/examples/cms/views/index.html
Initializes the nicEditor rich text editor with a specified button list and targets a specific panel instance. This snippet is used when no pages are defined, likely for creating new content.
```javascript
//
```
--------------------------------
### Configure and Use EJS Templating with Vision
Source: https://github.com/hapijs/vision/blob/master/API.md
Shows how to configure Hapi Vision to use the EJS templating engine. It sets up view paths and demonstrates rendering an EJS template with dynamic data via the response toolkit's `h.view` method.
```javascript
const Ejs = require('ejs');
const Hapi = require('@hapi/hapi');
const Vision = require('@hapi/vision');
const server = Hapi.Server({
port: 3000
});
const rootHandler = (request, h) => {
return h.view('index', {
title: 'examples/ejs/templates/basic | Hapi ' + request.server.version,
message: 'Hello Ejs!'
});
};
const provision = async () => {
await server.register(Vision);
server.views({
engines: { ejs: Ejs },
relativeTo: __dirname,
path: 'examples/ejs/templates/basic'
});
server.route({
method: 'GET',
path: '/',
handler: rootHandler
});
await server.start();
console.log('Server running at:', server.info.uri);
};
provision();
```
--------------------------------
### Configure and Use Pug Templating with Vision
Source: https://github.com/hapijs/vision/blob/master/API.md
Demonstrates configuring Hapi Vision to use the Pug templating engine, including setting `compileOptions` for `basedir`. It shows how to render a Pug template with data using `h.view`.
```javascript
const Path = require('path');
const Hapi = require('@hapi/hapi');
const Pug = require('pug');
const Vision = require('@hapi/vision');
const server = Hapi.Server({
port: 3000
});
const rootHandler = (request, h) => {
return h.view('index', {
title: 'examples/pug/templates | Hapi ' + request.server.version,
message: 'Hello Pug!'
});
};
const provision = async () => {
await server.register(Vision);
server.views({
engines: { pug: Pug },
relativeTo: __dirname,
path: 'examples/pug/templates',
compileOptions: {
// By default Pug uses relative paths (e.g. ../root.pug), when using absolute paths (e.g. include /root.pug), basedir is prepended.
// https://pugjs.org/language/includes.html
basedir: Path.join(__dirname, 'examples/pug/templates')
}
});
server.route({
method: 'GET',
path: '/',
handler: rootHandler
});
await server.start();
console.log('Server running at:', server.info.uri);
};
provision();
```
--------------------------------
### Hapi.js Marko Template Integration
Source: https://github.com/hapijs/vision/blob/master/API.md
Demonstrates how to register the @hapi/vision plugin and configure it to use the Marko templating engine for rendering views. It sets up a basic Hapi server with a route that renders a Marko template, passing context data to it.
```javascript
const Hapi = require('@hapi/hapi');
const Marko = require('marko');
const Vision = require('@hapi/vision');
const server = Hapi.Server({ port: 3000 });
const rootHandler = (request, h) => {
return h.view('index', {
title: 'examples/marko/templates | Hapi ' + request.server.version,
message: 'Hello Marko!'
});
};
const provision = async () => {
await server.register(Vision);
server.views({
engines: {
marko: {
compile: (src, options) => {
const opts = { preserveWhitespace: true, writeToDisk: false };
const template = Marko.load(options.filename, opts);
return (context) => {
return template.renderToString(context);
};
}
}
},
relativeTo: __dirname,
path: 'examples/marko/templates'
});
server.route({ method: 'GET', path: '/', handler: rootHandler });
await server.start();
console.log('Server running at:', server.info.uri);
};
provision();
```
--------------------------------
### Templating Partials and Includes
Source: https://github.com/hapijs/vision/blob/master/test/templates/valid/testHelpersAndPartials.html
Demonstrates the use of partials or includes within the templating engine. The `{{> ...}}` syntax indicates the inclusion of reusable template fragments, potentially with nested paths.
```templating
Nav:{{> navToMutate}}|{{> nested/navToMutate}}
```
--------------------------------
### Hapi Vision Plugin Configuration Options
Source: https://github.com/hapijs/vision/blob/master/API.md
Detailed breakdown of the configuration options available for the Hapi.js Vision plugin, used to manage template rendering and views.
```APIDOC
options:
- engines (object, required): Maps file extensions (e.g., 'html', 'hbr') to npm modules for rendering.
Each extension can map to:
- module (object): An npm module with a `compile()` function.
- `compile()`: The rendering function. Its signature depends on `compileMode`.
- If `compileMode` is 'sync': `compile(template, options)` returns a function `function(context, options)`.
- If `compileMode` is 'async': `compile(template, options, next)` calls `next(err, compiled)` where `compiled` is `function(context, options, callback)`.
- `prepare(config, next)`: Initializes additional engine state. Signature: `function(err)`.
- `registerPartial(name, src)`: Registers a partial template.
- `registerHelper(name, helper)`: Registers a helper function.
- Any of the `views` options (except `defaultExtension`) to override defaults for a specific engine.
- compileMode (string): Specifies if the engine `compile()` method is 'sync' or 'async'. Defaults to 'sync'.
- defaultExtension (string): Default filename extension appended to template names when no explicit extension is provided.
- path (string | string[]): Root file path(s) for resolving and loading templates via `h.view()`.
- partialsPath (string | string[]): Root file path(s) for locating partials.
- helpersPath (string | string[]): Directory path(s) for locating helper functions. Files must export a single method `function(context)` returning a string.
- relativeTo (string): Base path used as a prefix for `path` and `partialsPath`.
- layout (boolean | string): Enables layout support. If `true`, uses 'layout.ext'. If a string, uses that filename suffixed with the engine's extension.
- layoutPath (string | string[]): Root file path(s) for layout templates.
- layoutKeyword (string): The key used by the template engine for primary template content. Defaults to 'content'.
- encoding (string): Text encoding for reading files and outputting results. Defaults to 'utf8'.
- isCached (boolean): If `false`, templates are not cached and read from file on every use. Defaults to `true`.
- allowAbsolutePaths (boolean): If `true`, allows absolute template paths passed to `h.view()`. Defaults to `false`.
- allowInsecureAccess (boolean): If `true`, allows insecure template paths passed to `h.view()`.
```
--------------------------------
### The View Handler Configuration
Source: https://github.com/hapijs/vision/blob/master/API.md
Describes the 'view' handler configuration for Hapi.js routes, allowing direct specification of template, context, and rendering options within the route definition. The context defaults to request parameters, payload, query, and pre-values.
```APIDOC
handler: { view: options }
- The 'view' handler is used with routes registered in the same realm as the view manager.
- The `options` parameter can be a string (template path) or an object.
- Object keys for options:
- template: string - The template filename and path, relative to the configured templates path.
- context: object - Optional object for template rendering. Defaults to no context.
- options: object - Optional object to override server's views manager configuration for this response (e.g., `isCached`, `partialsPath`, `helpersPath` cannot be overridden here).
- Default rendering context includes `params`, `payload`, `query`, and `pre` values from the request.
```
--------------------------------
### Jinja2 Template Structure
Source: https://github.com/hapijs/vision/blob/master/examples/nunjucks/templates/layout.html
Illustrates a typical Jinja2 template structure, showing how to include other template files and define content blocks for inheritance. This pattern is common in web development for managing reusable UI components.
```jinja2
{% include 'includes/head.html' %}
Layout header
{% block content %} Default content -- should not show up! {% endblock %}
Layout footer
{% include 'includes/foot.html' %}
```
--------------------------------
### Initialize nicEditor Rich Text Editor (JavaScript)
Source: https://github.com/hapijs/vision/blob/master/examples/cms/views/edit.html
Initializes the nicEditor rich text editor on the 'contents' textarea. It configures the available toolbar buttons for rich text editing.
```javascript
bkLib.onDomLoaded(function() {
new nicEditor({
buttonList : [
'fontSize',
'bold',
'italic',
'underline',
'strikeThrough',
'subscript',
'superscript',
'html'
]
}).panelInstance('contents');
});
```
--------------------------------
### Request Object Methods for Views
Source: https://github.com/hapijs/vision/blob/master/API.md
Details methods available on the Hapi request object for rendering views and accessing the view manager. `request.render()` is used within request handlers and inherits its realm from the bound route. `request.getViewsManager()` retrieves the closest Views manager.
```APIDOC
request.render(template, context, [options], [callback])
- Works similarly to server.render() but is intended for use inside request handlers.
- Inherits its realm from the route the request was bound to.
- Note: Does not work reliably in `onRequest` extensions as the route is not yet set.
request.getViewsManager()
- Returns the closest Views manager to your realm (either on your realm or inherited from an ancestor realm).
```
--------------------------------
### Hapi Vision h.view Method
Source: https://github.com/hapijs/vision/blob/master/API.md
The h.view method is used to return a templatized view response. It accepts a template name, an optional context object for rendering, and optional options to override view manager configurations. It returns a response object with the 'variety' property set to 'view'.
```APIDOC
h.view(template, [context, [options]])
- template: The template filename and path, relative to the server's configured templates path.
- context: Optional object used by the template for rendering. Defaults to an empty object.
- options: Optional object to override server's views manager configuration for this response. Cannot override 'isCached', 'partialsPath', or 'helpersPath'.
Returns a [response object](https://github.com/hapijs/hapi/blob/master/API.md#response-object) with the 'variety' property set to 'view'. The same [lifecycle workflow](https://github.com/hapijs/hapi/blob/master/API.md#lifecycle-workflow) applies.
```
--------------------------------
### Hapi Vision TypeScript Augmented Interfaces for Render Methods
Source: https://github.com/hapijs/vision/blob/master/API.md
Augments Hapi Vision's interfaces to provide type definitions for decorated handlers, specifically for server.render, request.render, and h.view methods. This enables strict checking of template and context combinations.
```typescript
declare module '@hapi/vision' {
// User defined
type CustomTemplates = (
'test' | 'hello' | 'temp1'
);
// User defined
type CustomLayout = (
'auth' | 'unauth' | 'admin'
)
// server.render(...)
interface RenderMethod {
(template: CustomTemplates, context?: string): Promise
}
// { handler: (req) => req.render(...) }
interface RequestRenderMethod {
(template: CustomTemplates, context?: string): Promise
}
// { handler: (req, h) => h.view(...) }
interface ToolkitRenderMethod {
(template: CustomTemplates, context?: string): ResponseObject
}
}
```
--------------------------------
### Hapi Vision TypeScript Interfaces for Views
Source: https://github.com/hapijs/vision/blob/master/API.md
Provides TypeScript interfaces to strongly type the Vision plugin's view rendering capabilities. This includes defining custom template names and layout types for improved developer experience and compile-time checks.
```typescript
declare module '@hapi/vision' {
// User defined
type CustomTemplates = (
'test' | 'hello' | 'temp1'
);
// User defined
type CustomLayout = (
'auth' | 'unauth' | 'admin'
)
interface ViewTypes {
template: CustomTemplates
layout: CustomLayout
}
}
```
--------------------------------
### Handlebars Partials in Hapi.js Vision
Source: https://github.com/hapijs/vision/blob/master/test/templates/valid/testPartials.html
Illustrates how to define and use Handlebars partials within the Hapi.js Vision plugin configuration. Partials allow for reusable template fragments to be included in other templates.
```handlebars
Nav:{{> nav}}|{{> nested/nav}}
```
--------------------------------
### Hapi Vision TypeScript Strict Template/Context Typing
Source: https://github.com/hapijs/vision/blob/master/API.md
Demonstrates advanced TypeScript usage by defining specific context types for each template within the ToolkitRenderMethod interface. This enforces strict type checking for template-context pairings in handler implementations.
```typescript
// { handler: (req, h) => h.view(...) }
interface ToolkitRenderMethod {
(template: 'test', context: { apples: 5 }): ResponseObject
(template: 'hello', context: { oranges: true }): ResponseObject
(template: 'temp1', context: { bananas: 'green' }): ResponseObject
}
```
--------------------------------
### Hapi Vision h.getViewsManager Method
Source: https://github.com/hapijs/vision/blob/master/API.md
The h.getViewsManager method retrieves the closest Views manager instance associated with the current realm. This allows access to the manager's functionalities, potentially inherited from ancestor realms.
```APIDOC
h.getViewsManager()
Returns the closest [Views manager](#views-manager) to your `realm` (either on your realm or inherited from an ancestor realm).
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.