Skip to content

Commit

Permalink
make the post object available to templated shortcodes used in posts
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed Aug 20, 2016
1 parent d638e7b commit 1b49e17
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
9 changes: 7 additions & 2 deletions docs/manual.txt
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,10 @@ will create a shortcode called ``mycode`` you can use. Any options you pass to
the shortcode will be available as variables for that template. Non-keyword
options will be passed in a tuple varaible named ``_args``.

The post in which the shortcode is being used is available as the ``post``
variable, so you can access the title as ``post.title``, and data loaded
via the ``data`` field in the metadata using ``post.data(key)``.

If you use the shortcode as paired, then the contents between the paired tags
will be available in the ``data`` variable. If you want to access the Nikola
object, it will be available as ``site``. Use with care :-)
Expand Down Expand Up @@ -946,8 +950,9 @@ Finally, you can use a template shortcode without a file, by inserting the templ
{{% /template %}}
{{% /raw %}}

In that case, the template engine used will be your theme's and the arguments you pass, as well as the global
context from your ``conf.py``, are available to the template you are creating.
In that case, the template engine used will be your theme's and the arguments you pass,
as well as the global context from your ``conf.py``, are available to the template you
are creating.

Redirections
------------
Expand Down
4 changes: 2 additions & 2 deletions nikola/nikola.py
Original file line number Diff line number Diff line change
Expand Up @@ -1499,11 +1499,11 @@ def register_shortcode(self, name, f):
self.shortcode_registry[name] = f

# XXX in v8, get rid of with_dependencies
def apply_shortcodes(self, data, filename=None, lang=None, with_dependencies=False):
def apply_shortcodes(self, data, filename=None, lang=None, with_dependencies=False, extra_context={}):
"""Apply shortcodes from the registry on data."""
if lang is None:
lang = utils.LocaleBorg().current_lang
return shortcodes.apply_shortcodes(data, self.shortcode_registry, self, filename, lang=lang, with_dependencies=with_dependencies)
return shortcodes.apply_shortcodes(data, self.shortcode_registry, self, filename, lang=lang, with_dependencies=with_dependencies, extra_context=extra_context)

def generic_rss_renderer(self, lang, title, link, description, timeline, output_path,
rss_teasers, rss_plain, feed_length=10, feed_url=None,
Expand Down
10 changes: 6 additions & 4 deletions nikola/plugins/compile/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,16 @@ def compile_html(self, source, dest, is_two_file=True):
makedirs(os.path.dirname(dest))
error_level = 100
with io.open(dest, "w+", encoding="utf8") as out_file:
try:
post = self.site.post_per_input_file[source]
except KeyError:
post = None
with io.open(source, "r", encoding="utf8") as in_file:
data = in_file.read()
output, error_level, deps = self.compile_html_string(data, source, is_two_file)
output, shortcode_deps = self.site.apply_shortcodes(output, filename=source, with_dependencies=True)
output, shortcode_deps = self.site.apply_shortcodes(output, filename=source, with_dependencies=True, extra_context=dict(post=post))
out_file.write(output)
try:
post = self.site.post_per_input_file[source]
except KeyError:
if post is None:
if deps.list:
self.logger.error(
"Cannot save dependencies for post {0} due to unregistered source file name",
Expand Down
3 changes: 2 additions & 1 deletion nikola/shortcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def _split_shortcodes(data):


# FIXME: in v8, get rid of with_dependencies
def apply_shortcodes(data, registry, site=None, filename=None, raise_exceptions=False, lang=None, with_dependencies=False):
def apply_shortcodes(data, registry, site=None, filename=None, raise_exceptions=False, lang=None, with_dependencies=False, extra_context={}):
"""Apply Hugo-style shortcodes on data.
{{% name parameters %}} will end up calling the registered "name" function with the given parameters.
Expand Down Expand Up @@ -312,6 +312,7 @@ def apply_shortcodes(data, registry, site=None, filename=None, raise_exceptions=
kw['site'] = site
kw['data'] = data_arg
kw['lang'] = lang
kw.update(extra_context)
if name in registry:
f = registry[name]
if getattr(f, 'nikola_shortcode_pass_filename', None):
Expand Down

0 comments on commit 1b49e17

Please sign in to comment.