Skip to content

Commit

Permalink
Merge pull request #4 from rtibbles/template_tag_fixes
Browse files Browse the repository at this point in the history
Template tag fixes
  • Loading branch information
benjaoming committed May 18, 2016
2 parents 4580c75 + cea7174 commit c8dd38a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
5 changes: 3 additions & 2 deletions kolibri/core/templates/kolibri/base.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{% load i18n kolibri_tags webpack_tags %}<html>
<head>
<title>{% block title %}{% endblock %} - {% trans "Kolibri" %}</title>
{% webpack_assets 'default_frontend' %}
{% base_frontend_assets %}
{% webpack_asset 'default_frontend' %}
{% webpack_base_assets %}
{% webpack_base_async_assets %}
</head>
<body>

Expand Down
13 changes: 7 additions & 6 deletions kolibri/core/webpack/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,13 @@ class WebpackInclusionHook(hooks.KolibriHook):

def __init__(self, *args, **kwargs):
super(WebpackInclusionHook, self).__init__(*args, **kwargs)
assert \
self.bundle_class is not None,\
"Must specify bundle property, this one did not: {} ({})".format(
type(self),
type(self.bundle)
)
if not self._meta.abstract:
assert \
self.bundle_class is not None,\
"Must specify bundle_class property, this one did not: {} ({})".format(
type(self),
type(self.bundle_class)
)

def render_to_page_load_sync_html(self, extension=None):
html = ""
Expand Down
15 changes: 3 additions & 12 deletions kolibri/core/webpack/templatetags/webpack_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from django.utils.safestring import mark_safe

from .. import hooks
from ..utils import webpack_asset_render

register = template.Library()

Expand Down Expand Up @@ -68,12 +69,7 @@ def webpack_base_assets():
:return: HTML of script tags to insert into base.html
"""
tags = []
for hook in hooks.FrontEndBaseSyncHook().registered_hooks:
tags.append(
hook.render_to_page_load_sync_html()
)
return mark_safe('\n'.join(tags))
return webpack_asset_render(hooks.FrontEndBaseSyncHook, async=False)


@register.simple_tag()
Expand All @@ -85,9 +81,4 @@ def webpack_base_async_assets():
:return: HTML of script tags to insert into base.html
"""
tags = []
for hook in hooks.FrontEndBaseASyncHook().registered_hooks:
tags.append(
hook.render_to_page_load_async_html()
)
return mark_safe('\n'.join(tags))
return webpack_asset_render(hooks.FrontEndBaseASyncHook, async=True)
17 changes: 17 additions & 0 deletions kolibri/core/webpack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from __future__ import absolute_import, print_function, unicode_literals

from django.conf import settings
from django.utils.safestring import mark_safe


def render_as_url(chunk):
"""
Expand All @@ -23,3 +25,18 @@ def render_as_url(chunk):
static = getattr(settings, 'STATIC_URL')
url = chunk.get('publicPath') or chunk['url']
return "{static}{url}".format(static=static, url=url)

def webpack_asset_render(HookClass, async=False):
"""
This is produces content for a script tag for a WebpackInclusionHook subclass that implement
different render to html methods either sync or async.
:param HookClass: a subclass of WebpackInclusionHook
:param sync: Render sync or async.
:return: HTML of script tags to insert
"""
tags = []
for hook in HookClass().registered_hooks:
tags.append(
hook.render_to_page_load_sync_html() if not async else hook.render_to_page_load_async_html()
)
return mark_safe('\n'.join(tags))
6 changes: 4 additions & 2 deletions kolibri/plugins/example_plugin/kolibri_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ class ExampleAsset(webpack_hooks.WebpackBundleHook):
unique_slug = "example_plugin"
src_file = "kolibri/plugins/example_plugin/assets/example/example_module.js"
static_dir = "kolibri/plugins/example_plugin/static"
events = {
"hello": "hello_world"
}


class ExampleInclusionHook(webpack_hooks.FrontEndBaseSyncHook):
class ExampleInclusionHook(webpack_hooks.FrontEndBaseASyncHook):

bundle_class = ExampleAsset

0 comments on commit c8dd38a

Please sign in to comment.