Repository: pallets/werkzeug

Issue:
Deprecate werkzeug.Template
The builtin templating language should be deprecated. Reasoning: uses deprecated functionality and cannot be ported to Python 3 at all without going through the new ast compiler of Python 2.6.

We should recommend proper templating languages instead. The last internal user of Werkzeug is the testapp and debugger.

Related tickets: #22, #24



Discussion:

Resolving patch:
diff --git a/werkzeug/contrib/jsrouting.py b/werkzeug/contrib/jsrouting.py
--- a/werkzeug/contrib/jsrouting.py
+++ b/werkzeug/contrib/jsrouting.py
@@ -12,23 +12,25 @@
 try:
     from simplejson import dumps
 except ImportError:
-    def dumps(*args):
-        raise RuntimeError('simplejson required for jsrouting')
+    try:
+        from json import dumps
+    except ImportError:
+        def dumps(*args):
+            raise RuntimeError('simplejson required for jsrouting')
 
 from inspect import getmro
-from werkzeug.templates import Template
 from werkzeug.routing import NumberConverter
 
 
-_javascript_routing_template = Template(u'''\
-<% if name_parts %>\
-<% for idx in xrange(0, len(name_parts) - 1) %>\
-if (typeof ${'.'.join(name_parts[:idx + 1])} === 'undefined') \
-${'.'.join(name_parts[:idx + 1])} = {};
-<% endfor %>\
-${'.'.join(name_parts)} = <% endif %>\
-(function (server_name, script_name, subdomain, url_scheme) {
-    var converters = ${', '.join(converters)};
+def render_template(name_parts, rules, converters):
+    result = u''
+    if name_parts:
+        for idx in xrange(0, len(name_parts) - 1):
+            name = u'.'.join(name_parts[:idx + 1])
+            result += u"if (typeof %s === 'undefined') %s = {}\n" % (name, name)
+        result += '%s = ' % '.'.join(name_parts)
+    result += """(function (server_name, script_name, subdomain, url_scheme) {
+    var converters = %(converters)s;
     var rules = $rules;
     function in_array(array, value) {
         if (array.indexOf != undefined) {
@@ -160,7 +162,8 @@ def dumps(*args):
                    + '/' + lstrip(rv.path, '/');
         }
     };
-})''')
+})""" % {'converters': u', '.join(converters)}
+    return result
 
 
 def generate_map(map, name='url_map'):
@@ -203,11 +206,9 @@ def generate_map(map, name='url_map'):
             u'defaults':    rule.defaults
         })
 
-    return _javascript_routing_template.render({
-        'name_parts':   name and name.split('.') or [],
-        'rules':        dumps(rules),
-        'converters':   converters
-    })
+    return render_template(name_parts=name and name.split('.') or [],
+                           rules=dumps(rules),
+                           converters=converters)
 
 
 def generate_adapter(adapter, name='url_for', map_name='url_map'):
diff --git a/werkzeug/templates.py b/werkzeug/templates.py
--- a/werkzeug/templates.py
+++ b/werkzeug/templates.py
@@ -19,6 +19,11 @@
 from werkzeug.datastructures import MultiDict
 
 
+from warnings import warn
+warn(DeprecationWarning('werkzeug.templates is deprecated and '
+                        'will be removed in Werkzeug 1.0'))
+
+
 # Copyright notice: The `parse_data` method uses the string interpolation
 # algorithm by Ka-Ping Yee which originally was part of `Itpl20.py`_.
 #


Test patch: