changeset 18582:ef78450c8df6

templater: add get() function to access dict element (e.g. extra)
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Fri, 08 Feb 2013 23:49:14 +0100
parents 3490c91a1fcb
children 3af017bd8ef9
files mercurial/help/templates.txt mercurial/templater.py
diffstat 2 files changed, 22 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/help/templates.txt	Fri Feb 08 21:55:46 2013 +0100
+++ b/mercurial/help/templates.txt	Fri Feb 08 23:49:14 2013 +0100
@@ -44,19 +44,21 @@
 
 In addition to filters, there are some basic built-in functions:
 
+- date(date[, fmt])
+
+- fill(text[, width])
+
+- get(dict, key)
+
 - if(expr, then[, else])
 
 - ifeq(expr, expr, then[, else])
 
-- sub(pat, repl, expr)
-
 - join(list, sep)
 
 - label(label, expr)
 
-- date(date[, fmt])
-
-- fill(text[, width])
+- sub(pat, repl, expr)
 
 Also, for any expression that returns a list, there is a list operator:
 
--- a/mercurial/templater.py	Fri Feb 08 21:55:46 2013 +0100
+++ b/mercurial/templater.py	Fri Feb 08 23:49:14 2013 +0100
@@ -207,6 +207,19 @@
         f = context._filters[n]
         return (runfilter, (args[0][0], args[0][1], f))
 
+def get(context, mapping, args):
+    if len(args) != 2:
+        # i18n: "get" is a keyword
+        raise error.ParseError(_("get() expects two arguments"))
+
+    dictarg = args[0][0](context, mapping, args[0][1])
+    if not util.safehasattr(dictarg, 'get'):
+        # i18n: "get" is a keyword
+        raise error.ParseError(_("get() expects a dict as first argument"))
+
+    key = args[1][0](context, mapping, args[1][1])
+    yield dictarg.get(key)
+
 def join(context, mapping, args):
     if not (1 <= len(args) <= 2):
         # i18n: "join" is a keyword
@@ -285,11 +298,12 @@
     }
 
 funcs = {
+    "get": get,
     "if": if_,
     "ifeq": ifeq,
     "join": join,
+    "label": label,
     "sub": sub,
-    "label": label,
 }
 
 # template engine