Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nunjucks do not throw error when syntax error or none existing filter #1116

Closed
sophister opened this issue Jun 12, 2018 · 15 comments · Fixed by #1181
Closed

nunjucks do not throw error when syntax error or none existing filter #1116

sophister opened this issue Jun 12, 2018 · 15 comments · Fixed by #1181
Labels

Comments

@sophister
Copy link

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

@ArmorDarks
Copy link

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

@sophister
Copy link
Author

@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.

@sophister
Copy link
Author

@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

@ArmorDarks
Copy link

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?

@sophister
Copy link
Author

@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() %}

@sophister
Copy link
Author

seems like I've missed something.

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

@ArmorDarks
Copy link

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?

@sophister
Copy link
Author

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

@ArmorDarks
Copy link

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?

@sophister
Copy link
Author

@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

@ArmorDarks
Copy link

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.

@sophister
Copy link
Author

@ArmorDarks thx a lot

@dgirgenti
Copy link

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()

@simonv3
Copy link

simonv3 commented Aug 20, 2018

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.

@chocolateboy
Copy link

Related: #678, #1127

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants