Last active
December 31, 2015 23:38
-
-
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 :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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