yaml -> template file

48 views
Skip to first unread message

Brian Donahue

unread,
Oct 19, 2009, 1:52:03 PM10/19/09
to phil...@googlegroups.com
Folks,

I am trying to do something that's probably mind-numbingly easy, but I'm far too obtuse to figure it out.  I'm using Rake to build a .NET project (ACK!) and I want to set some local settings in a yaml file then read it and do search and replaces of tokens in a template.  Such as:

yaml:

connection_string: blah

Template:
<ConnectionString>%connection_string%</ConnectionString>

Where at the end of the day, I'd create a new file from my template with the %connection_string% token replaced with the yaml value.

Is there an easy way to do this, so that you end up with symbols or a class that I can use in a replace function?  (i.e. end up with :connection_string or config.connection_string after loading the yaml)?

Thanks for any tips!

Brian


Randy Schmidt

unread,
Oct 19, 2009, 1:54:04 PM10/19/09
to phil...@googlegroups.com
You could pass it through erb or liquid.

Randy Schmidt

unread,
Oct 19, 2009, 1:54:44 PM10/19/09
to phil...@googlegroups.com

Brian Donahue

unread,
Oct 19, 2009, 1:59:24 PM10/19/09
to phil...@googlegroups.com

Those would work for the templating part, right?  I'm still trying to figure out how to load yaml and have it be accessible via an object or symbol.  I'm having trouble finding similar examples on google, and I know I'm missing something obvious.  I have:

config = YAML::load_file "local.properties.yaml"

"puts config" just spits the yaml back out.  My ruby is too weak to figure out what kind of object/structure is loaded into config... is it just a string, or is there a way to access each value in the yaml file by name?

Randy Schmidt

unread,
Oct 19, 2009, 2:05:13 PM10/19/09
to phil...@googlegroups.com
You could use something like openstruct to create an arbitrary object
with methods from the hash.

<creepy>Go ahead, ride the mustache, you know you want to. </creepy>

Steve Eichert

unread,
Oct 19, 2009, 2:09:23 PM10/19/09
to phil...@googlegroups.com
YAML::load_file with the sample you provided will be a hash, so you can access the value by passing in the key that you're interested in.  So config[:connection_string] should provide you back your connection string.

Angel Pizarro

unread,
Oct 19, 2009, 2:09:43 PM10/19/09
to phil...@googlegroups.com
Brian, 
config = YAML.load_file( '/path/to/file' ) 

will set config to be a Hash object. from your example yaml file, you can access the connection_string variable via:

config["connection_sting"] # => gives the string "blah" 

See the Erubis project documentation (specifically rendering templates with a context) for more examples. 
--
Angel

Colin A. Bartlett

unread,
Oct 19, 2009, 2:16:15 PM10/19/09
to phil...@googlegroups.com
You have mustache, Randy? Ooooh, I can't wait to ride it.

</creepier>

Brian Donahue

unread,
Oct 19, 2009, 2:20:01 PM10/19/09
to phil...@googlegroups.com
Thanks, Angel!  That is what was eluding me.

Looking forward to your CouchDB talk on Wednesday!

Jason Yates

unread,
Oct 20, 2009, 3:03:22 PM10/20/09
to phil...@googlegroups.com
ActiveSupport, technically a part of Rails, adds a to_xml method into
the Hash class, so you may not need templates as long as the XML and
YAML are structured the same.

require 'active_support'

config = YAML.load_file('/path/to/file');
# assert config.class == "Hash"
puts config.to_xml

The documentation is here(just run a find on to_xml, the to_xml in
ActiveSupport not ActiveRecord).

http://api.rubyonrails.org/

--
Jason Yates
jay...@gmail.com

Brian Donahue

unread,
Oct 20, 2009, 3:19:04 PM10/20/09
to phil...@googlegroups.com
Thanks for this!  That looks useful though in this case it's a complex XML file with a lot of other info in it I can't lose, so I am doing token replacement.  Got it working using the YAML hash Angel showed.

Thanks everyone,

Brian
Reply all
Reply to author
Forward
0 new messages