templates.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """Templates to use in the reflex compiler."""
  2. from jinja2 import Environment, FileSystemLoader, Template
  3. from reflex import constants
  4. from reflex.utils.format import json_dumps
  5. class ReflexJinjaEnvironment(Environment):
  6. """The template class for jinja environment."""
  7. def __init__(self) -> None:
  8. """Set default environment."""
  9. extensions = ["jinja2.ext.debug"]
  10. super().__init__(
  11. extensions=extensions,
  12. trim_blocks=True,
  13. lstrip_blocks=True,
  14. )
  15. self.filters["json_dumps"] = json_dumps
  16. self.filters["react_setter"] = lambda state: f"set{state.capitalize()}"
  17. self.loader = FileSystemLoader(constants.JINJA_TEMPLATE_DIR)
  18. self.globals["const"] = {
  19. "socket": constants.SOCKET,
  20. "result": constants.RESULT,
  21. "router": constants.ROUTER,
  22. "event_endpoint": constants.Endpoint.EVENT.name,
  23. "events": constants.EVENTS,
  24. "state": constants.STATE,
  25. "final": constants.FINAL,
  26. "processing": constants.PROCESSING,
  27. "initial_result": {
  28. constants.STATE: None,
  29. constants.EVENTS: [],
  30. constants.FINAL: True,
  31. constants.PROCESSING: False,
  32. },
  33. "color_mode": constants.COLOR_MODE,
  34. "toggle_color_mode": constants.TOGGLE_COLOR_MODE,
  35. "use_color_mode": constants.USE_COLOR_MODE,
  36. "hydrate": constants.HYDRATE,
  37. }
  38. def get_template(name: str) -> Template:
  39. """Get render function that work with a template.
  40. Args:
  41. name: The template name. "/" is used as the path separator.
  42. Returns:
  43. A render function.
  44. """
  45. return ReflexJinjaEnvironment().get_template(name=name)
  46. # Template for the Reflex config file.
  47. RXCONFIG = get_template("app/rxconfig.py.jinja2")
  48. # Code to render a NextJS Document root.
  49. DOCUMENT_ROOT = get_template("web/pages/_document.js.jinja2")
  50. # Template for the theme file.
  51. THEME = get_template("web/utils/theme.js.jinja2")
  52. # Template for the context file.
  53. CONTEXT = get_template("web/utils/context.js.jinja2")
  54. # Template for Tailwind config.
  55. TAILWIND_CONFIG = get_template("web/tailwind.config.js.jinja2")
  56. # Template to render a component tag.
  57. COMPONENT = get_template("web/pages/component.js.jinja2")
  58. # Code to render a single NextJS page.
  59. PAGE = get_template("web/pages/index.js.jinja2")
  60. # Code to render the custom components page.
  61. COMPONENTS = get_template("web/pages/custom_component.js.jinja2")
  62. # Sitemap config file.
  63. SITEMAP_CONFIG = "module.exports = {config}".format
  64. # Code to render the root stylesheet.
  65. STYLE = get_template("web/styles/styles.css.jinja2")
  66. # Code that generate the package json file
  67. PACKAGE_JSON = get_template("web/package.json.jinja2")