If I have an .html.erb file that looks like this:
<html>
<head>
<title>Test</title>
</head>
<body>
<%= #name %>
</body>
</html>
How can I generate an HTML file that looks like this?
<html>
<head>
<title>Test</title>
</head>
<body>
John
</body>
</html>
How can I execute the template (passing the name parameter) given that the format is .html.erb and be able to get just an .html file?
#page.html.erb
<html>
<head>
<title>Test</title>
</head>
<body>
<%= #name %>
</body>
</html>
...
require 'erb'
erb_file = 'page.html.erb'
html_file = File.basename(erb_file, '.erb') #=>"page.html"
erb_str = File.read(erb_file)
#name = "John"
renderer = ERB.new(erb_str)
result = renderer.result()
File.open(html_file, 'w') do |f|
f.write(result)
end
...
$ ruby erb_prog.rb
$ cat page.html
<html>
<head>
<title>Test</title>
</head>
<body>
John
</body>
</html>
Of course, to make things more interesting, you could always change the line #name = "John" to:
print "Enter a name: "
#name = gets.chomp
The ruby ERB library operates on strings, so you would have to read the .erb file into a string, create an ERB object, and then output the result into an html file, passing the current binding to the ERB.result method.
It would look something like
my_html_str = ERB.new(#my_erb_string).result(binding)
where #my_erb_string. You could then write the html string into an html file.
You can read more about binding here and about the ERB library here.
Here is a sample code:
require 'erb'
template = File.read("template.html.erb")
renderer = ERB.new(template)
class Message
attr_accessor :name
def initialize(name)
#name = name
end
def get_binding
binding()
end
end
message = Message.new 'John'
File.open('result.html', 'w') do |f|
f.write renderer.result message.get_binding
end
The template.html.erb is your .html.erb template file and result.html is the rendered html file.
Some nice articles you can take a look, link1, link2.
Related
I want to attach logo at the end of the email while sending mail . Below is my mailer code
employee_mailer.erb -
def user_confirmation(emp,pwd)
#password = pwd
#employee = emp
#emp = Employee.find_by(id: #employee.id)
mail(to: #emp.email, subject: 'HRMS Password Detail')
user_confirmation.html.erb -
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h4>Confirmation Email</h4>
<p>
<td> Welcome in HRMS of <%= #emp.company.name %> !!
This is to inform you that your default password is <b><%= #password %></b>
You can change your password.</td>
</p>
</body>
</html>
How I can send image as well with the above code .
You just need to add img tag as we do in HTML:
<img src="/location/image" alt="logo">
OR Rails way using Rails helper:
<%= image_tag("logo.png") %>
as you are sending a HTML email.
You can also add it as an inline attachment if required:
<%= image_tag attachments['logo.png'].url -%>
And in your mailer action:
attachments.inline['logo.png'] = File.read('path/to/logo.png')
On a Side Note:
I am not sure why you are doing this:
#employee = emp
#emp = Employee.find_by(id: #employee.id)
When you already have the employee in emp variable directly assign it to #emp or directly use #employee in which you have assigned it.
Hope this helps.
I'm creating an ASP.net website and I need to use a VBScript to handle my code using a function so that every time i call that function it writes that code in html.
Something like this:
function heading(tite){
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1">
<title><%= title %></title>
</head>
}
How can i do this?
Thanks
<% sub heading(titl) %>
<html><head>your html here<% =titl %>
</head>
<% end sub %>
</html>
I'm trying to use Nokogiri to convert some template files from one format to another. But it keeps adding tags. I'm trying to prevent it from adding Doctype and meta tags, but can't figure it out. I've tried
#doc = Nokogiri::HTML.parse(r)
but that adds the tags. I've also tried
#doc = Nokogiri::HTML.fragment(r)
as suggested in "How to prevent Nokogiri from adding <DOCTYPE> tags?", but that removes any <html>, <head>, or <body> tags that are in the document.
If it matters, my code for reading the file is:
f = File.read(infile)
r = f.gsub(/<tmpl_var ([^>]*)>/, '{{{\1}}}')
#doc = Nokogiri::HTML.fragment(r)
I need to do a gsub beforehand because I need to replace <tmpl_var> tags which aren't proper HTML and cause more problems.
When using HTML.fragment(r), I do get an htmlParseStartTag: misplaced <html> tag error (as well as similar errors for <body> and <head>).
Is there a way to prevent it from making these additions?
An example conversion:
Before:
<html>
<head>
<script>
var x = "y";
</script>
</head>
<body>
<div>
Stuff
</div>
</body>
</html>
After using Parse:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
var x = "y";
</script>
</head>
<body>
<div>
Stuff
</div>
</body>
</html>
After using HTML.fragment or HTML::DocumentFragment.parse:
<script>
var x = "y";
</script>
<div>
Stuff
</div>
In this case, I want it to just output the before section. (In the real script I make a bunch of changes though).
Nokogiri can be told to not add the standard HTML headers. Consider these:
require 'nokogiri'
doc = Nokogiri::HTML('<p>foo</p>')
doc.to_html # => "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><p>foo</p></body></html>\n"
doc = Nokogiri::HTML.fragment('<p>foo</p>')
doc.to_html # => "<p>foo</p>"
tmpl_var is a bad tag name in HTML, as is {{{\1}}}, so asking Nokogiri to try to parse either will result in problems:
doc = Nokogiri::HTML.fragment('<templ_var p1="baz">foo</templ_var>')
doc.errors # => [#<Nokogiri::XML::SyntaxError: Tag templ_var invalid>]
But you can still munge the DOM:
doc.to_html # => "<templ_var p1=\"baz\">foo</templ_var>"
doc.search('templ_var').each { |t| t.name = 'bar'}
doc.to_html # => "<bar p1=\"baz\">foo</bar>"
Or:
doc.to_html # => "<div><templ_var p1=\"baz\">foo</templ_var></div>"
doc.search('templ_var').each { |t| t.replace('{{{\1}}}') }
doc.to_html # => "<div>{{{\\1}}}</div>"
Putting that stuff together, plus a bit of chicanery:
doc = Nokogiri::HTML.fragment('<div><templ_var p1="baz">foo</templ_var></div>')
doc.to_html # => "<div><templ_var p1=\"baz\">foo</templ_var></div>"
doc.search('templ_var').each { |t| t.replace('{{{\1}}}') }
doc.to_html # => "<div>{{{\\1}}}</div>"
header = Nokogiri::XML.fragment('<html><body>')
header.at('body').children = doc
header.to_html # => "<html><body><div>{{{\\1}}}</div></body></html>"
So, I'd go after it something like that.
Now, why is Nokogiri stripping the <html> tag when parsing a fragment? I don't know. It leaves <body> alone if <head> or <html> is missing:
Nokogiri::HTML.fragment('<p>foo<p>').to_html
# => "<p>foo</p><p></p>"
Nokogiri::HTML.fragment('<body><p>foo<p></body>').to_html
# => "<body>\n<p>foo</p>\n<p></p>\n</body>"
But it gets funky if <head> or <html> exists:
Nokogiri::HTML.fragment('<head><style></style></head><body><p>foo<p></body>').to_html
# => "<style></style><p>foo</p><p></p>"
Nokogiri::HTML.fragment('<html><head><style></style></head><body><p>foo<p></body></html>').to_html
# => "<style></style><p>foo</p><p></p>"
That smells like a bug in Nokogiri to me as I haven't seen anything to document that behavior.
You can get around this by using Nokogiri::XML::DocumentFragment instead of Nokogiri::HTML::DocumentFragment. The XML version won't remove the html, head, or body tags.
In custom_colors.css.erb file is:
<%
WebsiteSetting.last.link_color.present? ? (link_color = WebsiteSetting.last.link_color) : (link_color = '#0088cc')
%>
$custom_link: <%= link_color %>;
and in many css files i'm using $custom_link variable, but when i update WebsiteSetting.last by setting link_color field by colorpicker (hex), nothing changes in views. I suppose the problem is with precompiling custom_colors.css.erb file after request.
Dynamic assets precompilation (on each request) is normally done in development environment only for debugging reasons.
If you want to be able to use some request-dependent styles consider moving that code to the application layout:
application.html.erb:
<!DOCTYPE html>
<html lang="en">
<head>
...
<style type="text/css">
#my_div {
color: <% WebsiteSetting.last.link_color.present? ? WebsiteSetting.last.link_color : '#0088cc' %>;
}
</style>
...
</head>
<body>
...
</body>
</html>
I'm currently working on FusionChartsFree on a small internal application and I have a small html code like this.
<html>
<head>
<title>My First chart using FusionCharts XT</title>
<script type="text/javascript" src="FusionCharts.js"></script>
</head>
<body>
<div id="chartContainer">FusionCharts XT will load here!</div>
<script type="text/javascript">
var myChart = new FusionCharts( "Line.swf", "myChartId", "400", "300");
var strXML = "<chart caption='Critical' xAxisName='month' yAxisName='Count' yAxisMinValue ='40' showValues= '0'><set label = 'month1' value='55'/><set label = 'month2' value='55'/><set label = 'month3' value='55'/><set label = 'month4' value='55'/></chart>" ;
myChart.setXMLData(strXML);
myChart.render("chartContainer");
</script>
</body>
</html>
The above code works perfectly and renders a graph. Now, I'm trying to do the same thing using JSP as below :
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Graphs</title>
<script type="text/javascript" src="FusionCharts.js"></script>
</head>
<body>
<%
String data="<chart caption='Minor' xAxisName='month' yAxisName='Count' yAxisMinValue ='66500' showValues= '0'>"+"\n"+"<set label = 'month1' value='66560'/>"+"\n"+"<set label = 'month2' value='66560'/>"+"\n"+"<set label = 'month3' value='66647'/>"+"\n"+"<set label = 'month4' value='66631'/>"+"\n"+"</chart>";
System.out.println(data);
%>
<div id="chartContainer1" align="left" style="margin-top: 22px; padding-top: 310px;">blocker data</div>
<script>
var blocker = new FusionCharts("Line.swf", "myChartId1", "400", "300");
var strXML1="<%=data%>";
blocker.setXMLData(strXML1);
blocker.render("chartContainer1");
</script>
</body>
</html>
The problem comes when I'm generating the "data" String dynamically, I do not get any output. Please help
The problem is the extra "\n" in your XML of JSP page. Remove "\n" and check again, it will work fine.
When you are passing data using XMLData() function, FusionCharts expects a String of XML data without any line breaks(that are explicitly included in the XML).