Skip to content

Instantly share code, notes, and snippets.

@neemzy
Last active December 31, 2015 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neemzy/8060917 to your computer and use it in GitHub Desktop.
Save neemzy/8060917 to your computer and use it in GitHub Desktop.
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