Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented support for RactiveJS #11

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ included, it's easy to add. Inspired by `django-icanhaz`_.
.. _handlebars.js: http://handlebarsjs.com/
.. _ICanHaz.js: http://icanhazjs.com/
.. _django-icanhaz: http://github.com/carljm/django-icanhaz
.. _ractive.js: http://ractivejs.org

Quick Usage
-----------
Expand Down
4 changes: 3 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ template tags that you can use to render your mustache templates:
ICanHaz.js
* ``{% handlebarsjs ... %}`` renders templates ready for consumption by
Handlebars.js
* ``{% ractivejs ... %}`` renders templates ready for consumption by Ractive.js
* ``{% rawjstemplate ... %}`` renders the raw contents of a mustache template,
after preprocessing

Expand Down Expand Up @@ -341,7 +342,8 @@ It is simple to extend django-jstemplate to prepare your JavaScript templates to
be used with your favorite Javascript library by creating a template node class
that derives from ``jstemplate.templatetags.BaseJSTemplateNode``, and overriding
a single function. Refer to the existing tag definitions for ``mustachejs``,
``icanhazjs``, ``rawjstemplate``, and ``handlebarsjs`` for more information.
``icanhazjs``, ``rawjstemplate``, ``ractivejs`` and ``handlebarsjs`` for more
information.

Source
======
Expand Down
5 changes: 4 additions & 1 deletion jstemplate/finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import glob, os, sys, re
import six

from django.utils.importlib import import_module
try:
from importlib import import_module
except ImportError:
from django.utils.importlib import import_module

from .conf import conf

Expand Down
5 changes: 4 additions & 1 deletion jstemplate/loading.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import unicode_literals

import six
try:
from importlib import import_module
except ImportError:
from django.utils.importlib import import_module
from django.core.exceptions import ImproperlyConfigured
from django.utils.importlib import import_module

from .conf import conf

Expand Down
2 changes: 2 additions & 0 deletions jstemplate/templatetags/jstemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .mustachejs import mustachejs
from .icanhazjs import icanhazjs
from .dustjs import dustjs
from .ractivejs import ractivejs

register = template.Library()

Expand All @@ -14,3 +15,4 @@
register.tag(mustachejs)
register.tag(icanhazjs)
register.tag(dustjs)
register.tag(ractivejs)
31 changes: 31 additions & 0 deletions jstemplate/templatetags/ractivejs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django import template
from .base import BaseJSTemplateNode, jstemplate_tag_helper

register = template.Library()


class RactiveJSNode(BaseJSTemplateNode):
def generate_node_text(self, resolved_name, file_content):
mapping = (
('\\', r'\\'),
('\n', r'\n'),
("'", r"\'"),
)
output = file_content
for pair in mapping:
output = output.replace(*pair)
return (
u"<script>Ractive.TEMPLATES=Ractive.TEMPLATES||{{}};"
u"Ractive.TEMPLATES['{0}']=Ractive.parse('{1}');</script>"
).format(resolved_name, output)


@register.tag
def ractivejs(parser, token):
"""
Finds the RactiveJS template for the given name and renders it surrounded
by the requisite RactiveJS <script> tags.

"""
return jstemplate_tag_helper('ractivejs', RactiveJSNode,
parser, token)
6 changes: 1 addition & 5 deletions jstemplate/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
from .test_finders import *
from .test_loading import *
from .test_ttag import *
from .test_translation import *
from .test_makemessages import *

50 changes: 46 additions & 4 deletions jstemplate/tests/test_ttag.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import mock
import os.path

from django.template import Template, Context, TemplateSyntaxError
from django.test import TestCase
from mock import patch
import six

from .utils import override_settings



__all__ = [
"MustacheJSTemplateTagTest",
"RawTemplateTagTest",
"ICHTemplateTagTest",
"HandlebarsJSTemplateTagTest",
"DustTemplateTagTest",
"RactiveJSTemplateTagTest",
]


Expand Down Expand Up @@ -158,6 +155,51 @@ def test_variable_template_name(self):
"</script>")


class RactiveJSTemplateTagTest(TestCase, BaseJSTemplateTagTestMixin):
tag_string = 'ractivejs'

@override_settings(JSTEMPLATE_DIRS=[DIR])
def test_unicode(self):
res = Template(
"{% load jstemplate %}{% ractivejs 'unicodetemplate' %}"
).render(Context())

expected = (
"<script>Ractive.TEMPLATES=Ractive.TEMPLATES||{};"
"Ractive.TEMPLATES['unicodetemplate']=Ractive.parse("
"'北京'"
");</script>"
)
self.assertEqual(res, expected)

@override_settings(JSTEMPLATE_DIRS=[DIR])
def test_simple(self):
res = Template(
"{% load jstemplate %}{% ractivejs 'testtemplate' %}"
).render(Context())

expected = (
r"<script>Ractive.TEMPLATES=Ractive.TEMPLATES||{};"
r"Ractive.TEMPLATES['testtemplate']=Ractive.parse"
r"('<p>Mustache\'s template full of {{ foo }} and \\.</p>\n');"
r"</script>"
)
self.assertEqual(res, expected)

@override_settings(JSTEMPLATE_DIRS=[DIR])
def test_variable_template_name(self):
res = Template(
"{% load jstemplate %}{% ractivejs templatename %}").render(
Context({"templatename": "testtemplate"})
)

expected = (
r"<script>Ractive.TEMPLATES=Ractive.TEMPLATES||{};"
r"Ractive.TEMPLATES['testtemplate']=Ractive.parse"
r"('<p>Mustache\'s template full of {{ foo }} and \\.</p>\n');"
r"</script>"
)
self.assertEqual(res, expected)


class ICHTemplateTagTest(TestCase, BaseJSTemplateTagTestMixin):
Expand Down