Nunjucks中的Javascript函数

时间:2018-08-10 13:52:05

标签: javascript templating nunjucks

所以我已经在Nunjucks文档中找到了它:

函数调用

如果您已将javascript方法传递给模板,则可以像平常一样调用它。

  function remembrance_save()
  {
     // first thing, clear out the message div used for this (if there's anything there):
     document.getElementById("remembrance_message").innerHTML = "";

     // from here we need to get the contents of all the various entry areas, and pass them
     // off to a PHP file and save the data ...
     // copy award data to session object by use of ajax/php:
     var namecode    = document.getElementById("namecode").value;
     // get value from TinyMCE ... not the DOM for JavaScript:
     var remembrance = tinymce.get('remembrance').getContent();
     var cl          = document.getElementById("commentor_link").value;
     var cn          = document.getElementById("commentor_name").value;
     $.ajax
     ({
        type: "POST",
        url: "<?php echo $History_html_RootPath; ?>Who/AjaxCalls/remembrance_save.php",
        data: { 'namecode' : namecode,
                'remembrance' : remembrance,
                'commentor_link' : cl,
                'commentor_name' : cn },
        //cache: false,
        success: function(data)
        {
           // need to see if we have an error, if so, display it, otherwise,
           // we should hopefully have success ...
           if ( data.toLowerCase().includes( "error" ) )
           {
              var errormsg = "<div class='alert alert-danger'>"+data+"</div>";
              document.getElementById("remembrance_message").innerHTML = errormsg;
              return;
           }
           else
           {
              // success!
              // change the message, and then clear out the values:
              var message = "<div class='alert alert-success'>";
                  message += data;
                  message += "</div>";
              document.getElementById("remembrance_message").innerHTML = message;

              // clear out values:
              // the remembrance editor takes two, because TinyMCE (editor)
              // stores the values its own way
              document.getElementById("remembrance").value = "";
              tinymce.get('remembrance').setContent( "" );
              // the other two
              document.getElementById("commentor_link").value = "0";
              document.getElementById("commentor_name").value = "";
              return;
           }
        }  // end success
     }); // end ajax call
  }; // end function remembrance_save()

但是我似乎无法正常工作,我尝试将函数放在{{ foo(1, 2, 3) }} 标记的html页面上,但无法正常工作。

我还尝试将其与数据一起传递给渲染函数:

<script>

我得到:无法呼叫{ stuff: function (string, length) { while (string < length) { string = "0" + string } } }

1 个答案:

答案 0 :(得分:3)

您可以使用addGlobal(请参见g)或传递函数进行渲染(请参见f)。

var nunjucks  = require('nunjucks');
var env = nunjucks.configure();

function f (s) {
    return s + s; 
}

function g (s) {
    return s + s + s; 
}

env.addGlobal('g', g);

var res = nunjucks.renderString('{{f("OK")}} {{g("ok")}}', {f: f});    
console.log(res);

//Output: OKOK okokok