From 487197ae1957554b13d88dfcd255e9c0b208d37e Mon Sep 17 00:00:00 2001 From: CLiu13 Date: Sat, 19 Jan 2019 23:12:19 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20support=20for=20get=5Ftemplat?= =?UTF-8?q?e=5Ffrom=5Fstring()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With this addition, moban-handlebars will be compatible with moban 0.3.8. Closes https://github.com/moremoban/moban-handlebars/issues/1 --- moban_handlebars/engine.py | 7 +++++++ tests/test_handlebar_engine.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/moban_handlebars/engine.py b/moban_handlebars/engine.py index 31cb490..150d7c8 100644 --- a/moban_handlebars/engine.py +++ b/moban_handlebars/engine.py @@ -1,4 +1,5 @@ import codecs +import sys import moban.utils as utils from pybars import Compiler @@ -16,6 +17,12 @@ def get_template(self, template_file): hbr_template = Compiler().compile(source.read()) return hbr_template + def get_template_from_string(self, string): + if sys.version_info < (3, 0): + # Python 2 strings are not unicode by default + string = unicode(string) + return Compiler().compile(string) + def apply_template(self, template, data, _): rendered_content = "".join(template(data)) rendered_content = rendered_content diff --git a/tests/test_handlebar_engine.py b/tests/test_handlebar_engine.py index 2de42c9..849d292 100644 --- a/tests/test_handlebar_engine.py +++ b/tests/test_handlebar_engine.py @@ -29,3 +29,15 @@ def test_handlebars_file_tests(): content = output_file.read() eq_(content, "here") os.unlink(output) + + +def test_handlebars_string_template(): + template_string = "{{test}}" + output = "test.txt" + path = os.path.join("tests", "fixtures", "handlebars_tests") + engine = BaseEngine([path], path, EngineHandlebars) + engine.render_string_to_file(template_string, "file_tests.json", output) + with open(output, "r") as output_file: + content = output_file.read() + eq_(content, "here") + os.unlink(output)