Skip to content

Commit 185f09b

Browse files
committedAug 19, 2016
Fix #1227
1 parent 322170c commit 185f09b

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed
 

Diff for: ‎CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ New in master
44
Features
55
--------
66

7+
* Shortcode to escape to the template language (Issue #1227)
78
* Added link to raw file in listings (Issue #1995)
89
* New NO_DOCUTILS_TITLE_TRANSFORM (Issue #2382)
910
* Update options of chart directive to Pygal 2.2.3

Diff for: ‎docs/manual.txt

+13
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,19 @@ Then the output file will contain::
928928

929929
This uses the bar variable: bla
930930

931+
Finally, you can use a template shortcode without a file, by inserting the template in the shortcode itself::
932+
933+
{{% raw %}}
934+
{{% template %}}
935+
% for foo in bar:
936+
<li> ${foo}
937+
% endfor
938+
{{% /template %}}
939+
{{% /raw %}}
940+
941+
In that case, the template engine used will be your theme's and the arguments you pass, as well as the global
942+
context from your ``conf.py``, are available to the template you are creating.
943+
931944
Redirections
932945
------------
933946

Diff for: ‎nikola/nikola.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,14 @@ def render_shortcode(*args, **kw):
14521452
return render_shortcode
14531453

14541454
def _register_templated_shortcodes(self):
1455-
"""Register shortcodes provided by templates in shortcodes/ folders."""
1455+
"""Register shortcodes based on templates.
1456+
1457+
This will register a shortcode for any template found in shortcode/
1458+
and a generic "template" shortcode which will consider the content
1459+
in the shortcode as a template in itself."""
1460+
1461+
self.register_shortcode('template', self._template_shortcode_handler)
1462+
14561463
builtin_sc_dir = resource_filename(
14571464
'nikola',
14581465
os.path.join('data', 'shortcodes', utils.get_template_engine(self.THEMES)))
@@ -1470,6 +1477,20 @@ def _register_templated_shortcodes(self):
14701477
self.register_shortcode(name, self._make_renderfunc(
14711478
fd.read(), os.path.join(sc_dir, fname)))
14721479

1480+
def _template_shortcode_handler(self, *args, **kw):
1481+
t_data = kw.pop('data', '')
1482+
context = self.GLOBAL_CONTEXT.copy()
1483+
context.update(kw)
1484+
context['_args'] = args
1485+
context['lang'] = utils.LocaleBorg().current_lang
1486+
for k in self._GLOBAL_CONTEXT_TRANSLATABLE:
1487+
context[k] = context[k](context['lang'])
1488+
output = self.template_system.render_template_to_string(t_data, context)
1489+
# XXX FIXME: we have no standard way to get dependency information from
1490+
# a template that's not a file
1491+
dependencies = []
1492+
return output, dependencies
1493+
14731494
def register_shortcode(self, name, f):
14741495
"""Register function f to handle shortcode "name"."""
14751496
if name in self.shortcode_registry:

0 commit comments

Comments
 (0)
Please sign in to comment.