Ethan Ethan - 2 months ago 13
YAML Question

Is it possible to make a configuration file holding element inputs?

Can I take a configuration file, like YAML, and use it to fill in element text?

Example YAML:

YOUR_NAME: "your_name"


Output HTML:

<h1>{YOUR_NAME}</h1>

Answer

Yes that is certainly possible and easily doable. I use that within the pyramid framework, by parsing the YAML file and use the resulting mapping/dict to update the dictionary handed to the template engine.

@view_config(route_name='search', renderer='templates/search.pt')
def my_search(self):
    res = dict(YOUR_NAME="----- not set -----")
    with open('your_file.yaml') as fp:
       res.update(ruamel.yaml.safe_load(fp))
    return res

In the template use <h1>${YOUR_NAME}</h1> (the dollar is needed for the chameleon template engine) or <h1>${structure:YOUR_NAME}</h1> in case the values should be unescaped.

Note that it is not necessary to quote "your_name" in YAML, not even if it had spaces:

YOUR_NAME: first_name last_name

(names normally would not contain characters or character sequences for which you would need quoting of the scalar value)