-
-
Notifications
You must be signed in to change notification settings - Fork 229
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
Added add_template_global function to JinjaTemplate. #738
base: main
Are you sure you want to change the base?
Added add_template_global function to JinjaTemplate. #738
Conversation
… method to allow methods that can be called from within jinja2 templates. example usage can be like this from robyn import Robyn from robyn.templating import JinjaTemplate app = Robyn(__file__) def uppercase(string): return string.upper() current_file_path = pathlib.Path(__file__).parent.resolve() JINJA_TEMPLATE = JinjaTemplate(os.path.join(current_file_path, "templates")) JINJA_TEMPLATE.add_template_global(uppercase) #or JINJA_TEMPLATE.add_template_global(uppercase, 'upper')
Someone is attempting to deploy a commit to the sparckles Team on Vercel. A member of the Team first needs to authorize it. |
for more information, see https://pre-commit.ci
@sansyrox please review. |
Hey @JarriqTheTechie 👋 Thank you for the PR 😄 For my understanding, this PR aims to enable the user to inject a Python function in a jinja template? |
Yes this is correct. Similar to how in flask there are functions such as url_for() This pr lays the foundation to seamlessly expose functions similar to those above in jinja. I actually put the PR together based on the url_for() enhancement that I saw in issues. |
@JarriqTheTechie , that is very smart!
One more question, is |
I copied the naming and signature from flask. |
Alright perfect @JarriqTheTechie . I will do a complete review tomorrow 😄 |
@@ -28,5 29,10 @@ def render_template(self, template_name, **kwargs) -> Response: | |||
headers=Headers({"Content-Type": "text/html; charset=utf-8"}), | |||
) | |||
|
|||
def add_template_global(self, func: Callable, name: str | None = None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @JarriqTheTechie 👋
Just two suggestions
- can you write a docstring for this function?
- can you add docs for this function in
docs_src
folder? Herejinja_template = JinjaTemplate(os.path.join(current_file_path, "templates"))
Hey @JarriqTheTechie 👋 Any updates on this PR? |
Will work on this when I get some free time this weekend. |
Method to allow functions that can be called from within jinja2 templates.
example usage can be like this
in the jinja template the above example can be used like.