Repository: getnikola/nikola

Issue:
Find a way to access templates from posts
This is a similar idea to pelican's [liquid tags plugin](https://github.com/getpelican/pelican-plugins/tree/master/liquid_tags) but let's do it better.

The idea would be a block in the post content where you can use template markup. In reSt this could be done using a directive (ignore the name):

```
.. templated::

   This post is tagged 
   %for tag in post.tags():
       ${tag} 

```

While not terribly useful by itself, it would allow things like the series plugin to work in a very seamless way that integrates nicely with the post's text flow.

Also, it doesn't look horribly hard to do in reSt and in markdown it could be as simple as a fenced section.



Discussion:
What would be the context for those templates?

At least post, language and global context.

Would this be locked to a particular template language?  What happens with this if I change the template engine for my site - would I have to edit every post with this directive?  Would this be better as a plugin that provides a directive or shortcode?

(1) yes (2) have fun rewriting all posts (3) whatever is required to be compatible with everyone — probably a rest directive also available as a shortcode

Indeed a shortcode/directive for this is stupid simple to write!

After @kwpolska added the global context to shortcodes, and since shortcode arguments are passed to templates ... this is really really easy.


Resolving patch:
diff --git a/nikola/nikola.py b/nikola/nikola.py
--- a/nikola/nikola.py
+++ b/nikola/nikola.py
@@ -1452,7 +1452,14 @@ def render_shortcode(*args, **kw):
         return render_shortcode
 
     def _register_templated_shortcodes(self):
-        """Register shortcodes provided by templates in shortcodes/ folders."""
+        """Register shortcodes based on templates.
+
+        This will register a shortcode for any template found in shortcode/
+        and a generic "template" shortcode which will consider the content
+        in the shortcode as a template in itself.
+        """
+        self.register_shortcode('template', self._template_shortcode_handler)
+
         builtin_sc_dir = resource_filename(
             'nikola',
             os.path.join('data', 'shortcodes', utils.get_template_engine(self.THEMES)))
@@ -1470,6 +1477,20 @@ def _register_templated_shortcodes(self):
                     self.register_shortcode(name, self._make_renderfunc(
                         fd.read(), os.path.join(sc_dir, fname)))
 
+    def _template_shortcode_handler(self, *args, **kw):
+        t_data = kw.pop('data', '')
+        context = self.GLOBAL_CONTEXT.copy()
+        context.update(kw)
+        context['_args'] = args
+        context['lang'] = utils.LocaleBorg().current_lang
+        for k in self._GLOBAL_CONTEXT_TRANSLATABLE:
+            context[k] = context[k](context['lang'])
+        output = self.template_system.render_template_to_string(t_data, context)
+        # XXX FIXME: we have no standard way to get dependency information from
+        # a template that's not a file
+        dependencies = []
+        return output, dependencies
+
     def register_shortcode(self, name, f):
         """Register function f to handle shortcode "name"."""
         if name in self.shortcode_registry:


Test patch: