]> Devi Nivas Git - chevron.git/commitdiff
Added the parameter partials_dict to render
authornoah morrison <noah@morrison.ph>
Thu, 6 Nov 2014 22:23:07 +0000 (17:23 -0500)
committernoah morrison <noah@morrison.ph>
Thu, 6 Nov 2014 22:23:07 +0000 (17:23 -0500)
partials_dict will be searched before the filesystem is.
It is an easy way to only use strings and not need the filesystem.

entei.py

index 6d6eef4e786fcba5e8ef175ec3854187b5231d25..fb8c7137fd934fd4386e92ed8f7859772dc71100 100755 (executable)
--- a/entei.py
+++ b/entei.py
@@ -115,7 +115,8 @@ def tokenize(template):
         raise UnclosedSection()
 
 
-def render(template, data, partials_path='.', partials_ext='mustache'):
+def render(template, data, partials_path='.', partials_ext='mustache',
+           partials_dict={}):
     """Render a mustache template.
 
     Renders a mustache template with a data scope and partial capability.
@@ -137,6 +138,10 @@ def render(template, data, partials_path='.', partials_ext='mustache'):
                      (defaults to '.')
     partials_ext  -- The extension that you want the parser to look for
                      (defaults to 'mustache')
+    partials_dict -- A python dictionary which will be search for partials
+                     before the filesystem is. {'include': 'foo'} is the same
+                     as a file called include.mustache
+                     (defaults to {})
 
     Returns:
     A string containing the rendered template.
@@ -167,8 +172,12 @@ def render(template, data, partials_path='.', partials_ext='mustache'):
             except (TypeError, KeyError):
                 pass
 
-    def get_partial(path):
-        return partials_path + '/' + path + '.' + partials_ext
+    def get_partial(name):
+        try:
+            return partials_dict[name]
+        except KeyError:
+            path = partials_path + '/' + name + '.' + partials_ext
+            return open(path, 'r')
 
     tokens = tokenize(template)
 
@@ -204,7 +213,7 @@ def render(template, data, partials_path='.', partials_ext='mustache'):
 
         elif tag == 'partial':
             partial = get_partial(key)
-            output += render(open(partial, 'r'), scopes)
+            output += render(partial, scopes)
 
         else:
             print('>>', tag)