开发者

question on ruby erb templates

I have this script for generating config files. I am using ruby and its builtin ERB.

some of my templates consists of specific formats, spaces, braces etc

an example would be

command {
  var some_var;
}

My problem is my ERB templates are very dirty to look at because when I arrange the code in the template, it also destroys the output. I formatted the erb template in such a way that it will just look like clean normal code, in my case it has two spaces for indention. something like:

<% if var == 'something' %>
  command {
    var #{var};
  }
<% end %>

unfortunately, that doesn't produce my desired two spaced output like the example above this example. so what I had to do is something like:

<% if var == 'something' %>
command {
 var #{var};
}
<% end %>

erb template code above generates my desired output but it looks messy. especially when you have conditions under conditions. in the long run, it is very hard to read.

Are there any ways to improve this? Or is there another template language out ther开发者_如何学Goe suited for this? Also, maybe this is just me and I am open to opinions on improving.


I've fiddled a little with ERB, but found no way :/ Maybe you could try it with the <%- %> interpolation, that has something to do with indenting, although I couldn't find it in the rdoc of ERB.

You could also try HAML http://haml-lang.com/ - that's quite persistent on indenting. You can run Haml rendering from the command line or in Ruby code in addition to Rails templates:

irb
>> require 'rubygems'
>> gem 'haml'
>> require 'haml'
>> Haml::Engine.new("%bla").render(binding)
=> "<bla></bla>\n"

I've also found some references to other templating systems at the end of the ERB docs: http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/ , maybe that's a start.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜