From: Dylan Brotherston <22044664+Dylan-Brotherston@users.noreply.github.com> Date: Sat, 20 Mar 2021 15:14:28 +0000 (+1100) Subject: [Feature] Add the ability to keep unknown tags X-Git-Url: https://git.devinivas.org/?a=commitdiff_plain;h=bce179372f18022dc1e35b81a5aca61a38ae59f7;p=chevron.git [Feature] Add the ability to keep unknown tags Add a new option `keep` (default: False) to `chevron.renderer.render()` that allows for unknown tags to be kept in the returned string for later processing, eg: `eval()`. --- diff --git a/chevron/renderer.py b/chevron/renderer.py index f69d65f..c0cff7b 100644 --- a/chevron/renderer.py +++ b/chevron/renderer.py @@ -48,7 +48,7 @@ def _html_escape(string): return string -def _get_key(key, scopes, warn=False): +def _get_key(key, scopes, warn, keep, def_ldel, def_rdel): """Get a key from the current scope""" # If the key is a dot @@ -94,6 +94,9 @@ def _get_key(key, scopes, warn=False): if warn: sys.stderr.write("Could not find key '%s'%s" % (key, linesep)) + if keep: + return "%s %s %s" % (def_ldel, key, def_rdel) + return '' @@ -127,7 +130,7 @@ g_token_cache = {} def render(template='', data={}, partials_path='.', partials_ext='mustache', partials_dict={}, padding='', def_ldel='{{', def_rdel='}}', - scopes=None, warn=False): + scopes=None, warn=False, keep=False): """Render a mustache template. Renders a mustache template with a data scope and partial capability. @@ -172,7 +175,9 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', 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 + warn -- Issue a warning to stderr when a template substitution isn't found in the data + + keep -- Keep unreplaced tags when a template substitution isn't found in the data Returns: @@ -225,7 +230,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', # If we're a variable tag elif tag == 'variable': # Add the html escaped key to the output - thing = _get_key(key, scopes, warn=warn) + thing = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_ldel) if thing is True and key == '.': # if we've coerced into a boolean by accident # (inverted tags do this) @@ -238,7 +243,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', # If we're a no html escape tag elif tag == 'no escape': # Just lookup the key and add it - thing = _get_key(key, scopes, warn=warn) + thing = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_ldel) if not isinstance(thing, unicode_type): thing = unicode(str(thing), 'utf-8') output += thing @@ -246,7 +251,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', # If we're a section tag elif tag == 'section': # Get the sections scope - scope = _get_key(key, scopes, warn=warn) + scope = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_ldel) # If the scope is a callable (as described in # https://mustache.github.io/mustache.5.html) @@ -338,7 +343,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', # If we're an inverted section elif tag == 'inverted section': # Add the flipped scope to the scopes - scope = _get_key(key, scopes, warn=warn) + scope = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_ldel) scopes.insert(0, not scope) # If we're a partial