JSPM

  • Created
  • Published
  • Downloads 32274
  • Score
    100M100P100Q155853F

Wrap templates with layouts. Layouts can be nested and optionally use other layouts.

Package Exports

  • layouts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (layouts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

layouts NPM version

Wrap templates with layouts. Layouts can be nested and optionally use other layouts.

Install

npm

npm i layouts --save

Usage

var layouts = require('layouts');

API

## Layouts

Create a new instance of Layouts, optionally passing the default cache and options to use.

  • cache {Object}: A template cache. See Layouts#set for object details.
  • options {Object}: Options to use.
  • options.delims {Array}: Template delimiters to use formatted as an array (['{{', '}}'])
  • options.tag {String}: The tag name to use. Default is body (e.g. {{ body }})

Example:

var Layouts = require('layouts');
var layouts = new Layouts();

## setLayout

Store a template on the cache by its name, the layout to use, and the template's `content.

  • name {String|Object}: If name is a string, layout and content are required.
  • data {String|Object}: Pass a string defining the name of layout to use for the given template, or pass an object with a layout property.
  • content {String}: The template "content", this will not be compiled or rendered.

Example:

layouts.setLayout('a', 'b', '<h1>Foo</h1>\n{{body}}\n');

## _defaultLayout

Define the default layout variable and delimiters to be used.

## getLayout

Get a cached template by name.

  • name {String}
  • returns {Object}: The template object to return.

Example:

layouts.getLayout('a');
//=> { layout: 'b', content: '<h1>Foo</h1>\n{{body}}\n' }

## renderLayout

Render a layout using Lo-Dash, by passing content (str), context and options.

  • str {String}: Content for the layout to render.
  • options {Object}: Additional options used for building the render settings.
  • returns {String}: Rendered string.

Example:

layouts.renderLayout(str, context, options);

Since this method uses Lo-Dash to process templates custom delimiters may be passed on the options.delims property. This allows layouts to be rendered prior to injecting "pages" or other str with templates that should not be rendered when the layout stack is processed.

Example:

layouts.renderLayout(str, context, {
  delims: ['<%','%>']
});

## replaceTag

Replace a {{body}} tag with the given str. Custom delimiters and/or variable may be passed on the options. Unlike renderLayout, this method does not render templates, it only peforms a basic regex replacement.

  • str {String}: The string to use as a replacement value.
  • content {String}: A string with a {{body}} tag where the str should be injected.
  • returns {String}: Resulting flattened content.

Example:

layouts.replaceTag('ABC', 'Before {{body}} After');
//=> 'Before ABC After'

## inject

Return an object with the string (str) and data required to build a final layout. This is useful if you need to use your own template engine to handle this final step.

  • str {String}: The string to be injected into the layout. Usually a page, or inner layout, etc.
  • name {String}: The name of the first layout to use to build the stack.
  • returns {Object}: Resulting flattened layout.

Example:

var page = layouts.inject(str, 'base');
var tmpl = _.template(page, context);

Author

Jon Schlinkert

License

Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license


This file was generated by verb-cli on August 17, 2014.