# -*- coding: utf-8 -*-
import io
-from os import linesep
+from os import linesep, path
try:
from collections.abc import Sequence, Iterator, Callable
# Maybe the partial is in the dictionary
return partials_dict[name]
except KeyError:
+ # Don't try loading from the file system if the partials_path is None or empty
+ if partials_path is None or partials_path == '': return ''
+
# Nope...
try:
# Maybe it's in the file system
path_ext = ('.' + partials_ext if partials_ext else '')
- path = partials_path + '/' + name + path_ext
- with io.open(path, 'r', encoding='utf-8') as partial:
+ partial_path = path.join(partials_path, name + path_ext)
+ with io.open(partial_path, 'r', encoding='utf-8') as partial:
return partial.read()
except IOError:
data -- A python dictionary with your data scope
partials_path -- The path to where your partials are stored
+ If set to None, then partials won't be loaded from the file system
(defaults to '.')
partials_ext -- The extension that you want the parser to look for
self.assertEqual(result, expected)
+ def test_disabled_partials(self):
+ os.chdir('tests')
+ resultNone = chevron.main('test.mustache', 'data.json',
+ partials_path=None)
+
+ resultEmpty = chevron.main('test.mustache', 'data.json',
+ partials_path='')
+
+ with io.open('test-partials-disabled.rendered', 'r', encoding='utf-8') as f:
+ expected = f.read()
+ if not python3:
+ expected = expected.encode('utf-8')
+
+ self.assertEqual(resultNone, expected)
+ self.assertEqual(resultEmpty, expected)
+ os.chdir('..')
+
# Run unit tests from command line
if __name__ == "__main__":
--- /dev/null
+
+variable test
+===
+test
+===
+test
+===
+
+comment test
+===
+===
+===
+
+html escape test (triple brackets)
+===
+< > & "
+===
+< > & "
+===
+
+html escape test (ampersand)
+===
+< > & "
+===
+< > & "
+===
+
+html escape test (normal)
+===
+< > & "
+===
+< > & "
+===
+
+section test (truthy)
+===
+true
+===
+true
+===
+
+section test (falsy)
+===
+===
+===
+
+section test (list)
+===
+number: 1
+name: one
+---
+number: 2
+name: two
+---
+number: 3
+name: three
+---
+===
+number: 1
+name: one
+---
+number: 2
+name: two
+---
+number: 3
+name: three
+---
+===
+
+section test (scope)
+===
+test
+new test
+===
+test
+new test
+===
+
+inverted section test (truthy)
+===
+===
+===
+
+inverted section test (falsy)
+===
+false
+===
+false
+===
+
+partial test
+===
+===
+this is a partial
+===
+
+delimiter test
+===
+test
+test
+===
+test
+test
+===
+
+unicode test (basic)
+===
+(╯°□°)╯︵ ┻━┻
+===
+(╯°□°)╯︵ ┻━┻
+===
+
+unicode test (variable)
+===
+(╯°□°)╯︵ ┻━┻
+===
+(╯°□°)╯︵ ┻━┻
+===
+
+unicode test (partial)
+===
+===
+(╯°□°)╯︵ ┻━┻
+===
+
+unicode test (no-escape)
+===
+<a>(╯°□°)╯︵ ┻━┻
+===
+<a>(╯°□°)╯︵ ┻━┻
+===