Javascript 在mustache javascript中使用参数调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6045165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:00:14  来源:igfitidea点击:

calling function with arguments in mustache javascript

javascriptmustache

提问by sinisa

Is it possible to call a function with arguments with Mustache.js

是否可以使用Mustache.js调用带参数的函数

{{somefunction(somevalue)}}
谢谢你

回答by bluehazetech

Check out the section on Lambdas at http://mustache.github.com/mustache.5.html

http://mustache.github.com/mustache.5.html查看有关 Lambda 的部分

Mustache template block:

小胡子模板块:

{{#someFunction}}someValue{{/someFunction}}

Function block:

功能块:

someFunction : function () {
  return function(val, render) {
    return "I passed in this value: " + render(val);
  };
}

Output:

输出:

I passed in this value: someValue

回答by Baz1nga

If you want the script contents to be executed after the markup is inserted nito the dom you should use some library that will do the same like jquery.

如果您希望在将标记插入 nito dom 后执行脚本内容,您应该使用一些库,它会像 jquery 一样执行相同的操作。

Try this out here:

在这里试试这个:

http://jsfiddle.net/anilkamath87/GBP8N/

http://jsfiddle.net/anilkamath87/GBP8N/

Also if you want to invoke some other method in your script file. All you need to do is call the function depending on the scope of that function and if it has been preloaded into the dom.

此外,如果您想在脚本文件中调用其他一些方法。您需要做的就是根据该函数的范围以及它是否已预加载到 dom 中调用该函数。

Hope this helps.

希望这可以帮助。

P.S: note the escape of the script tag in the template markup

PS:注意模板标记中script标签的转义

回答by Holle

for me this works:

对我来说这有效:

add general function FUNC to json (data):

将通用函数 FUNC 添加到 json(数据):

 data.FUNC = function(){
                return function(val, render){
                    var values = JSON.parse(render(val));
                    return window[values.FUNCNAME].apply(this, values.FUNCARGS);
                };
            };

regular javascript on page:

页面上的常规 javascript:

 function foo(arg1, arg2){
    return "Arg1 is " + arg1 +  " and Arg2 is " + arg2;
};

Mustache template block calling the regular javascript-function with tag-values as arguments:

Mustache 模板块使用标签值作为参数调用常规 javascript 函数:

{{#FUNC}}{"FUNCNAME":"foo", "FUNCARGS":["{{page}}","{{title}}"]}{{/FUNC}}

you also can call a function defined in the json:

您还可以调用 json 中定义的函数:

{{#calljsfunction}} {{#FUNC}}{"FUNCNAME":"{{calljsfunction}}", "FUNCARGS":["{{page}}","{{title}}"]}{{/FUNC}}{{/calljsfunction}}

{{#calljsfunction}} {{#FUNC}}{"FUNCNAME":"{{calljsfunction}}", "FUNCARGS":["{{page}}","{{title}}"]}{{/FUNC}}{{/calljsfunction}}

回答by scunliffe

Are you trying to call a function as part of your parsing of the mustache code? or generate output, that would call the JavaScript function? e.g. This would output HTML that would call the function (I believe).

您是否尝试调用函数作为解析 mustache 代码的一部分?或生成输出,那会调用 JavaScript 函数?例如,这将输出将调用该函数的 HTML(我相信)。

{{#items}}
  <script>{{funcName}}("{{url}}");</script>
{{/items}}