return string
-def _get_key(key, scopes):
+def _get_key(key, scopes, warn=False):
"""Get a key from the current scope"""
# If the key is a dot
pass
# We couldn't find the key in any of the scopes
+
+ if warn:
+ sys.stderr.write("Could not find key '%s'" % key)
+
return ''
def render(template='', data={}, partials_path='.', partials_ext='mustache',
partials_dict={}, padding='', def_ldel='{{', def_rdel='}}',
- scopes=None):
+ scopes=None, warn=False):
"""Render a mustache template.
Renders a mustache template with a data scope and partial capability.
scopes -- The list of scopes that get_key will look through
+ warn -- Issue a warning to stderr when a template substitution isn't found in the data
+
Returns:
# If we're a variable tag
elif tag == 'variable':
# Add the html escaped key to the output
- thing = _get_key(key, scopes)
+ thing = _get_key(key, scopes, warn=warn)
if thing is True and key == '.':
# if we've coerced into a boolean by accident
# (inverted tags do this)
# If we're a no html escape tag
elif tag == 'no escape':
# Just lookup the key and add it
- thing = _get_key(key, scopes)
+ thing = _get_key(key, scopes, warn=warn)
if not isinstance(thing, unicode_type):
thing = unicode(str(thing), 'utf-8')
output += thing
# If we're a section tag
elif tag == 'section':
# Get the sections scope
- scope = _get_key(key, scopes)
+ scope = _get_key(key, scopes, warn=warn)
# If the scope is a callable (as described in
# https://mustache.github.io/mustache.5.html)
# If we're an inverted section
elif tag == 'inverted section':
# Add the flipped scope to the scopes
- scope = _get_key(key, scopes)
+ scope = _get_key(key, scopes, warn=warn)
scopes.insert(0, not scope)
# If we're a partial