Html 从模板读取并更新参数
我正在尝试使用电子邮件模板,并在程序中使用它。我想用程序变量和替换模板中的值 如果我在heredoc的程序中包含这个,我就有了它的工作示例。但是我想把它作为程序外的模板,这样我就可以把它分发给其他人,这样他们就可以定制它了。这意味着我试图使用程序中的变量替换模板中的#{tabcmd}和#{img} 但我得到了这个错误。我说用\来逃避,但没用。 下午1:19:35:捕获异常:正在退出 错误消息:(eval):5:语法错误,意外的tIDENTIFIER,应为$end 我甚至试过body1=Html 从模板读取并更新参数,html,ruby,templates,Html,Ruby,Templates,我正在尝试使用电子邮件模板,并在程序中使用它。我想用程序变量和替换模板中的值 如果我在heredoc的程序中包含这个,我就有了它的工作示例。但是我想把它作为程序外的模板,这样我就可以把它分发给其他人,这样他们就可以定制它了。这意味着我试图使用程序中的变量替换模板中的#{tabcmd}和#{img} 但我得到了这个错误。我说用\来逃避,但没用。 下午1:19:35:捕获异常:正在退出 错误消息:(eval):5:语法错误,意外的tIDENTIFIER,应为$end 我甚至试过body1=eval(
eval(“\”{body}\”)
。但它没有起作用。我需要使用yaml或其他东西吗
工作
def sendmessage(report_name, rep_desc, to_list, rcptList, subject_line, body, error_file_name, table_name_n, file_name_n , log_file)
##########################
# loop arround each image file and add it to the email.
##########################
imtg=""
#define global variable to loop arround each image file.
$party=""
file_name_n.each { |a| puts a
fname1 = "#{Base64.encode64(File.open(a, 'rb').read)}"
part4=<<EOF
Content-Type: image/png; name="#{a}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{a}"
#{fname1}
--#{marker}
EOF
$party << part4
imtg <<"<IMG SRC=\"#{a}\" alt=\"#{a}\">"
puts imtg
}
tabnam = File.open(table_name_n, 'rb').read
puts tabnam
##########################
# Build email body
##########################
body = <<EOF
<html>
<head>
<title>Dashboards</title>
</head>
<body style="margin: 0px; padding: 0px; font-family: 'Trebuchet MS',verdana;">
<table width="100%" style="height: 100%;" cellpadding="4" cellspacing="0" border="0">
<tr>
<!-- ============ HEADER SECTION ============== -->
<td colspan="3" style="height: 100px;" bgcolor="#38424F" align="center"><h1><font size="3" color="white">#{report_name}</font></h1></td>
</tr>
#{tabcmd}
..
#{img}
...
EOF
##########################
# loop arround each image file and add it to the email.
##########################
imtg=""
#define global variable to loop arround each image file.
$party=""
file_name_n.each { |a| puts a
fname1 = "#{Base64.encode64(File.open(a, 'rb').read)}"
part4=<<EOF
Content-Type: image/png; name="#{a}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{a}"
#{fname1}
--#{marker}
EOF
$party << part4
imtg <<"<IMG SRC=\"#{a}\" alt=\"#{a}\">"
puts imtg
}
tabnam = File.open(table_name_n, 'rb').read
puts tabnam
##########################
# Build email body
##########################
body = File.open(body, 'rb').read
puts body
更新:解决方案: 我在玩的时候发现了它。它是简单的gsub
body=body.gsub(/\#{tabnam}/, "#{tabnam}").gsub(/\#{imtg}/, "#{imtg}").gsub(/\#{report_name}/, "#{report_name}").gsub(/\#{rep_desc}/, "#{rep_desc}")
body=body.gsub(/\#{tabnam}/, "#{tabnam}").gsub(/\#{imtg}/, "#{imtg}").gsub(/\#{report_name}/, "#{report_name}").gsub(/\#{rep_desc}/, "#{rep_desc}")
body=body.gsub(/\#{tabnam}/, "#{tabnam}").gsub(/\#{imtg}/, "#{imtg}").gsub(/\#{report_name}/, "#{report_name}").gsub(/\#{rep_desc}/, "#{rep_desc}")