Skip to content

Commit

Permalink
ADD dynamic navigation items for header.inc.html
Browse files Browse the repository at this point in the history
FIX a typo: "heagerFooter" > "headerFooter"
ADD a property to templateJSON object in wrap.js middleware
ADD (placeholder) header navigation items in options.js, it's added into the modulesOptions.headerFooter object. (which was unused until now).
ADD an options property to headerFooter.js, seems to me that it there's a file specific for finding the template files and rendering the header & footer, this is the place to add the data about which navigation items the header contains.
  • Loading branch information
Norbert de Langen committed Feb 14, 2016
1 parent ed260ba commit 1a4165c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
11 changes: 8 additions & 3 deletions assets/templates/header.inc.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@

<nav class="source_header_nav">
<ul class="source_header_nav_lst">
<!-- Example markup -->
<!--<li class="source_header_nav_i"><a class="source_a_l" href="#777">Link</a></li>-->
<% for(var i=0; i<headerNavItems.length; i++) {%>
<% if (headerNavItems[i].path.match(/^http/)) { %>
<li class="source_header_nav_i"><a class="source_a_l" href="<%= headerNavItems[i].path %>" target="_blank"><%= headerNavItems[i].name %></a></li>
<% } else { %>
<li class="source_header_nav_i"><a class="source_a_l" href="<%= headerNavItems[i].path %>"><%= headerNavItems[i].name %></a></li>
<% } %>
<% } %>
</ul>
</nav>

<a class="js-hook source_login"></a>
</div>
</div>
</header>
</header>
28 changes: 15 additions & 13 deletions core/headerFooter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ var fs = require('fs');
var path = require('path');

exports.getHeaderAndFooter = function () {
var defaultTemplatePath = path.join(global.pathToApp, 'assets/templates');
var userTemplatePath = path.join(global.userPath, 'assets/templates');
var headerFile = 'header.inc.html';
var footerFile = 'footer.inc.html';
var defaultTemplatePath = path.join(global.pathToApp, 'assets/templates');
var userTemplatePath = path.join(global.userPath, 'assets/templates');
var headerFile = 'header.inc.html';
var footerFile = 'footer.inc.html';
var options = global.opts.assets.modulesOptions.headerFooter;

var output = {};
var output = {};

var userHeaderTplPath = path.join(userTemplatePath, headerFile);
output.headerPath = fs.existsSync(userHeaderTplPath) ? userHeaderTplPath : path.join(defaultTemplatePath, headerFile);
var userHeaderTplPath = path.join(userTemplatePath, headerFile);
output.headerPath = fs.existsSync(userHeaderTplPath) ? userHeaderTplPath : path.join(defaultTemplatePath, headerFile);

var userFooterTplPath = path.join(userTemplatePath, footerFile);
output.footerPath = fs.existsSync(userFooterTplPath) ? userFooterTplPath : path.join(defaultTemplatePath, footerFile);
var userFooterTplPath = path.join(userTemplatePath, footerFile);
output.footerPath = fs.existsSync(userFooterTplPath) ? userFooterTplPath : path.join(defaultTemplatePath, footerFile);

output.header = fs.readFileSync(output.headerPath, 'utf-8');
output.footer = fs.readFileSync(output.footerPath, 'utf-8');
output.header = fs.readFileSync(output.headerPath, 'utf-8');
output.footer = fs.readFileSync(output.footerPath, 'utf-8');
output.options = options;

return output;
};
return output;
};
17 changes: 9 additions & 8 deletions core/middlewares/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,31 @@ exports.process = function (req, res, next) {
content = $.html();
}

var heagerFooter = getHeaderAndFooter();
var headerFooter = getHeaderAndFooter();

// final data object for the template
var templateJSON = {
engineVersion: global.engineVersion,
content: content,
head: head,
info: info
info: info,
headerNavItems: headerFooter.options.headerNavItems
};

try {
templateJSON.header = ejs.render(heagerFooter.header, templateJSON);
templateJSON.header = ejs.render(headerFooter.header, templateJSON);
} catch(err){
var headerMsg = 'Error: EJS could render header template: ' + heagerFooter.headerPath;
var headerMsg = 'Error: EJS could render header template: ' + headerFooter.headerPath;
templateJSON.header = headerMsg;
global.log.warn(headerMsg, err.stack);
}

try {
templateJSON.footer = ejs.render(heagerFooter.footer, templateJSON, {
filename: heagerFooter.footerPath
templateJSON.footer = ejs.render(headerFooter.footer, templateJSON, {
filename: headerFooter.footerPath
});
} catch(err){
var footerMsg = 'Error: EJS could render footer template: ' + heagerFooter.footerPath;
var footerMsg = 'Error: EJS could render footer template: ' + headerFooter.footerPath;
templateJSON.footer = footerMsg;
global.log.warn(footerMsg, err.stack);
}
Expand All @@ -129,4 +130,4 @@ exports.process = function (req, res, next) {
// proceed to next middleware
next();
}
};
};
8 changes: 8 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ module.exports = {
// Page navigation hash update on scroll turned off because of performance issues
updateHash: false
},
headerFooter: {
headerNavItems: [
{name: 'Home', path: '/'},
{name: 'Docs', path: '/docs/'},
{name: 'Twitter', path: 'http://twitter.com/sourcejs'},
{name: 'Mail', path: 'mailto:address@example.com'}
]
},

search: {
autofocusOnNavigationPage: true,
Expand Down

0 comments on commit 1a4165c

Please sign in to comment.