Move template hook over to a template_hook tag. Seems to work! :)
authorChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 28 Jan 2013 20:08:16 +0000 (14:08 -0600)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Wed, 30 Jan 2013 19:22:19 +0000 (13:22 -0600)
mediagoblin/templates/mediagoblin/media_displays/image.html
mediagoblin/templates/mediagoblin/user_pages/media.html
mediagoblin/templates/mediagoblin/utils/templatehooks.html [deleted file]
mediagoblin/tools/template.py

index f27433bd428fea55b5140ad2e6bfdaf6c6b25de5..bc03c4566e16d9f8d698712f6bb6bccc18d3cf28 100644 (file)
 
 {% extends 'mediagoblin/user_pages/media.html' %}
 
-{% from "/mediagoblin/utils/templatehooks.html" import template_hook with context %}
-
 {% block mediagoblin_head %}
   {{ super() }}
-  {% for template in get_hook_templates("image_extrahead") %}
-    {% include template %}
-  {% endfor %}
-  {# {{ template_hook("image_extrahead") }} #}
+  {% template_hook("image_extrahead") %}
 {% endblock mediagoblin_head %}
 
 {% block mediagoblin_sidebar %}
-  {% for template in get_hook_templates("image_sideinfo") %}
-    {% include template %}
-  {% endfor %}
-  {# {{ template_hook("image_sideinfo") }} #}
+  {{ super() }}
+  {% template_hook("image_sideinfo") %}
 {% endblock %}
index 2e159be44ff6841504c7ff647985f49eea8644aa..1cc2a763c26f41f97a77b7907f1be096c0a85d19 100644 (file)
@@ -18,7 +18,6 @@
 {% extends "mediagoblin/base.html" %}
 
 {% import "/mediagoblin/utils/wtforms.html" as wtforms_util %}
-{% from "/mediagoblin/utils/templatehooks.html" import template_hook %}
 {% from "mediagoblin/utils/pagination.html" import render_pagination %}
 
 {% block title %}{{ media.title }} &mdash; {{ super() }}{% endblock %}
@@ -31,7 +30,7 @@
   <script type="text/javascript"
           src="{{ request.staticdirect('/js/keyboard_navigation.js') }}"></script>
 
-  {{ template_hook("media_extrahead") }}
+  {% template_hook("media_extrahead") %}
 {% endblock mediagoblin_head %}
 
 {% block mediagoblin_content %}
       </p>
     {% endif %}
 
-    {{ template_hook("media_sideinfo") }}
+    {% template_hook("media_sideinfo") %}
 
     {% block mediagoblin_sidebar %}
     {% endblock %}
diff --git a/mediagoblin/templates/mediagoblin/utils/templatehooks.html b/mediagoblin/templates/mediagoblin/utils/templatehooks.html
deleted file mode 100644 (file)
index 615ea63..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-{#
-# GNU MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Affero General Public License for more details.
-#
-# You should have received a copy of the GNU Affero General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-#}
-{% macro template_hook(hook_name) %}
-  {% for template in get_hook_templates(hook_name) %}
-    {% include template %}
-  {% endfor %}
-{% endmacro %}
index c76ce639398525aedf6b00128dd69e2e47e8efde..f83c82280e7b1f0f4ff4435595b9add91303c9ce 100644 (file)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from math import ceil
+
 import jinja2
+from jinja2.ext import Extension
+from jinja2.nodes import Include, Const
+
 from babel.localedata import exists
 from werkzeug.urls import url_quote_plus
 
@@ -49,7 +53,9 @@ def get_jinja_env(template_loader, locale):
     template_env = jinja2.Environment(
         loader=template_loader, autoescape=True,
         undefined=jinja2.StrictUndefined,
-        extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape'])
+        extensions=[
+            'jinja2.ext.i18n', 'jinja2.ext.autoescape',
+            TemplateHookExtension])
 
     template_env.install_gettext_callables(
         mg_globals.thread_scope.translations.ugettext,
@@ -102,3 +108,33 @@ def render_template(request, template_path, context):
 def clear_test_template_context():
     global TEMPLATE_TEST_CONTEXT
     TEMPLATE_TEST_CONTEXT = {}
+
+
+class TemplateHookExtension(Extension):
+    """
+    Easily loop through a bunch of templates from a template hook.
+
+    Use:
+      {% template_hook("comment_extras") %}
+
+    ... will include all templates hooked into the comment_extras section.
+    """
+
+    tags={"template_hook"}
+
+    def __init__(self, environment):
+        super(TemplateHookExtension, self).__init__(environment)
+
+    def parse(self, parser):
+        includes = []
+        expr = parser.parse_expression()
+        lineno = expr.lineno
+        hook_name = expr.args[0].value
+
+        for template_name in get_hook_templates(hook_name):
+            includes.append(
+                parser.parse_import_context(
+                    Include(Const(template_name), True, False, lineno=lineno),
+                    True))
+
+        return includes