]> Devi Nivas Git - chevron.git/commitdiff
[Feature] Add the ability to keep unknown tags
authorDylan Brotherston <22044664+Dylan-Brotherston@users.noreply.github.com>
Sat, 20 Mar 2021 15:14:28 +0000 (02:14 +1100)
committerDylan Brotherston <22044664+Dylan-Brotherston@users.noreply.github.com>
Sat, 20 Mar 2021 15:14:28 +0000 (02:14 +1100)
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()`.

chevron/renderer.py

index f69d65fae282d5d4c91c88e442814af4ea6de933..c0cff7bb6f100129913d61f5133e38591dfce1d3 100644 (file)
@@ -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