@@ -140,9 +140,9 @@ class AttrRef:
140140 config : Configuration
141141 attribute : str
142142
143- def __init__ (self , config : Configuration , attribue : str ):
143+ def __init__ (self , config : Configuration , attribute : str ):
144144 self .config = config
145- self .attribute = attribue
145+ self .attribute = attribute
146146
147147 def __call__ (self , func ):
148148 if not callable (func ):
@@ -163,21 +163,65 @@ class TemplateException(Exception):
163163 pass
164164
165165class TemplateContext :
166- def function (self , func ):
166+ functions : dict [str , Callable ]
167+
168+ def __init__ (self ):
169+ self .functions = {}
170+
171+ def add_function (self , name , func ):
167172 if not callable (func ):
168173 raise TemplateException ("function registration must be used with functions, actual: '%s'" % type (func ).__name__ )
169-
170- return context_func (func )
171174
172- def filter (self , func ):
175+ self .functions [name ] = func
176+
177+ def add_filter (self , name , func ):
173178 if not callable (func ):
174179 raise TemplateException ("function registration must be used with functions, actual: '%s'" % type (func ).__name__ )
175180
176181 raise TemplateException ("filter registration is not supported" )
177182
183+ def function (self , func ):
184+ if isinstance (func , str ):
185+ return TemplateFunctionRef (self , func )
186+
187+ self .add_function (func .__name__ , func )
188+ return func
189+
190+ def filter (self , func ):
191+ if isinstance (func , str ):
192+ return TemplateFilterRef (self , func )
193+
194+ self .add_filter (func .__name__ , func )
195+ return func
196+
178197 def variable (self , func ):
179198 raise TemplateException ("variable registration is not supported" )
180199
200+ class TemplateFunctionRef :
201+ context : TemplateContext
202+ attribute : str
203+
204+ def __init__ (self , context : TemplateContext , attribute : str ):
205+ self .context = context
206+ self .attribute = attribute
207+
208+ def __call__ (self , func ):
209+ self .context .add_function (self .attribute , func )
210+ return func
211+
212+
213+ class TemplateFilterRef :
214+ context : TemplateContext
215+ attribute : str
216+
217+ def __init__ (self , context : TemplateContext , attribute : str ):
218+ self .context = context
219+ self .attribute = attribute
220+
221+ def __call__ (self , func ):
222+ self .context .add_filter (self .attribute , func )
223+ return func
224+
181225def context_func (func ):
182226 func .cube_context_func = True
183227 return func
0 commit comments