使用ERB和Handlebars模板 [英] Using ERB with Handlebars templates

查看:79
本文介绍了使用ERB和Handlebars模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用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编译该文件首先作为 .erb 文件(将 erb 数据注入到 .hbs file),那么Rails将把该文件渲染为一个 .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 @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>

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 .hbs.erb at the end of it, rather than simply .hbs .

This will encourage Rails to compile the file first as an .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).

这篇关于使用ERB和Handlebars模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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