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

Added add_template_global function to JinjaTemplate. #738

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

JarriqTheTechie
Copy link

Method to allow functions 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')

in the jinja template the above example can be used like.

{{ uppercase('uppercase me') }}

or

{{ upper('uppercase me') }}

… 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')
Copy link

vercel bot commented Jan 14, 2024

Someone is attempting to deploy a commit to the sparckles Team on Vercel.

A member of the Team first needs to authorize it.

@JarriqTheTechie
Copy link
Author

@sansyrox please review.

@sansyrox
Copy link
Member

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?

@JarriqTheTechie
Copy link
Author

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()
get_flashed_messages()

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.

@sansyrox
Copy link
Member

@JarriqTheTechie , that is very smart!

def add_template_global

One more question, is add_template_global a conventional function name or something that you decided??

@JarriqTheTechie
Copy link
Author

I copied the naming and signature from flask.

@sansyrox
Copy link
Member

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):
Copy link
Member

@sansyrox sansyrox Jan 15, 2024

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

@sansyrox
Copy link
Member

Hey @JarriqTheTechie 👋

Any updates on this PR?

@JarriqTheTechie
Copy link
Author

Will work on this when I get some free time this weekend.

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