Skip to content
This repository

Add template_global decorator #657

Closed
wants to merge 3 commits into from

2 participants

Daniel Chatfield Armin Ronacher
Daniel Chatfield

Adds a decorator to allow developer to register global functions.

Armin Ronacher mitsuhiko closed this in 3b393f8 January 26, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Jan 02, 2013
Daniel Chatfield Add template_global decorator
Adds a decorator to allow developer to register global functions.
df580ad
Daniel Chatfield Fixed line endings
Fixed line endings
f656a20
Daniel Chatfield Fixed line endings properly 661c75e
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 32 additions and 0 deletions. Show diff stats Hide diff stats

  1. 32  flask/app.py
32  flask/app.py
@@ -1164,6 +1164,38 @@ def add_template_test(self, f, name=None):
1164 1164
         """
1165 1165
         self.jinja_env.tests[name or f.__name__] = f
1166 1166
 
  1167
+    @setupmethod
  1168
+    def template_global(self, name=None):
  1169
+        """A decorator that is used to register a custom template global function.
  1170
+        You can specify a name for the global function, otherwise the function
  1171
+        name will be used. Example::
  1172
+
  1173
+          @app.template_global()
  1174
+          def double(n):
  1175
+              return 2 * n
  1176
+
  1177
+        .. versionadded:: 0.10
  1178
+
  1179
+        :param name: the optional name of the global function, otherwise the
  1180
+                     function name will be used.
  1181
+        """
  1182
+        def decorator(f):
  1183
+            self.add_template_global(f, name=name)
  1184
+            return f
  1185
+        return decorator
  1186
+
  1187
+    @setupmethod
  1188
+    def add_template_global(self, f, name=None):
  1189
+        """Register a custom template global function.  Works exactly like the
  1190
+        :meth:`template_global` decorator.
  1191
+
  1192
+        .. versionadded:: 0.10
  1193
+
  1194
+        :param name: the optional name of the global function, otherwise the
  1195
+                     function name will be used.
  1196
+        """
  1197
+        self.jinja_env.globals[name or f.__name__] = f
  1198
+
1167 1199
 
1168 1200
     @setupmethod
1169 1201
     def before_request(self, f):
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.