erubis

Rails中的ERB中的<%,<%=,<%#和-%>有什么区别?

时间秒杀一切 提交于 2020-02-28 05:56:37
能否请您描述一下ERB文件中使用的以下字符的用法: <% %> <%= %> <% -%> <%# %> 每个有什么用? #1楼 Rails默认 不 使用 stdlib的ERB ,而是使用 erubis 。 资料来源: 该开发人员的评论 , ActionView的gemspec 接受了我在编写此 文档时所做的 合并请求 。 它们 之间行为的差异,特别是关于如何连字符运营 %- 和 -% 的工作。 文档稀缺, Ruby的ERB格式在哪里“正式”定义? 因此,以下是经验结论。 所有测试都假定: require 'erb' require 'erubis' 何时可以使用 - ERB:你必须通过 - 对 trim_mode 的选项 ERB.new 使用它。 erubis:默认启用。 例子: begin ERB.new("<%= 'a' -%>\nb").result; rescue SyntaxError ; else raise; end ERB.new("<%= 'a' -%>\nb" , nil, '-') .result == 'ab' or raise Erubis::Eruby.new("<%= 'a' -%> \n b").result == 'a b' or raise -% 是: ERB:如果下一个字符是换行符,则将其删除。 erubis: 在 <% %> (无 = )中

How to comment code in Rails views?

删除回忆录丶 提交于 2020-02-23 09:21:05
问题 As I'm playing with Rails and developing views I often want to comment out code. Simple enough with classes & models but views are a bit more tricky. What's best way to comment code in a view so it's not interpreted by, well, anything... HTML gives us <!-- commented Rails code here --> though code enclosed here seems to get interpreted anyway?!? Or is there a more Railsy way? 回答1: <% code code # comment %> USED to work but I think that was accidental. You were always supposed to put comments

Using a Rails helper method within a javascript asset

吃可爱长大的小学妹 提交于 2019-12-27 16:39:13
问题 Is there any way to use a Rails helper method, more specifically, a path helper method within a javascript asset file. This file foo.js.coffee.erb $('#bar').val("<%= create_post_path %>") I would love it if I could get from erubis $('#bar').val("path/to/create") 回答1: You can include any helper/module/class in an erb template with: <% environment.context_class.instance_eval { include MyHelper } %> See: https://github.com/rails/sprockets/blob/master/lib/sprockets/environment.rb and https:/

How to trim leading whitespace with `<%=` in ERB templates in Rails that end up in `pre` elements?

自古美人都是妖i 提交于 2019-12-23 18:34:24
问题 (As far as I have researched here, this is not a duplicate question. Trimming spaces -- often trailing newlines -- is being discussed for <%- or -%> , but not for <%= . It could be a minor defect in Erubi template engine as well, the one being used by Rails for ERB templates.) I want to render / syntax-highlight code in a view, and my ERB view template contains: <p> <strong>Code:</strong> <pre class="highlight github"> <%= highlight(@code.code, @code.language) %> </pre> </p> The result is

Erubis block helper throwing error with concat

a 夏天 提交于 2019-12-22 05:52:13
问题 I have a couple of block helpers, here's a simple example of what I'm doing: def wrap_foo foo, &block data = capture(&block) content = " <div class=\"foo\" id=\"#{foo}\"> #{data} </div>" concat( content ) end I'm just trying out erubis and it's giving me the following error: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.<< Removing the call to concat removes the error but ends up with my wrapper not being

Erubis block helper throwing error with concat

a 夏天 提交于 2019-12-22 05:52:01
问题 I have a couple of block helpers, here's a simple example of what I'm doing: def wrap_foo foo, &block data = capture(&block) content = " <div class=\"foo\" id=\"#{foo}\"> #{data} </div>" concat( content ) end I'm just trying out erubis and it's giving me the following error: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.<< Removing the call to concat removes the error but ends up with my wrapper not being

Erubis block helper throwing error with concat

岁酱吖の 提交于 2019-12-05 08:22:41
I have a couple of block helpers, here's a simple example of what I'm doing: def wrap_foo foo, &block data = capture(&block) content = " <div class=\"foo\" id=\"#{foo}\"> #{data} </div>" concat( content ) end I'm just trying out erubis and it's giving me the following error: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.<< Removing the call to concat removes the error but ends up with my wrapper not being rendered Using: Rails 2.3.5 Erubis 2.6.5 And tried this gem that helps Erubis (though 2.6.4) and Rails

Ruby 2.1 with erubis Template Engine

你。 提交于 2019-12-01 12:27:27
We are looking for fastest template engine for rendering of views. As i understand erubis is the fastest template engine in ruby. My usecase is render templates through script. Looking at the gem official page it's latest release was in 2011. Not sure if the community is active. https://rubygems.org/gems/erubis/versions Does anyone use ruby 2.1 with erubis template engine? Is it recommended to use erubis with ruby 2.1? Thanks Abhay abrocks I ran benchmark between ERB and erubis rendering with below code snippet. erubis_render_time = Benchmark.realtime { template_content = File.read("#{Rails

Ruby 2.1 with erubis Template Engine

一笑奈何 提交于 2019-12-01 11:14:52
问题 We are looking for fastest template engine for rendering of views. As i understand erubis is the fastest template engine in ruby. My usecase is render templates through script. Looking at the gem official page it's latest release was in 2011. Not sure if the community is active. https://rubygems.org/gems/erubis/versions Does anyone use ruby 2.1 with erubis template engine? Is it recommended to use erubis with ruby 2.1? Thanks Abhay 回答1: I ran benchmark between ERB and erubis rendering with