Hi all, Has anyone ever thought about or implemented a way to use Rails to pull content from a database and actually *generate* static html pages from view templates? I want to throw a bunch of content into a db using rails and then put all my website templates into the views dir and write the files out with the content inside. The funny thing is that the html pages Rails normally outputs to the browser are html with some funny memory/db address locations in there where you think text is........ go ahead, look at some of the pages your apps spit out -- stuff like <%Controller:12357643> etc.... is in there Any ideas about how I could output static pages please post! Thanks -- Chris Earle -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
Isn''t rails a framework for SOME web apps .... maybe this does fit inside the rails framework... I think this belong only to ERb ... and I have to do this once but I implement it with java servlets. On 6/26/06, Chris Earle (CBL) < cearle@cbltech.ca> wrote:> > Hi all, > > Has anyone ever thought about or implemented a way to use Rails to pull > content from a database and actually *generate* static html pages from > view > templates? > > I want to throw a bunch of content into a db using rails and then put all > my website templates into the views dir and write the files out with the > content inside. > > The funny thing is that the html pages Rails normally outputs to the > browser > are html with some funny memory/db address locations in there where you > think text is........ go ahead, look at some of the pages your apps spit > out -- stuff like <%Controller:12357643> etc.... is in there > > Any ideas about how I could output static pages please post! > > Thanks > -- > Chris Earle > > > -- > This message has been scanned for viruses and dangerous content by > MailScanner, and is believed to be clean. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- David Casta?eda R. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060626/d0221601/attachment.html
When you turn on Caching, Rails writes static pages to a subdirectory of /public/ so it''s already baked in there... ------------- Timothy Johnson www.foundinteractive.com On Jun 26, 2006, at 1:39 PM, David Casta?eda wrote:> Isn''t rails a framework for SOME web apps .... maybe this does fit > inside > the rails framework... I think this belong only to ERb ... and I have > to do > this once but I implement it with java servlets. > > On 6/26/06, Chris Earle (CBL) < cearle@cbltech.ca> wrote: >> >> Hi all, >> >> Has anyone ever thought about or implemented a way to use Rails to >> pull >> content from a database and actually *generate* static html pages from >> view >> templates? >> >> I want to throw a bunch of content into a db using rails and then >> put all >> my website templates into the views dir and write the files out with >> the >> content inside. >> >> The funny thing is that the html pages Rails normally outputs to the >> browser >> are html with some funny memory/db address locations in there where >> you >> think text is........ go ahead, look at some of the pages your apps >> spit >> out -- stuff like <%Controller:12357643> etc.... is in there >> >> Any ideas about how I could output static pages please post! >> >> Thanks >> -- >> Chris Earle >> >> >> -- >> This message has been scanned for viruses and dangerous content by >> MailScanner, and is believed to be clean. >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > > -- > David Casta?eda R. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Chris Earle (CBL) skrev:> Hi all, > > Has anyone ever thought about or implemented a way to use Rails to pull > content from a database and actually *generate* static html pages from view > templates? > > I want to throw a bunch of content into a db using rails and then put all > my website templates into the views dir and write the files out with the > content inside. >Yes, I have. It requires a bit hacking though. A site is built by a number of nodes, a node can be a page, an image, a file etc. Each node type inherit from Node and Node has an empty publish(). Every node is then responsible for writing itself to file while publishing. For example PageNode: def publish(options = {}) context_node = get_context_node(options) path = publish_path(options) result = yield(page.page_template.content, :view_mode => "publish", :page_node => context_node, :page => page, :view_node => context_node) File.open(path, "wb") do |file| file.write(result) end my_path = publish_path(:website_node => (options[:website_node] || find_website_node)) if(path != my_path) File.open(my_path, "wb") do |file| file.write(result) end end end The content of the PageNode is held (and accessed) through the page attribute. The node is only for site structure. This in turn is called from a publish job (a remote Drb service) or from a controller. publish() is called the follwoing way: ... node.publish(options) { |template, vars| vars.each_key { |key| self.instance_eval("@#{key} = vars[key]") } begin render_template(template) rescue => e msg = "Error while rendering node #{node_id} with ERb, #{e}\n#{e.backtrace.join("\n")}" err = true end } .... def render_template(template) erb_template = ERB.new(template) erb_template.result(binding) end By "passing in" the ERB template using a block I could call it directly from a controller as well altough that would violate DRY in this case. To get the variables "inside" the template I need to bind the variables put inside the vars hash (that happens when doing self.instance_eval(...)). This hash is created (or passed into) the publish() method in every Node instance. The result from the merging of the template (ie the rendered page) is passed back from the block to the calling node intstance (PageNode in this case) and is ready for file write (and then possibly a transfer with FTP (we do at least...)) I know this isn''t in the Rails sweetspot. But all administration of the sites is way inside the Rails sweetspot so that makes up (by far) for this not being inside it. /Marcus
Wow. you lost me a bit there with the ERB template concept. I wish it were simpler so I could just write the resulting html page to a file. My idea is that since I''m not going to do any logic in the ERb template, I can take an html template and replace content areas with instance variables and the controller will pull the content from the appropriate table row and put it into the output file. anyone think that might actually work? :-) -- Chris ----- Original Message ----- From: "Marcus Andersson" <m-lists@bristav.se> To: "Chris Earle (CBL)" <cearle@cbltech.ca>; <rails@lists.rubyonrails.org> Sent: Tuesday, June 27, 2006 2:15 AM Subject: Re: [Rails] Using Rails to Generate static pages> Chris Earle (CBL) skrev: > > Hi all, > > > > Has anyone ever thought about or implemented a way to use Rails to pull > > content from a database and actually *generate* static html pages fromview> > templates? > > > > I want to throw a bunch of content into a db using rails and then putall> > my website templates into the views dir and write the files out with the > > content inside. > > > Yes, I have. It requires a bit hacking though. A site is built by a > number of nodes, a node can be a page, an image, a file etc. Each node > type inherit from Node and Node has an empty publish(). Every node is > then responsible for writing itself to file while publishing. For > example PageNode: > > def publish(options = {}) > context_node = get_context_node(options) > path = publish_path(options) > result = yield(page.page_template.content, > :view_mode => "publish", > :page_node => context_node, > :page => page, > :view_node => context_node) > File.open(path, "wb") do |file| > file.write(result) > end > my_path = publish_path(:website_node => (options[:website_node] || > find_website_node)) > if(path != my_path) > File.open(my_path, "wb") do |file| > file.write(result) > end > end > end > > The content of the PageNode is held (and accessed) through the page > attribute. The node is only for site structure. This in turn is called > from a publish job (a remote Drb service) or from a controller. > publish() is called the follwoing way: > > ... > node.publish(options) { |template, vars| > vars.each_key { |key| > self.instance_eval("@#{key} = vars[key]") > } > begin > render_template(template) > rescue => e > msg = "Error while rendering node #{node_id} with ERb, > #{e}\n#{e.backtrace.join("\n")}" > err = true > end > } > .... > > def render_template(template) > erb_template = ERB.new(template) > erb_template.result(binding) > end > > By "passing in" the ERB template using a block I could call it directly > from a controller as well altough that would violate DRY in this case. > To get the variables "inside" the template I need to bind the variables > put inside the vars hash (that happens when doing > self.instance_eval(...)). This hash is created (or passed into) the > publish() method in every Node instance. The result from the merging of > the template (ie the rendered page) is passed back from the block to the > calling node intstance (PageNode in this case) and is ready for file > write (and then possibly a transfer with FTP (we do at least...)) > > I know this isn''t in the Rails sweetspot. But all administration of the > sites is way inside the Rails sweetspot so that makes up (by far) for > this not being inside it. > > /Marcus > > > -- > This message has been scanned for viruses and dangerous content by > MailScanner, and is believed to be clean.-- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
hello, so is there an ''easy'' way to generate static html pages from a rails site? where does one turn on the ''caching'' option? must i visit each link manually to create each static page? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Chris Earle (CBL) wrote:> The funny thing is that the html pages Rails normally outputs to the > browser > are html with some funny memory/db address locations in there where you > think text is........ go ahead, look at some of the pages your apps spit > out -- stuff like <%Controller:12357643> etc.... is in thereNo, it isn''t. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
bbqTree wrote:> hello, so is there an ''easy'' way to generate static html pages from a > rails site?Caching.> where does one turn on the ''caching'' option?See http://api.rubyonrails.com/classes/ActionController/Caching.html.> must i visit each link > manually to create each static page?Yes, but you could of course use wget -r or something like that. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---