如何在没有Web服务器的情况下从命令行执行ruby模板文件(ERB)? [英] How do I execute ruby template files (ERB) without a web server from command line?

查看:91
本文介绍了如何在没有Web服务器的情况下从命令行执行ruby模板文件(ERB)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 ERB (Ruby的模板系统)来模板化非HTML文件。

(相反,我想将其用于源文件,例如。 java,.cs,...)



如何从命令行执行 Ruby模板?

解决方案

您应该在ruby / bin目录中拥有所需的一切。在我的(WinXP,Ruby 1.8.6)系统上,我有 ruby​​ / bin / erb.bat

  erb.bat [开关] [输入文件] 
-x打印ruby脚本
-n打印行号为
的ruby脚本-v启用详细模式
-d将$ DEBUG设置为true
-r [库]加载库
-K [kcode]指定KANJI代码集
-S [safe_level]设置$ SAFE(0 .. 4)
-T [trim_mode]指定trim_mode(0..2,-)
-P忽略以%开头的行

所以 erb your_erb_file.erb 应该将结果写入STDOUT。



(编辑:Windows具有erb.bat,只是普通的 erb。.bat文件只是erb的包装,我想应该使同一命令在任何操作系统上的工作原理都差不多)



请参见 prag prog book 讨论(开始一个

还请注意,杰克·赫林顿(Jack Herrington)写了整本书有关使用Ruby / ERB的代码生成。


I need ERB (Ruby's templating system) for templating of non-HTML files.
(Instead, I want to use it for source files such as .java, .cs, ...)

How do I "execute" Ruby templates from command line?

解决方案

You should have everything you need in your ruby/bin directory. On my (WinXP, Ruby 1.8.6) system, I have ruby/bin/erb.bat

erb.bat [switches] [inputfile]
  -x               print ruby script
  -n               print ruby script with line number
  -v               enable verbose mode
  -d               set $DEBUG to true
  -r [library]     load a library
  -K [kcode]       specify KANJI code-set
  -S [safe_level]  set $SAFE (0..4)
  -T [trim_mode]   specify trim_mode (0..2, -)
  -P               ignore lines which start with "%"

so erb your_erb_file.erb should write the result to STDOUT.

(EDIT: windows has erb.bat and just plain "erb". The .bat file is just a wrapper for erb, which I guess should make the same command work pretty much the same on any OS)

See the prag prog book discussion (starts about half-way down the page).

Note also that Jack Herrington wrote a whole book about code generation that uses Ruby/ERB.

这篇关于如何在没有Web服务器的情况下从命令行执行ruby模板文件(ERB)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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