使用ERB和Handlebars模板 [英] Using ERB with Handlebars templates
问题描述
我有一个使用ajax制作新标签的模式。它使用标记参数执行POST方法,而无需重新充电视图。所以我想根据所选的price_type参数来渲染一个或多个价格。我使用Handlebars,所以我想这不是ruby的条件响应(因为AJAX),但Handlebars条件的响应性。
问题是我尝试了很多方法来制作Handlebars条件没有成功。
我想用Handlebars条件达到这个效果,其中 @ tag.price_type
存储为<$ JS中的c $ c> {{price_type}} 。
< script id =entry-template类型因为我使用的是AJAX,所以如果我尝试这样做,它将无法正常工作。 = 文本/ X-车把模板 >
<%if if tag.price_type === 1%>
< span class =small right white article_price> {{min_price}}€< / span>
<%elsif @ tag.price_type === 2%>
< span class =small right white article_price> {{min_price}}€< / span> - < span class =small right white article_price> {{max_price}}€< / span>
<%else%>
< span class =small right white article_price>无sabe< / span>
<%end%>
< / script>
可能吗?也许将文件保存为hbs.erb?
如果不是,那么纯手车牌模板将如何?我试着制作一个我不明白的定制助手,但没有任何东西。谢谢你们。 ,而不是简单的 .hbs
。
这将鼓励Rails编译该文件首先作为 I have a modal that makes new tags with ajax. It does a POST method with the Tags parameters without recharging the view. So I want, depending on the "price_type" parameter chosen, to render one price div or another. Im using Handlebars so I suppose this is not ruby's conditional responsability (because of AJAX), but Handlebars conditionals responsability. The problem is I tried plenty of ways of making Handlebars conditionals with no success. I want to achieve the equivalent of this with Handlebars conditionals, where Is it possible? Maybe saving the file as hbs.erb?
If not, how would the pure Handlebars template be? I tried making a custom Helper which I didn't understand, but nothing. Thank you guys. You need to name your file something with This will encourage Rails to compile the file first as an 这篇关于使用ERB和Handlebars模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! .erb
文件(将 erb
数据注入到 .hbs
file),那么Rails将把该文件渲染为一个
.hbs
(Rails按照文件扩展名从右到左的顺序编译文件)。 > @tag.price_type
is stored as {{price_type}}
in JS. Since I'm using AJAX, if I try this it wont work. <script id="entry-template" type="text/x-handlebars-template">
<% if @tag.price_type === 1 %>
<span class="small right white article_price">{{min_price}} €</span>
<% elsif @tag.price_type === 2 %>
<span class="small right white article_price">{{min_price}} €</span> - <span class="small right white article_price">{{max_price}} €</span>
<% else %>
<span class="small right white article_price">No sabe</span>
<% end %>
</script>
.hbs.erb
at the end of it, rather than simply .hbs
..erb
file (injecting erb
data into the .hbs
file), then Rails will render the file as a .hbs
(Rails compiles files in the order of file extensions from right to left).