Patchwork [4,of,6,json-style] templater: introduce jsonengine

mail settings
Submitter Gregory Szorc
Date Dec. 31, 2014, 10:45 p.m.
Message ID <850bab749730a01b318f.1420065934@gps-mbp.local>
Download mbox | patch
Permalink /patch/7289/
State Changes Requested
Headers show


Gregory Szorc - Dec. 31, 2014, 10:45 p.m.
# HG changeset patch
# User Gregory Szorc <>
# Date 1420056018 28800
#      Wed Dec 31 12:00:18 2014 -0800
# Node ID 850bab749730a01b318ff100858c06fa87c8d78e
# Parent  055802e347f62d47560201096e6f280601b2f60d
templater: introduce jsonengine

This patch introduces a template engine that specializes in rendering

If a named template doesn't exist, it defaults to rendering a JSON
object for the passed mapping.


diff --git a/mercurial/ b/mercurial/
--- a/mercurial/
+++ b/mercurial/
@@ -625,9 +625,23 @@  class engine(object):
         mapping contains added elements for use during expansion. Is a
         return _flatten(runtemplate(self, mapping, self._load(t)))
-engines = {'default': engine}
+class jsonengine(engine):
+    '''Template engine used for rendering JSON.
+    When this engine is asked to process a template, it defaults to converting
+    all passed mappings into an object and returning the JSON representation of
+    that object. However, if a named entry for a template exists, it overrides
+    this default behavior.
+    '''
+    def process(self, t, mapping):
+        try:
+            return super(jsonengine, self).process(t, mapping)
+        except TemplateNotFound:
+            return _flatten(templatefilters.json(mapping))
+engines = {'default': engine, 'json': jsonengine}
 def stylelist():
     paths = templatepaths()
     if not paths: