Nunjucks: nunjucks do not throw error when syntax error or none existing filter

Created on 12 Jun 2018  路  15Comments  路  Source: mozilla/nunjucks

hi guys, I have two templates: layout.nj and home.nj. home.nj extends layout.nj.

in somewhere of layout.nj, I have some syntax error, such as

{% set tpl_now = Date.now() %}

or use a filter not defined

{{user.userId | raw | default("null") }}

when I run my node server, env.render DO NOT throw error, and just return null silently.

maybe it's better to throw error in such cases, and so we can find out what is going wrong.

any advice is appreciated.

Thx in advance

bug

Most helpful comment

I agree that the wording of the docs is vague. One would expect, with no callback provided, for an error to be thrown by render()

All 15 comments

You need to check error in a callback of env.render (first argument). If it exists, you must throw it on your own. See docs

@ArmorDarks thx. But according to the docs,

Renders the template named name with the context hash. If callback is provided, it will be called when done with any possible error as the first argument and the result as the second. Otherwise, the result is returned from render and errors are thrown.

If I do not provide a callback, errors should be thrown.

@ArmorDarks I have tried the callback way. And it turns out, nunjucks do set error in the callback function, in case of using not existing filter.

But nunjucks still catch error silently when syntax error .
in case of this, {% set tpl_now = Date.now() %} , err in callback is null

If I do not provide a callback, errors should be thrown.

hm, well, never used it (because it's async, so you need to use callback anyway). Might be a bug

in case of this, {% set tpl_now = Date.now() %} , err in callback is null

Am I missing something? Where exactly there a syntax error?

@ArmorDarks seems like nunjucks do not support javascript native code. see #419 and #650(comment).

Date is not supported to be accessed globally. Even if I use addGlobal("Date", Date), still cannot use {% set tpl_now = Date.now() %}

seems like I've missed something.

{% set tpl_now = Date.now() %} works outside any {% block %}. But cannot work inside block

Date is not supported to be accessed globally. Even if I use addGlobal("Date", Date), still cannot use {% set tpl_now = Date.now() %}

addGlobal("Date", Date) should work and make Date accessible for templates. Are you sure you're calling addGlobal method on the configurated environment, which you're latter using for rendering?

@ArmorDarks yes, addGlobal("Date", Date) does work * outside {%blcok%} *. But cannot work inside block

Are you sure that you're properly passing Date to Nunjucks? I've tested with env.addGlobal('Date', Date), and {{ Date.now() }} works just fine. I was unable to reproduce the issue.

Also, are you sure that you're not using Date outside of the block in a template, which extends another template?

@ArmorDarks here is my case: I have a block in layout.nj like this:

<!doctype html>
{% block block_assign %}
{% set test_val = "hello" %}
{% set tpl_now = Date.now() %}
{% endblock %}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script>
    window.tplConf = {
        user : {
            now: '{{ tpl_now }}',
            test_val: '{{ test_val }}'
        },
    };
</script>

later in my template, accessing tpl_now and test_val is empty. It has nothing to do with Date, seems like I cannot access a variable outside the block where it is defined

later in my template, accessing tpl_now and test_val is empty. It has nothing to do with Date, seems like I cannot access a variable outside the block where it is defined

This is correct. Block creates a kind of closure or scope, so you can't access variables, defined within it, outside of it. If you want to access them outside, you need to define them outside of the block too.

@ArmorDarks thx a lot

I agree that the wording of the docs is vague. One would expect, with no callback provided, for an error to be thrown by render()

It took me a really long time to realize that there was an error in my code thinking that if there was nunjucks would complain, but apparently it only complains if there's a callback, and... yeah. Agreed.

Related: #678, #1127

Was this page helpful?
0 / 5 - 0 ratings