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

✨ Add support for get_template_from_string() #4

Merged
merged 1 commit into from
Jan 20, 2019
Merged
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
7 changes: 7 additions & 0 deletions moban_handlebars/engine.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import codecs
import sys

import moban.utils as utils
from pybars import Compiler
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_handlebar_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)