Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #1398 (closed enhancement: worksforme)

Opened 3 years ago

Last modified 3 years ago

[PATCH] Render a string and return a string

Reported by: robbyrussell Assigned to: David
Priority: normal Milestone:
Component: ActionPack Version: 0.12.1
Severity: normal Keywords: render string to string
Cc:

Description

I am storing ERb templates in the database and would like to use them for things such as emails, pdf generation, etc.

I was able to create this functionality with this method (see attached).

       def render_string_to_string(text = nil, type = "rhtml") #:doc:
         add_variables_to_assigns
         @template.render_template(type, text)
       end

Which I could call like so:

foo = render_string_to_string(letter_template.body)

With render_template(), it would output to the browser. render_string() required a physical file on the system as a template.

Is there a better way to do this? I noticed that in the trunk, render* is completely refactored, so maybe this isn't the case anymore?

Attachments

render_string_to_string.diff (382 bytes) - added by robbyrussell on 06/04/05 19:10:21.
render_string_to_string

Change History

06/04/05 19:10:21 changed by robbyrussell

  • attachment render_string_to_string.diff added.

render_string_to_string

06/04/05 19:37:00 changed by robbyrussell

I saw this ticket (541). It was marked as a duplicate. I tried render_to_string, but this expects a physical file template, it doesn't take a string... say from the database.

In my scenario, I don't want the content to be sent to the browser in html, I need to use it for different things, such as PDFs, Emails, etc. Is using ActionController for rendering this, the wrong place to do it?

06/04/05 21:44:16 changed by bitsweat

  • status changed from new to closed.
  • resolution set to worksforme.
  • summary changed from Render a string and return a string to [PATCH] Render a string and return a string.

render_as_string wraps render but returns the result to you rather than the browser:

erb_template = grab_erb_from_database
content = render_to_string(:inline => erb_template)