jthi3rry / django-silhouette

Elegant Form Templating for Django
MIT License
13 stars 3 forks source link

jinja support #10

Open hampsterx opened 9 years ago

hampsterx commented 9 years ago

Hi Thierry,

Attempting to use silhouette with Jinja (https://niwibe.github.io/django-jinja/) and had to do a couple of hacks :P

silhouette_tags.py


from django.utils.safestring import mark_safe
from django_jinja import library
from jinja2 import contextfunction
import jinja2

def register_tag(silhouette_class):
    # Django_jinja has it's own tag registration
    #register.simple_tag(tag, True, tag_name)
    library.global_function(tag_name,tag)
    tag.contextfunction = True

def __enter__(self):
   # Context is jinja2 context object, doesn't support update
   #self.context.update(scope)    
   self.context.vars.update(scope)

def __exit__(self, *args, **kwargs):
    # Jinja context object doesn't support pop
    #self.context.pop()    
    return self.context

def render(self, context):
   #return render_to_string(self.template,context)
   result =  self.template.render(context)
   return mark_safe(result)

silhouette_filters.py

Basically all of those will need to be registered with django_jinja too. Should just be able to do a decorator similar to what you have done in silhouette_tags.py

I then had to create my own template as the template markup is a bit different but essentially it works. Would be great if jinja could be optionally supported by django-silhouette and even better if you provided a "jinja" template for it.

yes, asking a lot, cheers, TiM~

jthi3rry commented 9 years ago

Hi Tim,

I won't have much time in the near future to look into adding support for jinja2, but feel free to send a pull request :-)

Cheers, Thierry