I implemented the scripts that appending a child in the existing HTML body. When I append a new child, I want to use variable in the value.
我实现了将子项附加到现有HTML主体中的脚本。当我追加一个新孩子时,我想在值中使用变量。
For example:
例如:
date=Time.now.strftime("%Y-%m-%d")
orig_html = defect.Field(@td_columns[:"Comments"])
#XML module only provides html parsing without the DOCTYPE tag.
doc = Nokogiri::XML::DocumentFragment.parse(orig_html)
tmp1 = '<div align="left"><font face="Arial"><span style="font-size:9pt">#{date}</span></font></div>'
child1 = Nokogiri::XML::fragment(tmp1)
body = doc.at('body')
body.add_child(child1)
But, the date
variable is not displayed as an actual date (ex. 2015-02-09) but just #{date}
.
但是,日期变量不会显示为实际日期(例如2015-02-09),而只是#{date}。
Does anyone have advice to insert variables in HTML contents?
有没有人有建议在HTML内容中插入变量?
2 个解决方案
#1
1
You're using a single-quote delimited string which doesn't allow interpolation of variables. Instead you have to use a double-quote delimited string or one of the forms that allows interpolation:
您使用的是单引号分隔字符串,它不允许插入变量。相反,您必须使用双引号分隔的字符串或允许插值的其中一种形式:
var_a = 'var_b'
'#{ var_a }' # => "\#{ var_a }"
%q[#{ var_a }] # => "\#{ var_a }"
"#{ var_a }" # => "var_b"
%[#{ var_a }] # => "var_b"
%Q[#{ var_a }] # => "var_b"
Your code is too complicated. You can simplify it using:
你的代码太复杂了。您可以使用以下方法简化:
tmp1 = "<div align='left'><font face='Arial'><span style='font-size:9pt'>#{date}</span></font></div>"
body = doc.at('body')
body.add_child(tmp1)
Nokogiri is smart enough to know it should convert the contents of tmp1
into the equivalent HTML so you don't have to do it for it:
Nokogiri非常聪明,知道它应该将tmp1的内容转换为等效的HTML,因此您不必为此执行此操作:
require 'nokogiri'
date = Time.now
doc = Nokogiri::HTML('<html><body></body></html>')
tmp1 = "<div align='left'><font face='Arial'><span style='font-size:9pt'>#{date}</span></font></div>"
body = doc.at('body')
body.add_child(tmp1)
puts body.to_html
# >> <body><div align="left"><font face="Arial"><span style="font-size:9pt">2015-02-08 23:30:49 -0700</span></font></div></body>
#2
2
String interpolation only works with double quotes:
字符串插值仅适用于双引号:
foo = 'bar'
"foo #{foo} foo" # works and returns 'foo bar foo'
'foo #{foo} foo' # does not work: 'foo #{foo} foo'
That said, change the quotes to double quotes (and escape existing double quotes) in the following line:
也就是说,在以下行中将引号更改为双引号(并转义现有的双引号):
tmp1 = "<div align=\"left\"><font face=\"Arial\"><span style=\"font-size:9pt\">#{date}</span></font></div>"
#1
1
You're using a single-quote delimited string which doesn't allow interpolation of variables. Instead you have to use a double-quote delimited string or one of the forms that allows interpolation:
您使用的是单引号分隔字符串,它不允许插入变量。相反,您必须使用双引号分隔的字符串或允许插值的其中一种形式:
var_a = 'var_b'
'#{ var_a }' # => "\#{ var_a }"
%q[#{ var_a }] # => "\#{ var_a }"
"#{ var_a }" # => "var_b"
%[#{ var_a }] # => "var_b"
%Q[#{ var_a }] # => "var_b"
Your code is too complicated. You can simplify it using:
你的代码太复杂了。您可以使用以下方法简化:
tmp1 = "<div align='left'><font face='Arial'><span style='font-size:9pt'>#{date}</span></font></div>"
body = doc.at('body')
body.add_child(tmp1)
Nokogiri is smart enough to know it should convert the contents of tmp1
into the equivalent HTML so you don't have to do it for it:
Nokogiri非常聪明,知道它应该将tmp1的内容转换为等效的HTML,因此您不必为此执行此操作:
require 'nokogiri'
date = Time.now
doc = Nokogiri::HTML('<html><body></body></html>')
tmp1 = "<div align='left'><font face='Arial'><span style='font-size:9pt'>#{date}</span></font></div>"
body = doc.at('body')
body.add_child(tmp1)
puts body.to_html
# >> <body><div align="left"><font face="Arial"><span style="font-size:9pt">2015-02-08 23:30:49 -0700</span></font></div></body>
#2
2
String interpolation only works with double quotes:
字符串插值仅适用于双引号:
foo = 'bar'
"foo #{foo} foo" # works and returns 'foo bar foo'
'foo #{foo} foo' # does not work: 'foo #{foo} foo'
That said, change the quotes to double quotes (and escape existing double quotes) in the following line:
也就是说,在以下行中将引号更改为双引号(并转义现有的双引号):
tmp1 = "<div align=\"left\"><font face=\"Arial\"><span style=\"font-size:9pt\">#{date}</span></font></div>"