partials-loader

0.1.3 • Public • Published

partials-loader

Overview

This is a node module to help facilitate registering partials with their respective templating engine. In doing so it creates a namespace from the directory it is loaded from.

This means that when using this module you will be able to reference partials in your templates directory by providing the relative path from your template directory to the partial being loaded and handlebars will be aware of it.

Example Usage

First, pretend that we have a folder called
/Users/username/node project/

And in this folder there is this directory structure
/Users/username/node project/
|__templates
    |__example.hbs
    |__index.hbs
    |__more partials
        |__header.hbs
        |__content.html
        |__footers.hbs
    |__partials
        |__header.hbs
        |__content.html
        |__footers.hbs

Register all files from the templates folder

// First load your engine and the partials-loader
var handlebars = require('handlebar');
var partialLoader = require('partials-loader');
 
//  This registers every handlebar file 
partialLoader.handlebars({ template_engine_reference: handlebars, 
                            template_root_directories: '/Users/username/node project/templates',
                            partials_directory_names: '.',
                            template_extensions: 'hbs',
                            delimiter_symbol: '/'
                        });

This will result in handlebars having the registered partials:

  • templates/example
  • templates/index
  • templates/more partials/header
  • templates/more partials/footer
  • templates/partials/header
  • templates/partials/footer

Register files from a specific subfolder

// First load your engine and the partials-loader
var handlebars = require('handlebar');
var partialLoader = require('partials-loader');
 
//  This registers every handlebar file in the partials folder
partialLoader.handlebars({ template_engine_reference: handlebars, 
                            template_root_directories: '/Users/username/node project/templates',
                            partials_directory_names: 'partials',
                            template_extensions: 'hbs',
                            delimiter_symbol: '/'
                        });

This will result in handlebars having the registered partials:

  • templates/partials/header
  • templates/partials/footer

Register files from multiple specific subfolders

// First load your engine and the partials-loader
var handlebars = require('handlebar');
var partialLoader = require('partials-loader');
 
//  This registers every handlebar file in the specified folders
partialLoader.handlebars({ template_engine_reference: handlebars, 
                            template_root_directories: '/Users/username/node project/templates',
                            partials_directory_names: ['more partials', 'partials'],
                            template_extensions: 'hbs',
                            delimiter_symbol: '/'
                        });

This will result in handlebars having the registered partials:

  • templates/more partials/header
  • templates/more partials/footer
  • templates/partials/header
  • templates/partials/footer

Register multiple file types from multiple specific subfolders

// First load your engine and the partials-loader
var handlebars = require('handlebar');
var partialLoader = require('partials-loader');
 
//  This registers every handlebar file in the specified folders
partialLoader.handlebars({ template_engine_reference: handlebars, 
                            template_root_directories: '/Users/username/node project/templates',
                            partials_directory_names: ['more partials', 'partials'],
                            template_extensions: ['hbs', 'html'],
                            delimiter_symbol: '/'
                        });

This will result in handlebars having the registered partials:

  • templates/more partials/header
  • templates/more partials/content
  • templates/more partials/footer
  • templates/partials/header
  • templates/partials/content
  • templates/partials/footer

Currently Supported Template Engines

  • Handlebars

If there is a desire for supporting more engines, please post in the issues section of the repository, or feel free to implement it yourself and send a pull request for integration.

Package Sidebar

Install

npm i partials-loader

Weekly Downloads

94

Version

0.1.3

License

ISC

Last publish

Collaborators

  • loganknecht
  • jzon
  • windhandelj