Variables and templates inheritance

In Nunjucks, inheriting templates can have private variables.

Variables and blocks

When you rely on template inheritance using the {% extends %} declaration , you cant’ have a {% block %}{% endblock %} inside a variable set block. The other way, a variable declared inside a {% block %}{% endblock %} will not be accessible outside. Therefore, you can’t gather all variables in a global variable passed as an argument to the processing filter. However, you still have the possibility to process each block individualy.

Variables scope

When using variables declared with the set block, never forget that any variable whose name begins with one or more underscore character is private. Therefore it can not be imported outside of its block scope unfortunatly, the Nunjucks documentation doesn’t state it: I found this important note in the Jinja2 documentation. .

Comments

Home

Github

Twitter

RSS

All tips

  1. ...1 Site structure Site structure and directories
  2. ...2 Cloning 11tyTips site Start bloging with a clean site structure
  3. ...3 Source tree A bird's-eye view of the site
  4. ...4 Eleventy settings The main components of an Eleventy site build
  5. ...5 Global settings Global data for frequent use
  6. ...6 Styles guide Styles listing
  7. ...7 Names guide How variable identifiers are forged
  8. ...8 Front matter Accessing front matter data in Markdown and templates
  9. ...9 Shortcodes Processing Markdown content with shortcodes
  10. ..10 Minifying styles and script Generate minified CSS and JavaScript as inline blocks or files
  11. ..11 Using shorthands Use shorthand notation to simplify and clean your markdown
  12. ..12 Front matter post processing function A front matter function can be invoqued to modify the global or partial output of the template engine
  13. ..13 Processing before and after the template engine Add any kind of processing before and after your templating engine has done its work
  14. ..14 Variables scope and templates inheritance Take care of scope rules when using template inheritance
  15. ..15 Creating a menu A smart menu of posts is hard to create
  16. ..16 CSS mixins Filters ala SASS
  17. ..17 Light(ning) images How to slim overweight images