npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-handlebars-file-include

v1.0.0

Published

Gulp Plugin used to include external files into html.

Downloads

598

Readme

gulp-handlebars-file-include

Gulp plugin for create html templates in a simpler way. A very common problem when developers create html templates for a site, is the amount of repeated html code. This module resolve that problem allowing you define a sections of code in separated files, for later invoke it. Much better still, this allow you build semantic templates with handlebars.


Installation

$ npm install gulp-handlebars-file-include

Basic Usage

Configure gulp-handlebars-file-include in your gulp file

gulp.js

var gulpHandlebarsFileInclude = require('gulp-handlebars-file-include');

gulp.src('src/**/*')
    .pipe(gulpHandlebarsFileInclude())
    .pipe(gulp.dest('dist'))

Example

Let say we create a file to represent a button

src/button.html

<button class="btn simple-btn">
    click me
</button>

Then you can use the button in another file, for example

src/index.html

<h1>Hello World!!</h1>
<p>this is the content of the page</p>
{{ fileInclude 'src/button' }}

(Note the use of fileInclude helper) and you get as result

dist/index.html

<h1>Hello World!!</h1>
<p>this is the content of the page</p>
<button class="btn simple-btn">
    click me
</button>

Let say you want to add an icon image to the buttons, the only thing you need to do is change your button.html file, for example.

src/button.html

<button class="btn simple-btn">
    <img src="main.png"/>
    click me
</button>

But you can even improve the behavior of your button and allow set its image and text when it is used, for example

src/button.html

<button class="btn simple-btn">
    {{#if image}}
    <img src="{{ image }}"/>
    {{else}}
    <img src="main.jpg"/>
    {{/if}}
    
    {{#if text}}
    {{text}}
    {{else}}
    click me
    {{/if}}
</button>

here we say to button set the image and text context property if those values are supplied, if not, set the 'main.png' and 'click me' values respectively. Then in your index.html you can say

src/index.html

<h1>Hello World!!</h1>
<p>this is the content of the page</p>
{{ fileInclude 'src/button.html' image='bird.jpg' text='buy birds' }}

and as you can suppose the result is

dist/index.html

<h1>Hello World!!</h1>
<p>this is the content of the page</p>
<button class="btn simple-btn">
    <img src="bird.jpg"/>
    buy birds
<button>

even better, you can use the eval helper (provided with this module) to evaluate an expression on fly, that will reduce your button.html file to this:

src/button.html

<button class="btn simple-btn">
    <img src="{{eval "this.image || 'main.jpg'" }}"/>
    {{eval "this.text || 'click me'"}}
</button>

Note that this helper receive a string expression to evaluate, and you access to context parameters with this keyword.


Handlebars Helpers

  • fileInclude This helper receive the path as string, of an external file used to compile with handlebars and included the compiled result. You can pass parameters used to compile the external file in the way arg1=value1 arg2=value2 ...

  • eval This helper receive an expression as string, this expression is evaluated and return its result. You can access to context properties in the expression, using the this keyword


API

gulpHandlebarsFileInclude(globalContext, options)

  • globalContext object used as a default context for all templates. This can be useful if you want to set, for example, the same footer message for all indexes page.

  • options object with the following properties

    • rootPath string, or string[] used to set where the compiler search for files to include. This is useful to take you away to define the whole path of the file to include. If the compiler can't find a file in the rootPath, then is search as normal absolute file path.

    • extensions string[] to set the valid file extensions in which the compiler search the files. This allow to declare a file to include without extensions. Default is ['.html', '.hbs', '.hb', '.handlebars'].

    • maxRecursion int used to restrict the maximum amount of times in which a file can include it-self. This is used to stop infinite recursion of the included file. Default value is 10.

    • ignoreFiles function(string) => boolean that receive a filePath of the current file to compile and return boolean to indicate if you want generate the file in dist. That is useful to avoid generate files of partial templates. For example, maybe all your partial files are in src/partials, then you can check the path of file to generete and ignore from src/partials with

        function(filePath){
            console.log(filePath)
            return filePath.startsWith(path.resolve(__dirname, 'src/partials'));
        }
    • handlebarsHelpers {name: <string>, fn: function}[] Array of Objects with the properties name of string and fn of function. This is used to include custom helpers to the handlebars compiler.