niwinz / django-jinja

Simple and nonobstructive jinja2 integration with Django.
http://niwinz.github.io/django-jinja/latest/
BSD 3-Clause "New" or "Revised" License
363 stars 102 forks source link

Add support for Django-unicorn? #299

Open bschollnick opened 1 year ago

bschollnick commented 1 year ago

Folks,

is there someway to add compatibility with Django unicorn? Out of the box it's not working, and the closest I can come up with is an environment patch:

` from django.templatetags.static import static from django.urls import reverse from django_icons.templatetags.icons import icon_tag from jinja2 import Environment from django_unicorn.templatetags.unicorn import unicorn_scripts, unicorn

def environment(options): env = Environment(options) env.globals.update({ 'static': static, 'url': reverse, 'icon':icon_tag, 'unicorn':unicorn, 'unicorn_scripts':unicorn_scripts, }) return env `

But I'm having issues with unicorn requiring parameters to be passed? I'm talking with the Django-Unicorn folks, but I'm surprised this hasn't come up before... I suspect that since unicorn is suppose to be loaded that it's causing issues? But that's a guess...

auvipy commented 1 year ago

what does django unicorn do?

bschollnick commented 1 year ago

Unicorn is a reactive component framework that progressively enhances a normal Django view, makes AJAX calls in the background, and dynamically updates the DOM. It seamlessly extends Django past its server-side framework roots without giving up all of its niceties or re-building your website.

I’d like to use it to replace my mishmash of AJAX calls…

I’ve located the template tag file, but it’s not behaving the way I would expect… The “unicorn” function is requiring parameters to be passed in.

The author states:

To answer your question, parser and token arguments are used for the unicorn function because it's a custom templatetag that jumps through a bunch of hoops to parse the args, kwargs, render the component (similar to how the include templatetag works if I remember correctly), etc. That all happens in render. For more details on how this works, check out Writing the compilation function in the docs.

I look forward to seeing if this is possible and what you come up with!

While helpful, to me it isn't, since I'm not sure where to investigate next? I can't figure out where the actual javascript is being added to the template, and so forth.

I'm really open to suggestions here, I've made some interesting (but mostly unrelated) discoveries in researching this. Including that I might be seriously unoptimized in my ajax routine... but that doesn't help here...

wizpig64 commented 1 year ago

django-unicorn is an intriguing project. I haven't used it myself yet, but I've looked at the code a little bit. I think given that it uses TemplateResponse, there should be nothing stopping it from working. But the magic they're using has a lot of surface area, so i bet there will be a lot of pitfalls to pave over. For example, their unicorn_errors tag points to unicorn/errors.html, and you might have to that django template with a with a jinja version, and that might require a custom templatetag of its own.

If I had infinite time I'd build out a django-jinja-unicorn project myself just to figure this out, because learning django-unicorn would be cool. But for now, I'll say I'm open to reviewing pull requests.