javascript - Stop Nunjunks from spacing out specific tags - Stack Overflow

I have a combination of Google Apps Script logic and JavaScript that I am using within my Nunjucks envi

I have a combination of Google Apps Script logic and JavaScript that I am using within my Nunjucks environment. The logic looks like this within one of my views


//...Nunjucks logic above

<script>
   //debug | see parameter data in terminal and on page
    function displayParameterData() {
      const datags = <?!= JSON.stringify(data) ?>;
      console.log(datags);
      document.write(JSON.stringify(datags));
   }
   displayParameterData();
</script>

//...Nunjucks logic below

When I build my npm project, Nunjucks makes the following adjustment to my datags value above... < ? ! = JSON.stringify(data) ? > ;

Is there anyway I can tell Nunjucks to not make that adjustment and leave the markup exactly how it is in this specific area?

I found other resources on stack in regards to Nunjucks espcaping, but nothing close enough to the code setup of my situation..

Thanks for any advice

I have a combination of Google Apps Script logic and JavaScript that I am using within my Nunjucks environment. The logic looks like this within one of my views


//...Nunjucks logic above

<script>
   //debug | see parameter data in terminal and on page
    function displayParameterData() {
      const datags = <?!= JSON.stringify(data) ?>;
      console.log(datags);
      document.write(JSON.stringify(datags));
   }
   displayParameterData();
</script>

//...Nunjucks logic below

When I build my npm project, Nunjucks makes the following adjustment to my datags value above... < ? ! = JSON.stringify(data) ? > ;

Is there anyway I can tell Nunjucks to not make that adjustment and leave the markup exactly how it is in this specific area?

I found other resources on stack in regards to Nunjucks espcaping, but nothing close enough to the code setup of my situation..

Thanks for any advice

Share Improve this question edited Mar 25 at 21:59 Progman 19.7k7 gold badges55 silver badges82 bronze badges asked Mar 25 at 4:37 klewisklewis 8,41816 gold badges69 silver badges112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

The issue you’re running into is that Nunjucks is escaping the < ? ! = ... ? > syntax because it doesn’t recognize it as part of its templating system. Nunjucks provides a {% raw %} block that prevents the templating engine from processing specific parts of your code

Try this out,

{% raw %}
<script>
   //debug | see parameter data in terminal and on page
    function displayParameterData() {
      const datags = <?!= JSON.stringify(data) ?>;
      console.log(datags);
      document.write(JSON.stringify(datags));
   }
   displayParameterData();
</script>
{% endraw %}

After much testing, what worked for me was actually modifying my .pipe() process within my gulp-nunjucks-render settings, which is an npm package within my gulpfile.js file.

In detail, right before my Nunjucks .html files are sent to the /appsscript folder...

//DETERMINES IF WE ARE READY TO PREP AND PUSH TO GOOGLE
  const GPREP = yargs.argv.gprep;

  //...more code...

  .pipe(gulpif(GPREP, dest(mApps)))

...I am now using two additional pipe processes to find and replace ("correct"), the Google Apps Script tags, using the npm packages gulp-replace...

.pipe(gulpif(GPREP, replace('< ? !','<?!')))
.pipe(gulpif(GPREP, replace('? > ;','?>;')))
.pipe(gulpif(GPREP, dest(mApps)))

As a result, when I look at my .html file in my /appsscript folder, the script is now corrected...

<script>
      //debug | see parameter data in terminal and on page
      function displayParameterData() {
          const datags = <?!= JSON.stringify(data) ?>;
          console.log(datags);
          document.write(JSON.stringify(datags));
      }
      displayParameterData();
  </script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744217649a4563630.html

相关推荐

  • javascript - Stop Nunjunks from spacing out specific tags - Stack Overflow

    I have a combination of Google Apps Script logic and JavaScript that I am using within my Nunjucks envi

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信