使用helper在handlebars.js中创建链接 [英] Create link in handlebars.js using helper

查看:150
本文介绍了使用helper在handlebars.js中创建链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照关于如何创建链接的handlebars文档中的示例,但说明对我来说很不清楚。 handlebarsjs.com/expressions.html (请参阅帮助者)



首先,如果没有链接助手,我可以让链接文本出现在屏幕上,但只是文本,而不是链接。

 < script id =templatetype =text / x-handlebars-template> 
< h4> {{{my_link}}}< / h4>
< / script>

然后从ajax请求中检索文本。
然后添加链接关键字

 < h4> {{link my_link}}}< / h4> ; 

然后不显示任何内容。这是我目前的帮手代码,它不起作用:

  $(function(){
Handlebars .registerHelper('link',function(my_link){

var url = Handlebars.escapeExpression(my_link.url);
var result =< a href ='+ url + '>< / a>;
console.log(result);

return new Handlebars.SafeString(result);

});
});

与JavaScript中的这段代码相关吗?



当我点击一个提交按钮时,就会发出ajax请求并检索链接。使用链接助手,console.log会提供一个空链接:

 < a href =''>< / A>中


解决方案

由于您将助手的名称注册为 link ,您的模板应该以 {{{link



所以

< h4> {{{my_link}}}< / h4>



应该是

< h4> {{link my_link}}}< / h4>



$ b $ var url = Handlebars.escapeExpression (my_link.url);



应改为

var url = Handlebars.escapeExpression(my_link);


I have tried to follow the example in the handlebars documentation on how to create a link, but the instructions are very unclear to me. handlebarsjs.com/expressions.html (see "Helpers")

First of all, without the link helper I can make the link text appear on the screen, but just as text, not a link.

<script id="template" type="text/x-handlebars-template">
          <h4>{{{my_link}}}</h4>
 </script>

And the text is retrieved from an ajax request. Then I add the link keyword

<h4>{{{link my_link}}}</h4>

And then nothing is displayed. This is the helper code I have currently, which doesn't work:

$(function(){
    Handlebars.registerHelper('link', function(my_link) {

        var url = Handlebars.escapeExpression(my_link.url);
        var result = "<a href='" + url + "'></a>";
        console.log(result);

        return new Handlebars.SafeString(result);

    });
});

Is it relevant where I put this piece of code in the javascript?

When I click a submit button, the ajax request is made and the link is retrieved. With the link helper, the console.log gives an empty link:

"<a href=''></a>"

解决方案

Since you registered the name of the helper as link, your template should start with {{{link

So

<h4>{{{my_link}}}</h4>

should be

<h4>{{{link my_link}}}</h4>

And

var url = Handlebars.escapeExpression(my_link.url);

should be changed to

var url = Handlebars.escapeExpression(my_link);

这篇关于使用helper在handlebars.js中创建链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆