Code

Ticket #9666: future_ssi-1.diff

File future_ssi-1.diff, 4.2 KB (added by seanbrant, 4 years ago)

future ssi tag replacement

Line 
1Index: django/templatetags/future_ssi.py
2===================================================================
3--- django/templatetags/future_ssi.py   (revision 0)
4+++ django/templatetags/future_ssi.py   (revision 0)
5@@ -0,0 +1,66 @@
6+from django.conf import settings
7+from django.template import Library, Node, Template, TemplateSyntaxError
8+from django.template.defaulttags import include_is_allowed
9+
10+
11+register = Library()
12+
13+
14+class SsiNode(Node):
15+    def __init__(self, filepath, parsed):
16+        self.filepath, self.parsed = filepath, parsed
17+
18+    def render(self, context):
19+        filepath = self.filepath.resolve(context)
20+        if not include_is_allowed(filepath):
21+            if settings.DEBUG:
22+                return "[Didn't have permission to include file]"
23+            else:
24+                return '' # Fail silently for invalid includes.
25+        try:
26+            fp = open(filepath, 'r')
27+            output = fp.read()
28+            fp.close()
29+        except IOError:
30+            output = ''
31+        if self.parsed:
32+            try:
33+                t = Template(output, name=filepath)
34+                return t.render(context)
35+            except TemplateSyntaxError, e:
36+                if settings.DEBUG:
37+                    return "[Included template had syntax error: %s]" % e
38+                else:
39+                    return '' # Fail silently for invalid included templates.
40+        return output
41+
42+
43+def ssi(parser, token):
44+    """
45+    Outputs the contents of a given file into the page.
46+
47+    Like a simple "include" tag, the ``ssi`` tag includes the contents
48+    of another file -- which must be specified using an absolute path --
49+    in the current page::
50+
51+        {% ssi "/home/html/ljworld.com/includes/right_generic.html" %}
52+
53+    If the optional "parsed" parameter is given, the contents of the included
54+    file are evaluated as template code, with the current context::
55+
56+        {% ssi "/home/html/ljworld.com/includes/right_generic.html" parsed %}
57+    """
58+    bits = token.contents.split()
59+    parsed = False
60+    if len(bits) not in (2, 3):
61+        raise TemplateSyntaxError("'ssi' tag takes one argument: the path to"
62+                                  " the file to be included")
63+    if len(bits) == 3:
64+        if bits[2] == 'parsed':
65+            parsed = True
66+        else:
67+            raise TemplateSyntaxError("Second (optional) argument to %s tag"
68+                                      " must be 'parsed'" % bits[0])
69+    filepath = parser.compile_filter(bits[1])
70+    return SsiNode(filepath, parsed)
71+ssi = register.tag(ssi)
72Index: django/template/defaulttags.py
73===================================================================
74--- django/template/defaulttags.py      (revision 13984)
75+++ django/template/defaulttags.py      (working copy)
76@@ -893,6 +893,12 @@
77 
78         {% ssi /home/html/ljworld.com/includes/right_generic.html parsed %}
79     """
80+
81+    import warnings
82+    warnings.warn('The old style ssi tag is being deprecated. '
83+                  'You can start using the new style ssi (link to docs).',
84+                  category=PendingDeprecationWarning)
85+
86     bits = token.contents.split()
87     parsed = False
88     if len(bits) not in (2, 3):
89Index: docs/ref/templates/builtins.txt
90===================================================================
91--- docs/ref/templates/builtins.txt     (revision 13984)
92+++ docs/ref/templates/builtins.txt     (working copy)
93@@ -885,6 +885,29 @@
94 
95 See also: ``{% include %}``.
96 
97+.. versionchanged:: 1.3
98+
99+If you find yourself needing to use a context variable as the filepath,
100+you can now do that. To do so you must load a new ssi tag that overwrites
101+the previous tag.
102+
103+For example::
104+
105+    {% load future_ssi %}
106+    {% ssi path_to_file parsed %}
107+
108+Now if you have ``path_to_file`` defined in your template context
109+it will use that value. Please note that old paths that are
110+not context variables need to be quoted using the new ssi tag.
111+
112+For example::
113+
114+    {% url "/home/html/ljworld.com/includes/right_generic.html" parsed %}
115+
116+In version 1.3 the old ssi tag throws a ``PendingDeprecationWarning``,
117+then is version 1.4 this will move to a ``DeprecationWarning`` and finally
118+in version 1.5 the old tag will be removed and replaced by the new tag.
119+
120 .. templatetag:: templatetag
121 
122 templatetag