Improvements of django_adapter

2 views
Skip to first unread message

Olivier Verdier

unread,
Oct 25, 2007, 3:48:05 PM10/25/07
to brevé template engine
A couple of improvements to allow passing some arguments from the
settings file to the brevé template. One may define
settings.BREVE_OPTS, a dictionary which will be passed around for the
creation of the template. I personally like the options:
BREVE_OPTS = {'xml_encoding':'', 'xmlns':'http://www.w3.org/1999/
xhtml'}.
(another quirk of brevé is that tags.xmlns is defined as
'xmlns="something'. is this a typo?)

Anyway, the diff for django_adapter is as follows:

Index: django_adapter.py
===================================================================
--- django_adapter.py (revision 267)
+++ django_adapter.py (working copy)
@@ -7,9 +7,13 @@

from django.conf import settings
BREVE_ROOT = settings.BREVE_ROOT
+try:
+ BREVE_OPTS = settings.BREVE_OPTS
+except AttributeError:
+ BREVE_OPTS = {}

def flatten_string ( obj ):
- return unicode ( obj ).encode ( settings.DEFAULT_CHARSET )
+ return obj

class _loader ( object ):
def __init__ ( self, root, breve_opts = None ):
@@ -35,11 +39,12 @@
django.template.TemplateDoesNotExist.
"""
def __init__ ( self, names, root = BREVE_ROOT, breve_opts =
{ } ):
- self.template = Template ( tags = html.tags, root = root )
+ self.template = Template ( tags = html.tags, root = root,
**breve_opts)
self.names = names
self.breve_opts = breve_opts

def render ( self, vars = None ):
+ import os # why??
if vars == None:
vars = { }
elif isinstance ( vars, Context ):
@@ -69,7 +74,7 @@
for name in
self.names ] ) )


-loader = _loader ( root = BREVE_ROOT )
+loader = _loader ( root = BREVE_ROOT, breve_opts = BREVE_OPTS)

def render_to_response ( template, vars = None ):
t = loader.get_template ( template )

Cliff Wells

unread,
Nov 9, 2007, 5:11:52 AM11/9/07
to breve-...@googlegroups.com
On Thu, 2007-10-25 at 12:48 -0700, Olivier Verdier wrote:
> A couple of improvements to allow passing some arguments from the
> settings file to the brevé template. One may define
> settings.BREVE_OPTS, a dictionary which will be passed around for the
> creation of the template. I personally like the options:
> BREVE_OPTS = {'xml_encoding':'', 'xmlns':'http://www.w3.org/1999/
> xhtml'}.
> (another quirk of brevé is that tags.xmlns is defined as
> 'xmlns="something'. is this a typo?)

Most definitely. Fixed in CVS.

> Anyway, the diff for django_adapter is as follows:
>
> Index: django_adapter.py
> ===================================================================
> --- django_adapter.py (revision 267)
> +++ django_adapter.py (working copy)
> @@ -7,9 +7,13 @@
>
> from django.conf import settings
> BREVE_ROOT = settings.BREVE_ROOT
> +try:
> + BREVE_OPTS = settings.BREVE_OPTS
> +except AttributeError:
> + BREVE_OPTS = {}
>
> def flatten_string ( obj ):
> - return unicode ( obj ).encode ( settings.DEFAULT_CHARSET )
> + return obj

What is this going to mean in terms of existing code? I believe that
the line you're removing was in fact a patch from another Django user.


> class _loader ( object ):
> def __init__ ( self, root, breve_opts = None ):
> @@ -35,11 +39,12 @@
> django.template.TemplateDoesNotExist.
> """
> def __init__ ( self, names, root = BREVE_ROOT, breve_opts =
> { } ):
> - self.template = Template ( tags = html.tags, root = root )
> + self.template = Template ( tags = html.tags, root = root,
> **breve_opts)
> self.names = names
> self.breve_opts = breve_opts
>
> def render ( self, vars = None ):
> + import os # why??

Indeed, why? I notice the original has a seemingly unneeded "import os"
a bit later on.

I'm a bit curious what the overall effect of these changes are. I don't
use Django much myself (except Satchmo, but I just use the provided
templates for that), so I'm not in a good position to test this.

Cliff

Olivier

unread,
Nov 10, 2007, 11:32:51 AM11/10/07
to brevé template engine

The impact on existing code is zero. Any working project will work
just as well with this patch. This patch is only to allow the use of
the already existing breve_opt variable. That variable existed but
there was no way to actually give this variable any value. This allows
django+brevé users to customise the brevé behaviour via the django
settings file.

>
> > class _loader ( object ):
> > def __init__ ( self, root, breve_opts = None ):
> > @@ -35,11 +39,12 @@
> > django.template.TemplateDoesNotExist.
> > """
> > def __init__ ( self, names, root = BREVE_ROOT, breve_opts =
> > { } ):
> > - self.template = Template ( tags = html.tags, root = root )
> > + self.template = Template ( tags = html.tags, root = root,
> > **breve_opts)
> > self.names = names
> > self.breve_opts = breve_opts
>
> > def render ( self, vars = None ):
> > + import os # why??
>
> Indeed, why? I notice the original has a seemingly unneeded "import os"
> a bit later on.

I don't know. If I don't put that I get a NameError exception saying
that os is not defined (or that `os` is none, i don't remember). I
have no idea of why this is happening since `os` is imported at the
beginning of the file. I believe that the name `os` gets somehow
redefined or deleted along the way but i was not able to determine
exactly where.

>
> I'm a bit curious what the overall effect of these changes are. I don't
> use Django much myself (except Satchmo, but I just use the provided
> templates for that), so I'm not in a good position to test this.

As I said, the effect of this patch will go unnoticed to existing
project. It just adds new possibilities.

>
> Cliff

Reply all
Reply to author
Forward
0 new messages