Gathering detailed insights and metrics for grunt-handlebars-simple-layouts
Gathering detailed insights and metrics for grunt-handlebars-simple-layouts
Gathering detailed insights and metrics for grunt-handlebars-simple-layouts
Gathering detailed insights and metrics for grunt-handlebars-simple-layouts
npm install grunt-handlebars-simple-layouts
Typescript
Module System
Min. Node Version
Node Version
NPM Version
44.1
Supply Chain
80.9
Quality
65.3
Maintenance
25
Vulnerability
95.6
License
JavaScript (56.58%)
HTML (43.42%)
Total Downloads
2,444
Last Day
1
Last Week
7
Last Month
11
Last Year
384
MIT License
7 Commits
1 Forks
1 Watchers
2 Branches
1 Contributors
Updated on Oct 18, 2015
Latest Version
0.2.4
Package Id
grunt-handlebars-simple-layouts@0.2.4
Size
13.64 kB
NPM Version
2.11.2
Node Version
0.12.6
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
600%
7
Compared to previous week
Last Month
37.5%
11
Compared to previous month
Last Year
-41.6%
384
Compared to previous year
5
1
Handlebars helpers which implement Jade-like layout blocks.
A grunt.js task to render Handlebars templates against a context & produce HTML.
If you want to pre-compilation your Handlebars use the grunt-contrib-handlebars Grunt task. This task will pre-compile multiple templates into a single file.
This task renders Handlebars templates against a context to produce HTML.
Inspired by grunt-dust-html and handlebars-layouts
This plugin requires Grunt ~0.4.0
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
1npm install grunt-handlebars-layouts --save-dev
Next, add this line to your project's grunt file:
1grunt.loadNpmTasks("grunt-handlebars-layouts");
This plugin was designed to work with Grunt 0.4.x. If you're still using grunt v0.3.x it's strongly recommended that you upgrade, but in case you can't please use v0.3.3.
Lastly, add the configuration settings (see below) to your grunt file.
This task has two required properties, src
and dest
. src
is the path to your source file and dest
is the file this task will write to (relative to the grunt.js file). If this file already exists it will be overwritten.
1 files: { 2 'dest.html': 'src.html' 3 },
An example configuration looks like this:
1 grunt.initConfig({
2 handlebarslayouts: {
3 home: {
4 files: {
5 'dist/home.html': 'src/home.html'
6 },
7 options: {
8 partials: [
9 'src/partials/*.hbs',
10 'src/layout.html'
11 ],
12 modules: [
13 'src/helpers/helpers-*.js'
14 ],
15 basePath: 'src/',
16 context: {
17 title: 'Layout Test',
18 items: [
19 'apple',
20 'orange',
21 'banana'
22 ]
23 }
24 }
25 }
26 }
27 });
28 grunt.registerTask('default', ['handlebarslayouts']);
This plugin can be customized by specifying the following options:
partials
: partials files.basePath
: The base location to all your templates so that includes/partials can be resolved correctly.context
: A JavaScript object to render the template against. This option supports a few different types:modules
: add your customs helpersstrict
: when is true the process fails if one file is missing (when is false juste a warning is trigger)Useful Handlebars Helpers : handlebars-helpers
String: the location to a file containing valid JSON:
1context: '/path/to/file.json'
Object: a regular ol' JavaScript object:
1context: { 2 pageTitle: 'My Awesome Website' 3}
Array: an array of contexts, either string (files to parse) or JavaScript objects, or both. Each item in the array will be merged into a single context and rendered against the template:
1context: [ 2 'path/to/context.json', 3 'path/to/another/context.json', 4 { more: 'data' } 5]
1{{#extend "layout"}} 2 {{#append "head"}} 3 <link rel="stylesheet" href="assets/css/home.css" /> 4 {{/append}} 5 6 {{#replace "body"}} 7 <h2>Welcome Home</h2> 8 9 <ul> 10 {{#items}} 11 <li>{{.}}</li> 12 {{/items}} 13 </ul> 14 {{/replace}} 15 16 {{#prepend "footer"}} 17 <script src="assets/js/analytics.js"></script> 18 {{/prepend}} 19{{/extend}}
1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 6 {{#block "head"}} 7 <title>{{title}}</title> 8 <meta name="description" content=""> 9 {{/block}} 10 </head> 11 12 <body> 13 <div class="site"> 14 <div class="site-hd" role="banner"> 15 {{#block "header"}} 16 <h1>{{title}}</h1> 17 {{/block}} 18 </div> 19 20 <div class="site-bd" role="main"> 21 {{#block "body"}} 22 <h2>Hello World</h2> 23 {{/block}} 24 </div> 25 26 <div class="site-ft" role="contentinfo"> 27 {{#block "footer"}} 28 <small>© 2014</small> 29 {{/block}} 30 </div> 31 </div> 32 33 {{#block "footer"}} 34 <p>footer</p> 35 {{/block}} 36 37 {{> footer}} 38 </body> 39</html>
1<footer>footer</footer>
Helpers can either be an object or a single register function. If register is on the object, then it calls the register function, passing in the engine.
1module.exports.register = function (Handlebars, options) { 2 Handlebars.registerHelper('foo', function () { 3 return 'foo'; 4 }); 5};
1'use strict'; 2 3var crypto = require('crypto'), 4 fs = require('fs'); 5 6// The module to be exported 7var helpers = { 8 md5: function (path) { 9 var content = fs.readFileSync(path); 10 return crypto.createHash('md5').update(content).digest('hex'); 11 } 12}; 13 14// Export helpers 15module.exports.register = function (Handlebars, options) { 16 options = options || {}; 17 18 for (var helper in helpers) { 19 if (helpers.hasOwnProperty(helper)) { 20 Handlebars.registerHelper(helper, helpers[helper]); 21 } 22 } 23};
1'use strict'; 2 3// The module to be exported 4var helpers = { 5 foo: function () { 6 return 'foo'; 7 }, 8 bar: function () { 9 return 'bar'; 10 } 11}; 12 13// Export helpers 14module.exports.register = function (Handlebars, options) { 15 options = options || {}; 16 17 for (var helper in helpers) { 18 if (helpers.hasOwnProperty(helper)) { 19 Handlebars.registerHelper(helper, helpers[helper]); 20 } 21 } 22};
Very simple Markdown for partial. do not forget to add your .md files to the partial list.
1options: { 2 partials: 'src/partials/*.md', 3 ... 4}
1/** 2 * Handlebars Markdown Helpers 3 * Copyright (c) 2014 Thierry Charbonnel 4 * Licensed under the MIT License (MIT). 5 */ 6'use strict'; 7 8var marked = require('marked'); 9 10// Export helpers 11module.exports.register = function (Handlebars, options) { 12 options = options || {}; 13 options.marked = options.marked || { 14 renderer: new marked.Renderer(), 15 gfm: true, 16 tables: true, 17 breaks: false, 18 pedantic: false, 19 sanitize: true, 20 smartLists: true, 21 smartypants: false 22 }; 23 24 Handlebars.registerHelper('md', function(name, context){ 25 var result; 26 marked.setOptions(options.marked); 27 // Convert inline markdown by prepending the name string with `:` 28 if(name.match(/^:/)) { 29 result = marked(name.replace(/^:/, '')); 30 } else { 31 try { 32 result = marked(Handlebars.partials[name]); 33 } catch(err) { 34 result = '<!-- error -->'; 35 } 36 } 37 return new Handlebars.SafeString(result); 38 }); 39}; 40
Add it in devDependencies and in gruntfiles.js
1"devDependencies": { 2 "handlebars-helper-moment": "*" 3},
Add it in gruntfiles.js
1handlebarslayouts: { 2 home: { 3 files: { 4 'dist/home.html': 'src/home.html', 5 'dist/index.html': 'src/index.html' 6 }, 7 options: { 8 ... 9 modules: ['src/helpers/helpers-*.js', 'handlebars-helper-moment'], 10 ... 11 } 12 } 13}
or ( v0.1.4 > )
1handlebarslayouts: { 2 home: { 3 files: { 4 'dist/*.html': 'src/*.hsb' 5 }, 6 options: { 7 ... 8 modules: ['src/helpers/helpers-*.js', 'handlebars-helper-moment'], 9 ... 10 } 11 } 12}
npm install
.
├── partials
| ├── footer.(html|hsb|md)
| └── header.(html|hsb|md)
├── pages
| ├── 2014-08-14-Handlebars-layouts-foo.(html|hsb)
| └── 2014-08-14-Handlebars-layouts-bar.(html|hsb)
├── data
| └── members.json
├── helpers
| └── helpers-*.js
└── index.html
1
2/*global module:false*/
3module.exports = function(grunt) {
4 'use strict';
5 grunt.initConfig({
6
7 handlebarslayouts: {
8 dev: {
9 files: {
10 //'dist/home.html': 'src/home.html'
11 'dist/*.html': 'src/*.hsb'
12 },
13 options: {
14 partials: ['src/partials/*.hbs', 'src/partials/*.md', 'src/layout.html'],
15 basePath: 'src/',
16 modules: ['src/helpers/helpers-*.js', 'handlebars-helper-moment'],
17 context: {
18 title: 'Layout Test',
19 projectName: 'Grunt handlebars layout',
20 items: [
21 'apple',
22 'orange',
23 'banana'
24 ]
25 }
26 }
27 }
28 },
29
30 connect: {
31 server: {
32 options: {
33 livereload: true,
34 port: 8000,
35 base:'dist/',
36 open: true
37 }
38 }
39 },
40
41 watch: {
42 layout: {
43 files: 'src/layout.html',
44 tasks: 'handlebarslayouts:dev'
45 },
46 hsb: {
47 files: 'src/**/*.hsb',
48 tasks: 'handlebarslayouts:dev'
49 },
50 options: {
51 livereload: true
52 }
53 },
54
55 });
56
57 grunt.loadTasks('../tasks');
58
59 grunt.loadNpmTasks('grunt-contrib-connect');
60 grunt.loadNpmTasks('grunt-contrib-watch');
61
62 grunt.registerTask('default', ['handlebarslayouts']);
63
64 grunt.registerTask('serve', ['handlebarslayouts', 'connect:server', 'watch']);
65
66};
67
68
69
70
71
=======
Copyright (c) 2014 Thierry Charbonnel, contributors.
Released under the MIT license
Sooooon. Help is welcome.
No vulnerabilities found.