Cache for _do_template call. May result in nice speed improvement (4-5 times faster).

pull/13000/head
Yannig Perré 9 years ago
parent f528ad1848
commit 87a9485b2f

@ -122,6 +122,7 @@ class Templar:
self._filters = None self._filters = None
self._tests = None self._tests = None
self._available_variables = variables self._available_variables = variables
self._cached_result = {}
if loader: if loader:
self._basedir = loader.get_basedir() self._basedir = loader.get_basedir()
@ -298,18 +299,24 @@ class Templar:
elif resolved_val is None: elif resolved_val is None:
return C.DEFAULT_NULL_REPRESENTATION return C.DEFAULT_NULL_REPRESENTATION
result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides) # Using a cache in order to prevent template calls with already templated variables
cache_key = variable + str(preserve_trailing_newlines) + str(escape_backslashes) + str(overrides)
if convert_data: try:
# if this looks like a dictionary or list, convert it to such using the safe_eval method result = self._cached_result[cache_key]
if (result.startswith("{") and not result.startswith(self.environment.variable_start_string)) or \ except KeyError:
result.startswith("[") or result in ("True", "False"): result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides)
eval_results = safe_eval(result, locals=self._available_variables, include_exceptions=True) if convert_data:
if eval_results[1] is None: # if this looks like a dictionary or list, convert it to such using the safe_eval method
result = eval_results[0] if (result.startswith("{") and not result.startswith(self.environment.variable_start_string)) or \
else: result.startswith("[") or result in ("True", "False"):
# FIXME: if the safe_eval raised an error, should we do something with it? eval_results = safe_eval(result, locals=self._available_variables, include_exceptions=True)
pass if eval_results[1] is None:
result = eval_results[0]
else:
# FIXME: if the safe_eval raised an error, should we do something with it?
pass
self._cached_result[cache_key] = result
#return self._clean_data(result) #return self._clean_data(result)
return result return result

Loading…
Cancel
Save