diff --git a/reflex/.templates/jinja/web/pages/macros.js.jinja2 b/reflex/.templates/jinja/web/pages/macros.js.jinja2 index 9e3f29510..68810d896 100644 --- a/reflex/.templates/jinja/web/pages/macros.js.jinja2 +++ b/reflex/.templates/jinja/web/pages/macros.js.jinja2 @@ -1,45 +1,29 @@ -{% macro sortHooks(hooks) %} - {% set internal_hooks = [] %} - {% set pre_trigger_hooks = [] %} - {% set post_trigger_hooks = [] %} - - {% for hook, data in hooks %} - {% if data and data.position and data.position == const.hook_position.INTERNAL %} - {% set internal_hooks = internal_hooks + [hook] %} - {% elif data and data.position and data.position == const.hook_position.POST_TRIGGER %} - {% set post_trigger_hooks = post_trigger_hooks+ [hook] %} - {% else %} - {% set pre_trigger_hooks = pre_trigger_hooks + [hook] %} - {% endif %} - {% endfor %} -{% endmacro %} - {% macro renderHooks(hooks) %} - {{ sortHooks(hooks) }} + {% set sorted_hooks = sort_hooks(hooks) %} {# Render the grouped hooks #} - {% for hook in internal_hooks %} + {% for hook, _ in sorted_hooks[const.hook_position.INTERNAL] %} {{ hook }} {% endfor %} - {% for hook in pre_trigger_hooks %} + {% for hook, _ in sorted_hooks[const.hook_position.PRE_TRIGGER] %} {{ hook }} {% endfor %} - {% for hook in post_trigger_hooks %} + {% for hook, _ in sorted_hooks[const.hook_position.POST_TRIGGER] %} {{ hook }} {% endfor %} {% endmacro %} {% macro renderHooksWithMemo(hooks, memo)%} - {{ sortHooks(hooks) }} - + {% set sorted_hooks = sort_hooks(hooks) %} + {# Render the grouped hooks #} - {% for hook in internal_hooks %} + {% for hook, _ in sorted_hooks[const.hook_position.INTERNAL] %} {{ hook }} {% endfor %} - {% for hook in pre_trigger_hooks %} + {% for hook, _ in sorted_hooks[const.hook_position.PRE_TRIGGER] %} {{ hook }} {% endfor %} @@ -47,7 +31,7 @@ {{ hook }} {% endfor %} - {% for hook in post_trigger_hooks %} + {% for hook, _ in sorted_hooks[const.hook_position.POST_TRIGGER] %} {{ hook }} {% endfor %} diff --git a/reflex/compiler/templates.py b/reflex/compiler/templates.py index 631aa4ee2..5445e43ab 100644 --- a/reflex/compiler/templates.py +++ b/reflex/compiler/templates.py @@ -3,9 +3,43 @@ from jinja2 import Environment, FileSystemLoader, Template from reflex import constants +from reflex.constants import Hooks from reflex.utils.format import format_state_name, json_dumps +def _sort_hooks(hooks): + """Sort the hooks by their position. + + Args: + hooks: The hooks to sort. + + Returns: + The sorted hooks. + """ + sorted_hooks = { + Hooks.HookPosition.INTERNAL: [], + Hooks.HookPosition.PRE_TRIGGER: [], + Hooks.HookPosition.POST_TRIGGER: [], + } + + for hook, data in hooks: + if data and data.position and data.position == Hooks.HookPosition.INTERNAL: + sorted_hooks[Hooks.HookPosition.INTERNAL].append((hook, data)) + elif not data or ( + not data.position + or data.position == constants.Hooks.HookPosition.PRE_TRIGGER + ): + sorted_hooks[Hooks.HookPosition.PRE_TRIGGER].append((hook, data)) + elif ( + data + and data.position + and data.position == constants.Hooks.HookPosition.POST_TRIGGER + ): + sorted_hooks[Hooks.HookPosition.POST_TRIGGER].append((hook, data)) + + return sorted_hooks + + class ReflexJinjaEnvironment(Environment): """The template class for jinja environment.""" @@ -47,6 +81,7 @@ class ReflexJinjaEnvironment(Environment): "frontend_exception_state": constants.CompileVars.FRONTEND_EXCEPTION_STATE_FULL, "hook_position": constants.Hooks.HookPosition, } + self.globals["sort_hooks"] = _sort_hooks def get_template(name: str) -> Template: