Skip to content

Commit 68f2e3d

Browse files
committed
Update pages task
Use frontmatter for layout specifying Update pages
1 parent bb4d5a1 commit 68f2e3d

27 files changed

Lines changed: 3031 additions & 3090 deletions

File tree

build/paths/app.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ var destDir = config.destDir;
4242

4343
exports.themes = srcDir + "/**/*-theme.scss";
4444

45+
/***********************************************
46+
* Application template files
47+
************************************************/
48+
49+
/*
50+
All template files in application.
51+
Those should registered as handlebars partials
52+
in order to use feature like includes or layouts
53+
*/
54+
55+
exports.templates = srcDir + "/**/*.hbs";
56+
4557
/***********************************************
4658
* Application page files
4759
************************************************/
@@ -54,18 +66,17 @@ var destDir = config.destDir;
5466

5567
exports.pages = srcDir + "/**/*-page.hbs";
5668

57-
5869
/***********************************************
59-
* Application template files
70+
* Application layout files
6071
************************************************/
61-
72+
6273
/*
63-
All template files in application.
64-
Those should registered as handlebars partials
65-
in order to use feature like includes or layouts
74+
Layouts are used for "wrapping" the content of individual pages with common elements,
75+
such as the <head></head> and footer sections, which usually contain necessities
76+
such as <link> and <script> tags.
6677
*/
6778

68-
exports.templates = srcDir + "/**/*.hbs";
79+
exports.layouts = srcDir + "/**/*-layout.hbs";
6980

7081
/***********************************************
7182
* Application handlebars helpers files
@@ -82,17 +93,6 @@ var destDir = config.destDir;
8293
];
8394

8495

85-
/***********************************************
86-
* Application layout files
87-
************************************************/
88-
89-
/*
90-
Layouts are used for "wrapping" the content of individual pages with common elements,
91-
such as the <head></head> and footer sections, which usually contain necessities
92-
such as <link> and <script> tags.
93-
*/
94-
95-
exports.layouts = srcDir + "/**/*-layout.hbs";
9696

9797
/***********************************************
9898
* Application asset files

build/tasks/app-pages.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,66 @@
11
var path = require('path');
22
var glob = require('glob');
3-
var fs = require('fs');
3+
var fs = require('fs-extra');
4+
var through = require('through2');
45
var extend = require('util')._extend;
56

67
var config = require('../config');
78

89
module.exports.task = function(gulp, plugins, paths) {
9-
10+
11+
// Handlebars engine
12+
var handlebars = new require('handlebars');
13+
var handlebarsRegistrar = require('handlebars-registrar');
14+
15+
// Register handlebars engine helpers and partials
16+
handlebarsRegistrar(handlebars, {
17+
helpers: paths.app.helpers,
18+
partials: paths.app.templates,
19+
parsePartialName: function (file) {
20+
return file.shortPath;
21+
},
22+
});
23+
1024
gulp.src(paths.app.pages)
1125
// Frontmatter
1226
.pipe(plugins.frontMatter())
13-
// handlebars compilation
14-
.pipe(plugins.hb({
15-
// Register all templates as partials
16-
partials: paths.app.templates,
17-
// Partials naming e.g. 'app/app-layout'
18-
parsePartialName: function (file) {
19-
return file.shortPath;
20-
},
21-
// Registering template helpers
22-
helpers: paths.app.helpers,
23-
// Context data for each page file
24-
dataEach: function (context, file) {
25-
26-
var contextExtended = extend(context, getPageContext(file));
27-
contextExtended = extend(contextExtended, file.frontMatter);
28-
29-
return contextExtended;
30-
},
31-
// Remove cache every time for 'watch'
32-
bustCache: true
27+
// Render pages
28+
.pipe(through.obj(function (file, enc, cb) {
29+
// Page render result
30+
var pageRes = "";
31+
32+
// Get context from _context.js files and frontmatter
33+
var context = getPageContext(file);
34+
context = extend(context, file.frontMatter);
35+
36+
// Compile template
37+
var template = handlebars.compile(String(file.contents));
38+
var templateRes = template(context);
39+
40+
// Layout processing
41+
var layout = file.frontMatter.layout || null;
42+
43+
// If the layout exists, render it with template inside
44+
if (layout && handlebars.partials[layout]) {
45+
var layoutData = extend(context, {
46+
body: templateRes
47+
});
48+
49+
// Render layout with given context and content
50+
var layoutRes = handlebars.partials[layout](layoutData);
51+
52+
pageRes = layoutRes;
53+
}
54+
// Return rendered template
55+
else {
56+
pageRes = templateRes;
57+
}
58+
59+
file.contents = new Buffer(pageRes);
60+
61+
this.push(file);
62+
cb();
3363
}))
34-
3564
// Handle errors
3665
.on('error', plugins.util.log)
3766

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
"url": "https://github.com/modularcode/modular-admin-html/issues"
1313
},
1414
"homepage": "https://github.com/modularcode/modular-admin-html",
15-
"dependencies": {
16-
},
15+
"dependencies": {},
1716
"devDependencies": {
17+
"fs-extra": "^0.26.4",
1818
"glob": "^5.0.14",
1919
"gulp": "^3.9.0",
2020
"gulp-autoprefixer": "^2.3.1",
@@ -33,8 +33,11 @@
3333
"gulp-uglify": "^1.2.0",
3434
"gulp-util": "^3.0.6",
3535
"gulp-watch": "^4.3.4",
36+
"handlebars": "^4.0.5",
3637
"handlebars-layouts": "^3.1.0",
37-
"main-bower-files": "^2.9.0"
38+
"handlebars-registrar": "^1.5.2",
39+
"main-bower-files": "^2.9.0",
40+
"through2": "^2.0.0"
3841
},
3942
"scripts": {
4043
"gulp": "gulp --gulpfile build/gulpfile.js",

src/app/_common/sidebar/sidebar.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
<ul>
4545
<li {{#is page "===" "charts-flot"}}class="active"{{/is}} >
4646
<a href="charts-flot.html">
47-
Flot Chart
47+
Flot Charts
4848
</a>
4949
</li>
5050

5151
<li {{#is page "===" "charts-morris"}}class="active"{{/is}} >
5252
<a href="charts-morris.html">
53-
Morris Chart
53+
Morris Charts
5454
</a>
5555
</li>
5656
</ul>

src/app/app-blank-layout.hbs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
<div class="app blank sidebar-opened">
3232
{{!-- Content section --}}
3333
<article class="content">
34-
{{#block "body"}}
35-
36-
{{/block}}
34+
{{{body}}}
3735
</article>
3836
</div>
3937
<!-- Reference block for JS -->

src/app/app-layout.hbs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,18 @@
3131
<div class="main-wrapper">
3232
<div class="app" id="app">
3333
{{!-- Default header block --}}
34-
{{#block "header"}}
35-
{{> app/_common/header/header}}
36-
{{/block}}
34+
{{> app/_common/header/header}}
3735

3836
{{!-- Default sidebar block --}}
39-
{{#block "sidebar"}}
40-
{{> app/_common/sidebar/sidebar}}
41-
{{/block}}
37+
{{> app/_common/sidebar/sidebar}}
4238

4339
{{!-- Content section --}}
4440
<article class="content {{page}}-page">
45-
{{#block "body"}}
46-
47-
{{/block}}
41+
{{{body}}}
4842
</article>
4943

5044
{{!-- Default footer block --}}
51-
{{#block "footer"}}
52-
{{> app/_common/footer/footer}}
53-
{{/block}}
45+
{{> app/_common/footer/footer}}
5446

5547
{{> app/_common/modals/modals}}
5648
</div>

0 commit comments

Comments
 (0)