Skip to content
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 pystache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ def render(template, context=None, **kwargs):
context = context and context.copy() or {}
context.update(kwargs)
return Template(template, context).render()

30 changes: 30 additions & 0 deletions pystache/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pystache import Template
import argparse
import json
from loader import Loader

def main():
parser = argparse.ArgumentParser(description='Render a mustache template with the given context.')
parser.add_argument('template', help='A filename or a template code.')
parser.add_argument('context', help='A filename or a JSON string')
args = parser.parse_args()

if args.template.endswith('.mustache'):
args.template = args.template[:-9]

try:
template = Loader().load_template(args.template)
except IOError:
template = args.template

try:
context = json.load(open(args.context))
except IOError:
context = json.loads(args.context)

print(Template(template, context).render())


if __name__=='__main__':
main()

11 changes: 6 additions & 5 deletions pystache/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

class Loader(object):

template_extension = 'mustache'
template_path = '.'
template_encoding = None
def __init__(self, paths='.', extension='mustache', encoding=None):
self.template_paths = paths
self.template_extension = extension
self.template_encoding = encoding

def load_template(self, template_name, template_dirs=None, encoding=None, extension=None):
'''Returns the template string from a file or throws IOError if it non existent'''
if None == template_dirs:
template_dirs = self.template_path
template_dirs = self.template_paths

if encoding is not None:
self.template_encoding = encoding
Expand Down Expand Up @@ -44,4 +45,4 @@ def _load_template_file(self, file_path):
finally:
f.close()

return template
return template
14 changes: 9 additions & 5 deletions pystache/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ class Template(object):

modifiers = Modifiers()

def __init__(self, template=None, context=None, **kwargs):
def __init__(self, template=None, context=None, partials=None, **kwargs):
from view import View

self.template = template
self.partials = partials

if kwargs:
context.update(kwargs)
Expand Down Expand Up @@ -118,7 +119,7 @@ def _render_tags(self, template):

def _render_dictionary(self, template, context):
self.view.context_list.insert(0, context)
template = Template(template, self.view)
template = Template(template, self.view, self.partials)
out = template.render()
self.view.context_list.pop(0)
return out
Expand Down Expand Up @@ -149,9 +150,12 @@ def _render_comment(self, tag_name):

@modifiers.set('>')
def _render_partial(self, template_name):
from pystache import Loader
markup = Loader().load_template(template_name, self.view.template_path, encoding=self.view.template_encoding)
template = Template(markup, self.view)
if self.partials:
template = Template(self.partials[template_name], self.view, self.partials)
else:
from pystache import Loader
markup = Loader().load_template(template_name, self.view.template_path, encoding=self.view.template_encoding)
template = Template(markup, self.view)
return template.render()

@modifiers.set('=')
Expand Down
3 changes: 2 additions & 1 deletion pystache/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ def __getattr__(self, attr):
raise AttributeError("Attribute '%s' does not exist in View" % attr)

def __str__(self):
return self.render()
return self.render()

2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def publish():
url='http://github.com/defunkt/pystache',
packages=['pystache'],
license='MIT',
entry_points = {
'console_scripts': ['pystache=pystache.commands:main']},
classifiers = (
"Development Status :: 4 - Beta",
"License :: OSI Approved :: MIT License",
Expand Down