从手柄栏帮手中获取模板中的值 [英] Get values from template in handlebars helper

查看:107
本文介绍了从手柄栏帮手中获取模板中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Handlebars作为我的模板。有没有一种方法可以使用helper使用的模板的值?



重要的是给定的值是一个字符串,并且以后是值。 / p>

示例:

  // Template.html 
< ; p> {{myHelper'这是一个awsome {name}'}}< / p>

Helper.js
Handlebars.registerHelper('myHelper',function(string){

// string'This is a awsome {name}'AND {name}是一个字符串,并且此时不是真实值

var myNewString = string.replace(\ {name} \,Handlebars.getValueByKey(name));
返回myNewString

});

如果 name 是Test,返回的值将是这是一个非常复杂的测试



name的值在另一个Helper中为该模板给出,其中name是Test作为字符串。



我正在寻找一个类似于我的例子中使用的函数 - getValueByKey



有没有一个典型的方法来做到这一点?我在官方文档中没有找到类似的内容。



编辑:对不起 - 我不知道为什么代码中的例子,盒子看起来像这样。

解决方案

你很近。关键是传递给你想要的模板:

  // Template.html 
< p> ; {{myHelper'这是一个很棒的'姓名}}< / p>

Helper.js
Handlebars.registerHelper('myHelper',函数(字符串,名称){

//字符串''这是一个很棒的'
// {name}的值作为另一个值传递
//如果您想用{name}做其他事情,您可以
//在
下面的函数中执行此操作
var myNewString = string + name;
return myNewString

});

希望更接近您的要求。


I am using Handlebars for my template. Is there a way to use the values from a template where the helper is in use ?

It is important that the given value is a string and later be the value.

Example:

// Template.html
<p>{{myHelper 'This is an awsome {name}'}}</p>

Helper.js
Handlebars.registerHelper('myHelper', function(string){

 // string is 'This is an awsome {name}' AND it is important that {name} is a string and at this point not the real value

 var myNewString = string.replace(\{name}\, Handlebars.getValueByKey(name));
 return myNewString

});

If name is "Test" the returned value will be This is an awsome Test

The value for name is given in another Helper for this template, where is defined that "name" is "Test" as a string.

I am looking for a function something like i used in my example - getValueByKey

Is there a typical way to do this ? I did not find anything like this in the offical documentation.

Edit: I am sorry - i dont know why the example in the code-box looks like this.

解决方案

You're close. The key is in passing in exactly what you want to the template:

// Template.html
<p>{{myHelper 'This is an awesome' name}}</p>

Helper.js
Handlebars.registerHelper('myHelper', function(string, name){

  // string is 'This is an awesome'
  // value of {name} is passed as another value
  // If you wanted to do other things with the {name} you could
  // do that in the function below

  var myNewString = string + name;
  return myNewString

});

Hope that's closer to what you were looking for.

这篇关于从手柄栏帮手中获取模板中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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