Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework TemplateEngine to support code #4023

Merged
merged 10 commits into from
Jan 8, 2024
Merged

Conversation

IvanIvanoff
Copy link
Member

@IvanIvanoff IvanIvanoff commented Dec 7, 2023

Changes

Rework the TemplateEngine run/{1,2} and run_generate_positional_params/{1,2} functions to also support code templates with {% %}.

As part of the rework:

  • Split the functionality into multiple modules;
  • Add better documentation and tests;
  • Rework the return format of the functions for better error handling;

Example:

template = "My name is {{name}} and 5 + 2 = {% 5 + 2 %}}"
Sanbase.TemplateEngine.run(template, params: %{name: "Ivan"})
"My name is Ivan and 5 + 2 = 7"

To provide non-empty environment for the code segments, provide the :env option:

env = Sanbase.SanLang.Environment.new()

env =
  Sanbase.SanLang.Environment.put_env_bindings(env, %{"a" => 1, "b" => "some string value"})

template = """
1 + @a = {% 1 + @a %}
The value of @b is {% @b %}
"""

Sanbase.TemplateEngine.run!(template, env: env) 
"""
1 + @a = 2
The value of @b is some string value
"""

Ticket

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have tried to find clearer solution before commenting hard-to-understand parts of code
  • I have added tests that prove my fix is effective or that my feature works

@IvanIvanoff IvanIvanoff force-pushed the support-san-lang-in-queries branch 2 times, most recently from 3bdb59e to f8e23c9 Compare December 12, 2023 16:01
@IvanIvanoff IvanIvanoff changed the title Support san lang in queries Rework TemplateEngine to support code Dec 14, 2023
@IvanIvanoff IvanIvanoff requested a review from tspenov December 14, 2023 11:00
@IvanIvanoff IvanIvanoff force-pushed the support-san-lang-in-queries branch from e76030d to 3fb9611 Compare December 14, 2023 11:07
lib/sanbase/san_lang/san_lang.ex Show resolved Hide resolved
@IvanIvanoff IvanIvanoff force-pushed the support-san-lang-in-queries branch 3 times, most recently from ffa7282 to e5f0345 Compare December 18, 2023 13:43
@IvanIvanoff IvanIvanoff force-pushed the support-san-lang-in-queries branch from ab942df to 7bff2c3 Compare January 8, 2024 10:42
@IvanIvanoff IvanIvanoff force-pushed the support-san-lang-in-queries branch from 7bff2c3 to 3184119 Compare January 8, 2024 13:06
@IvanIvanoff IvanIvanoff merged commit 7f4df44 into master Jan 8, 2024
4 checks passed
@delete-merged-branch delete-merged-branch bot deleted the support-san-lang-in-queries branch January 8, 2024 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants