Navigation Menu

Skip to content

Commit

Permalink
global data (Issue #2477)
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed Aug 27, 2016
1 parent ac49ce3 commit ee6a182
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -14,6 +14,7 @@ Features
* Added new options --html2text and --transform-to-markdown
to WordPress importer (Issue #2261)
* Listing: guess the lexer if cannot be determined from the file name.
* Read files from data/ and insert data in global context (Issue #2477)

Bugfixes
--------
Expand Down
14 changes: 14 additions & 0 deletions docs/manual.txt
Expand Up @@ -956,6 +956,20 @@ In that case, the template engine used will be your theme's and the arguments yo
as well as the global context from your ``conf.py``, are available to the template you
are creating.

.. sidebar:: The Global Context

There is a ``GLOBAL_CONTEXT`` field in your ``conf.py`` where you can
put things you want to make available to your templates.

It will also contain things you put in a ``data/`` directory within your
site. For example, if you create ``data/foo.json`` containing this::

{"bar": "bat"}

Then your templates can use things like ``${data['foo']['bar']}`` and
it will be replaced by "bat".


Redirections
------------

Expand Down
22 changes: 19 additions & 3 deletions nikola/nikola.py
Expand Up @@ -32,6 +32,7 @@
from copy import copy
from pkg_resources import resource_filename
import datetime
import glob
import locale
import os
import json
Expand Down Expand Up @@ -896,7 +897,8 @@ def __init__(self, **config):
else:
self.bad_compilers.add(k)

self._set_global_context()
self._set_global_context_from_config()
self._set_global_context_from_data()

# Set persistent state facility
self.state = Persistor('state_data.json')
Expand Down Expand Up @@ -1036,8 +1038,12 @@ def plugin_position_in_places(plugin):
self._register_templated_shortcodes()
signal('configured').send(self)

def _set_global_context(self):
"""Create global context from configuration."""
def _set_global_context_from_config(self):
"""Create global context from configuration.
These are options that are used by templates, so they always need to be
available.
"""
self._GLOBAL_CONTEXT['url_type'] = self.config['URL_TYPE']
self._GLOBAL_CONTEXT['timezone'] = self.tzinfo
self._GLOBAL_CONTEXT['_link'] = self.link
Expand Down Expand Up @@ -1128,6 +1134,16 @@ def _set_global_context(self):

self._GLOBAL_CONTEXT.update(self.config.get('GLOBAL_CONTEXT', {}))

def _set_global_context_from_data(self):
"""Load files from data/ and put them in the global context."""
self._GLOBAL_CONTEXT['data'] = {}
for fname in glob.glob('data/*'):
data = utils.load_data(fname)
key = os.path.basename(fname)
key = os.path.splitext(key)[0]
self._GLOBAL_CONTEXT['data'][key] = data


def _activate_plugins_of_category(self, category):
"""Activate all the plugins of a given category and return them."""
# this code duplicated in tests/base.py
Expand Down

0 comments on commit ee6a182

Please sign in to comment.