【问题标题】:Liquid filters not working (rails app)液体过滤器不起作用(rails 应用程序)
【发布时间】:2014-03-26 12:21:40
【问题描述】:

我使用 Liquid 作为用户可以从我的网站下载的价格标签的模板语言。

Liquid 单独可以正常工作,但是没有自定义过滤器可以工作。他们根本什么都不做。

这是一个例子:

$ {{item.primary_offer.subscription.month_price | no_decimal}},- per month

这应该会导致:

$ 199,- per month.

但是过滤器不起作用所以结果是:

$ 199.0,- per month.

我的过滤器文件如下所示:

    module LiquidFilters
      include ActionView::Helpers::NumberHelper

      def no_decimal(input)
        number_to_currency(input.to_f, precision: 0)
      end

    end

Liquid::Template.register_filter(LiquidFilters)

呈现液体输入的应用程序助手如下所示:

def liquidize(content, arguments)
  Liquid::Template.parse(content).render(arguments, :filters => [LiquidFilters])
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 liquid


    【解决方案1】:

    我通过将 LiquidFilters 模块移动到应用程序帮助文件(在 ApplicationHelper 模块下)解决了这个问题。

    一切突然奏效。

    我不明白为什么这会有所作为,但确实有效。如果有人可以解释这一点,请支持他们的答案。 :)

    【讨论】:

    • 这对我不起作用,ruby 2.1.5p273 - rails 4.1.8。 Liquid 只是忽略过滤器并返回一个空白。
    猜你喜欢
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 2018-10-06
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多