-
-
Notifications
You must be signed in to change notification settings - Fork 247
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
Add equivalent to Flask's url_for() method for templates to construct Robyn routes #996
Comments
I have a working call to url_for from a jenja template, it does not affect anything else. Only 2 changes needed to templating.py First, we define url_for (it just returns a fixed str at the moment):
Second, we add this to the jenja environment (just one line needed):
we can see this working by adding url_for to the test.html body:
Then, a one-line addition to test_get_requests.py it fails if url_for does not return the correct fixed str, it passes with the code above:
Will you accept a PR with these changes? |
Hey @dave42w 👋 Thank you for volunteering. One of the contributors tried implementing url_for previously. You can have a look here - https://github.com/sparckles/Robyn/pull/758/files Unfortunately, it was abandoned and we couldn't merge it. However, I liked the approach in the same PR and would be happy to accept a new one 😄 Maybe you can use the PR(https://github.com/sparckles/Robyn/pull/758/files ) as an inspiration? |
I'm very happy to take inspiration from that. I'll create a PR with what I have and see what you think. |
See #1004 |
I suggest the next step should be to take something from the earlier PR https://github.com/sparckles/Robyn/pull/758/files#diff-1276ef09a4b09c2f539dfc4e497e5054f03f2388440a91da0354e74f7fa828cd
Instead of simply adding url_for to the environment as in my first PR we will have 2 benefits
The current tests should still pass and I'll add an additional one for adding a custom function to the Jinja environment. |
I want to separate Flasks url_for into two functions. It doesn't make sense to use a single url_for function for both static routes (passing "static" as an argument") and dynamic routes for functions. Therefore I suggest we go from url_for to
If we think the names are too long then maybe
|
I'm stuck. We have a very clean separation, which means I can't see how to get hold of the list of routes from templating.py Any suggestions? |
Working version committed to PR includes passing test |
In a template we want to be able to create a URL to a Robyn endpoint and we don't want to hard code it.
So if we consider the example: https://robyn.tech/documentation/example_app/modeling_routes and imagine that
renders a template with all the crimes listed. Each crime in the table will need URLs for the details, to edit, to delete. The whole page will need a url to add an additional crime. The end points are all defined in the code:
Flask uses url_for() to do this eg:
As the example is currently written that URL would resolve to
<a href="/crime/42">Crime number 42</a>
The flask url_for has extra capabilities. For example, the need to change the scheme and host is very rare.
The Flask url_for is based on the function name rather than the argument passed to the decorator. If the function name is within a blueprint then the name of the blueprint needs to be in the argument to url_for. In Robyn that means we want the first argument to be the name argument to the SubRouter (in the examples whatever name resolves to eg "frontend") plus the name of the function within that, plus the arguments.
This way if we change the SubRoute prefix or the decorator path the template does not need to be changed.
So these changes will not change the template (as they neither change the name of the module nor the function name)
to
Note we will still need to change the template if we move the function from global into a SubRouter, change the module name, change the function name, or the function arguments.
Adding this function helps make templates less brittle and more reusable.
The text was updated successfully, but these errors were encountered: