繁体   English   中英

如何在Ruby中使用ERB文件在文本文件中写入一些文本

[英]How to use a ERB file in Ruby to write some text in a text file

我有一个play.erb文件:

<p>Welcome!<br><p>
<% if $wgplaying==true %>
        Template: <%= $template %>
<% end %>
<br><br>

<% if $wginword==true && $wgplaying==true && $a!='' %>
        Character <%= $a %> is in word.
<% end %>

<% if $wginword==false && $wgplaying==true && $a!='' %>
        Character <%= $a %> is not in word.
<% end %>   

<br><br>

我想写

"Character <%= $a %> is in word."

要么

"Character <%= $a %> is not in word." 

在文本文件中,但是我不知道应该使用什么命令。 或者,我应该在Ruby文件中执行此操作吗?

ERB在Ruby的标准库中,因此请尝试以下操作:

require 'erb'

TEMPLATE_FILE = 'template.erb'

$wgplaying = true
$template = "This is my template"
$wginword = true
$wgplaying = true
$a = "something"

render = ERB.new(File.read(TEMPLATE_FILE),trim_mode: ">")
puts render.result

new方法有很多选择。

这些是trim_mode选项:

%  enables Ruby code processing for lines beginning with %
<> omit newline for lines starting with <% and ending in %>
>  omit newline for lines ending in %>
-  omit blank lines ending in -%>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM