简体   繁体   中英

Nunjucks console log not working as node js template engine

I am new in node.js "nunjucks" templating, i have got most of the information available in nunjucks docs but i don't know how to console the variable init?

i tried following but didn't worked for me:

{{ console.log(varible) }}

the following worked fine on the front end for me:

If you want to log "Cabin" as a string you can use

<script> console.log( "Cabin" )</script>

If you want to log a variable, try:

{% set thing=5 %}
<script> console.log({{ thing }})</script>

...which returns 5 (the number)

EDIT: As Valorad pointed out below, if the 'thing' you're trying to point out is not a string, you may have to do some processing and add a few filters before the console.log(); outputs the proper info in the console, such as:

<script> console.log(JSON.stringfiy({{ thing }}))</script>

OR, more likely:

<script> console.log('{{ data.page | dump | safe }}')</script>

Thanks Valorad for reporting back!

<script>
    console.log({{ surveyData | dump | safe}})
</script>

I would recommend using it this way.

dump(2) indents the object to be readable.

<script>console.log("data:", `{{ data | dump(2) | safe }}`)</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM