Skip to content

Instantly share code, notes, and snippets.

@neemzy neemzy/Gruntfile.js
Last active Dec 31, 2015

Embed
What would you like to do?
Un p'tit essai sur une génération de menu récursif avec Nunjucks. Bon c'est fait à l'arrache hein, à développer (sans mauvais jeu de mots), mais sur le principe ça marche comme je l'imaginais :)
module.exports = function(grunt)
{
// Nunjucks
grunt.registerTask('nunjucks', 'compilation du template', function(src, dest) {
var nunjucks = require('nunjucks');
grunt.file.write(
dest,
nunjucks.render(
src, {
menuitems: {
'menu 1': {
'submenu 1': { 'subsubmenu 1': null, 'subsubmenu 2': null },
'submenu 2': null
},
'menu 2': { 'submenu 1': null, 'submenu 2': null }
}
}
)
);
});
grunt.registerTask('template', ['nunjucks:templates/index.tpl:public/index.html']);
// Config
grunt.initConfig({
pkg: grunt.file.readJSON('package.json')
});
// Tasks
require('load-grunt-tasks')(grunt);
grunt.registerTask('default', ['template']);
};
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>menu récursif</title>
</head>
<body>
<nav class="menu" role="navigation">
{% include 'templates/tree.tpl' %}
</nav>
</body>
</html>
<ul>{% for key, item in menuitems %}
<li>{{ key }}
{% if item != null %}
{% set menuitems = item %}
{% include 'templates/tree.tpl' %}
{% endif %}
{% endfor %}</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.