The Hitchhiker’s Guide to Ruby On Rails Galaxy

Records of my voyage through RoR Galaxy

Using ERB as a dynamic template to create a file whose contents are dynamic

Posted by arjunghosh on February 17, 2009

The following code will create a temporary file for say attaching a vcard file to email.The following code is dynamic to use ERB as a template to create the file dynamically every time with the variable “telephone”,”location”,”email” which gets binded to the ERB file and is under scope.
The code is:-

{
telephone = “347-27456″
location = “New Delhi”
email = “xyz@gmail.com”
vcard = ERB.new( File.open(‘address.vcard.erb’ ){ |f| f.read } ).result( binding )
f = File.new(“temp_file.vcf”,”wb”)
f.puts vcard
f.close
}

and in pastie is here

About these ads

One Response to “Using ERB as a dynamic template to create a file whose contents are dynamic”

  1. Michael Tim said

    I love your site!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: