title: nunjucks function arguments arrive undefined
body: <p>I have been using nunjucks for several months, and have found it to be a great templating engine. However, this morning I ran into an issue that seems simple, but I simply cannot figure it out. I am hoping that another set of eyes can help point to the solution.</p>
  
  <p>The problem: If I pass a function to a template, any arguments passed to that function are undefined inside the function body. </p>
  
  <p>Values and objects can be passed to templates without a problem, and if I pass a function I can log to the console from within the function (so I know the function itself is there), but the arguments are all undefined.</p>
  
  <p>This initially seemed like something that could be solved with a closure, but 1) I dont see closures in any examples I can find, and 2) when I tried a closure I found that they too receive undefined arguments.</p>
  
  <p>Through the course of the day I have pared my code back to just about the simplest possible case and still cant figure this out:</p>
  
  <p>The template:</p>
  
  <pre><code>&lt;div&gt;
  
  &lt;p&gt;{{ value }}&lt;/p&gt;
  
  &lt;p&gt;{{ object|pretty }}&lt;/p&gt;
  
  &lt;p&gt;{{ func(4) }}&lt;/p&gt;
  
  &lt;p&gt;{{ args(4)|pretty }}&lt;/p&gt;
  
  &lt;p&gt;{{ local(4) }}&lt;/p&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>The code that renders the template (this is inside of a requirejs define, not shown):</p>
  
  <pre><code>    var nunjucks = require('lib/nunjucks-slim.min'), // v1.3.4
          env = new nunjucks.Environment(null), // global templates
          $tgt = $('#test'),
          local;
  
      env.addGlobal('value', 3);
  
      env.addGlobal('object', {
          a: 2
      });
  
      env.addFilter('pretty', function (obj) {
          return JSON.stringify(obj, null, 2);
      });
  
      env.addGlobal('func', function (val) {
          return 'func: ' + val;
      });
  
      env.addGlobal('args', function () {
          return arguments;
      });
  
      local = function (val) {
          return 'local: ' + val;
      };
  
      $tgt.html(env.render('test.nj', {
          'local': local
      }));
  </code></pre>
  
  <p>and the rendered HTML looks like this:</p>
  
  <pre><code>3                  // value
  
  { "a": 2 }         // object|pretty
  
  func: undefined    // func
  
  {}                 // args
  
  local: undefined   // local
  </code></pre>
  
  <p>So, a value is fine. The object works as well, and even looks nice after passing it through the "pretty" filter.</p>
  
  <p>However, a value passed to "func" becomes undefined in the function body, and using the arguments variable inside the function doesnt help. Furthermore, passing a function directly into the template context (local) doesnt work either.</p>
  
  <p>Filters (which are basically just functions) work fine, but regular functions dont, whether they are passed as part of the template context or as a global.</p>
  
  <p>A few notes that might help:</p>
  
  <ul>
  <li><p>I am using requirejs although (except for importing nunjucks itself) I have tried to eliminate it from the equation.</p></li>
  <li><p>This example uses nunjucks v1.3.4. Through the course of debugging I tried to verify this behavior in v2.1 but it appears that v2.x may have broken requirejs compatibility, and I didnt want to tackle two problems in parallel.</p></li>
  <li><p>I am precompiling my templates using grunt-nunjucks, then uglifying the result. The relevant portions of my Gruntfile are as follows:</p>
  
  <pre><code>module.exports = function (grunt) {
  'use strict';
  
  grunt.initConfig({
  
  pkg: grunt.file.readJSON('package.json'),
  
  nunjucks: {
      common: {
          baseDir: 'site/',
          src: [
              'site/core/**/*.nj'
          ],
          dest: 'application/static/js/common.js'
      },
  },
  
  uglify: {
      templates: {
          options: {
              preserveComments: false,
              mangle: true,
              compress: {
                  dead_code: true,
                  loops: true,
                  conditionals: true,
                  booleans: true,
                  unused: true,
                  if_return: true,
                  join_vars: true,
                  drop_console: true
              }
          },
          files: {
              'application/static/js/common.min.js': ['application/static/js/common.js'],
          }
      }
  },
  });
  
  grunt.loadNpmTasks('grunt-nunjucks');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  };
  </code></pre></li>
  </ul>
  
  <p>The test template (shown above) is one of several that are compiled into common.js, then uglified into common.min.js using the settings above. Note that I have tried compiling the templates without uglifying, but get the same result.</p>
  
  <p>This is pretty basic functionality so If this were a bug I would expect to see lots of references on SE and in the issue log, but I could hardly find any. I assume I am missing something really obvious, but just cant seem to find it.</p>
  
  <p>Any thoughts?</p>