-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Process JS code separately [draft] #1856
base: main
Are you sure you want to change the base?
Conversation
Maybe I'll split off some concepts in this PR into separate PRs:
|
Slightly related: what are the recommended methods of string interpolation within |
If escaping all those curly brackets is awkward, you could consider breaking up the string, and only apply the formatting on the part where you want to insert a variable. Example where I insert a custom id:
|
I also tried adapting the On a side note will this pull request need to be finalized before you will make a new release containing the |
Thanks for your comments @hansthen.
Well said, and I agree. I don't think this change is worth it. Maybe if we encounter this more in the future it could become worth it, but for now it's too much additional complexity for too little saved logic.
No, that's not necessary. I split off a small idea from this PR into a separate PR (#1862), maybe that would be nice to get processed before doing a release. I'll also check out #1861. Afterwards, let's do a release. |
Testing out how we can use the concept of the new
JsCode
class, which was added in #1848. Idea is that we no longer have to manually put Javascript code in templates.Problem before was that a Javascript-function-as-Python-string would get transformed into a Javascript string. Because of that we had to put those functions into the template manually.
Now we have a
JsCode
class that we can use to mark strings as Javascript code. When putting stuff in templates, we can check for that class and prevent adding string quotes.I looked into using a custom JSON encoder with the
json
module, but that turns out to be awkward, because what we're producing is not actually valid JSON.So I've opted to make a custom filter like the
tojson
filter we use a lot, but now allowing for Javascript code. To register this filter I had to subclassTemplate
.Using it means:
Template
class fromfolium.template
JsCode.optional_create()
parse_options
.{{ options|tojavascript }}
filter in the template.I've now applied this new workflow on the
LayerControl
,MousePosition
andRealtime
classes.Some smaller included changes:
JsCode
a subclass ofstr
, that makes it easier to put it in a template without additional needed code.JsCode
, I figured that's more user-friendly then requiringJsCode
.MousePosition
example with the custom formatter no longer works with the trailing;
character, so that's a breaking change.If this seems like a good change I'll add tests as well.
Question is whether the added complexity of the new code weighs up to the more straightforward way in working with Javascript code.