Valter Silva
Valter Silva

Reputation: 16656

How to use YAML files with Vagrant?

I'm trying to improve my YAML file for my Vagrant project. According to this post, if I have something like this:

en:
  site_name: "Site Name"
  static_pages:
    company:
      description: ! '%{site_name} is an online system'

I should be able to print "Site Name is an online system", but I don't know how to use it inside my Vagrantfile. I tried so far but I couldn't print it out properly, just this:

%{site_name} is an online system

This is how I'm using it:

require 'yaml'
set = YAML.load_file(ENV['DEVOPS_HOME'] + '/vagrant/server/settings.yml')
puts set['en']['static_pages']['company']['description']

Upvotes: 2

Views: 2274

Answers (2)

Frederic Henri
Frederic Henri

Reputation: 53713

as they say in the answer of the post

and then call in the appropriate view with the site name as a parameter

so you don't get directly after you load the yaml file the expected string, you need to match the parameter. one way you could work this in your Vagrantfile is

require 'yaml'
set = YAML.load_file(ENV['DEVOPS_HOME'] + '/vagrant/server/settings.yml')
str = set['en']['static_pages']['company']['description']
arg = {:site_name=> set['en']['site_name']}
p str % arg

will print out "Site Name is an online system"

The other way would be to use the answer from mudasobwa which is also detailed in the original post you reference

Upvotes: 3

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

You might want to use YAML aliases to achieve this functionality:

en:
  site_name: &site_name "Site Name" # declaring alias
  static_pages:
    company:
      description: 
        - *site_name                # reference
        - "is an online system"

And later on:

puts set['en']['static_pages']['company']['description'].join(' ')

Upvotes: 2

Related Questions