Skip to content
This repository has been archived by the owner on Jan 25, 2019. It is now read-only.

Arbitrary, site-wide template variables in config file #94

Open
robatron opened this issue Nov 29, 2012 · 5 comments
Open

Arbitrary, site-wide template variables in config file #94

robatron opened this issue Nov 29, 2012 · 5 comments
Labels

Comments

@robatron
Copy link
Contributor

It would be great to have the ability to add arbitrary, site-wide template variables in the config file.

For example, in config, you might have a map called template_globals:

site_title: My Site
url_pattern: /{category}/{slug}.html
template_globals: {
    DEBUG: True,
}

And anything in template_globals would be available to every template, e.g.,

{% if DEBUG %}
    <div id='debug'>Debug mode</div>
    <script text='text/javascript'>
        console.log('debug mode')
    </script>
@oampo
Copy link
Contributor

oampo commented Jan 16, 2013

Just a quick note - I'm doing this using a yaml file and a hook. The code is at https://gist.github.com/4546750. You just need to add a file called globals.yaml to your root directory, and any variables are merged into the template variables dictionary.

@robatron
Copy link
Contributor Author

Thanks, @oampo, that's a great idea!

@mythmon, why a YAML config file, and not a Python file like Django? For security? Just curious.

@mythmon
Copy link
Owner

mythmon commented Jan 16, 2013

Huh. I don't remember seeing this issue... anyways, good news! You can already do this. This is probably one of the holes in the documentation, but you can add anything you like to the config file, and it will be available under the site template variable. So to modify @robatron's example:

config:

site_title: My Site
url_pattern: /{category}/{slug}.html

DEBUG: True

A template:

{% if site.DEBUG %}
    <div id="debug">Debug mode</div>
    <script text="text/javascript">
        console.log('debug mode')
    </script>
{% endif %}

and this works. The same is true of individual pages. Any thing that the parser finds in the YAML header that doesn't make sense to it, it just passes along under the page template variable. Sites like OSULUG (source) use this to great advantage to automate formatting images onto a page.

The decision to use YAML instead of a Python file was largely because at the time, the the Python import path was a dark and scary place for me: YAML did everything I wanted, and was easier to load. Although having settings in a turing complete format has it's advantages, I don't think it is really something that is needed here.

@robatron
Copy link
Contributor Author

Awesome, that's good to know. Thanks, Mike!

@oampo
Copy link
Contributor

oampo commented Jan 17, 2013

That is handy - yeah, a note in the documentation would be helpful.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants