librelist archives

« back to archive

Adding a custom jinja2 filter: I just can't get this to work!

Adding a custom jinja2 filter: I just can't get this to work!

From:
Guy Matz
Date:
2014-10-07 @ 12:54
Hi!  I've tried everything out there to try to add a custom jinja2 filter
but always get:
TemplateAssertionError: no filter named 'to_nice_yaml'

The majority of this code was lifted from a github project . . . it simply
renders jinja2 back to the browser for debugging, etc. . . .  I'm trying to
add a custom filter 'to_nice_yaml' using a decorator approach here, and
have tried many other approaches which don't work.  Can anyone say what's
wrong with this: http://pastebin.com/Hrnd2NpF

Thanks soooo much,
Guy

Re: [flask] Adding a custom jinja2 filter: I just can't get this to work!

From:
Matthias Urlichs
Date:
2014-10-07 @ 14:41
Hi,

Guy Matz:
> Hi!  I've tried everything out there to try to add a custom jinja2 filter
> but always get:
> TemplateAssertionError: no filter named 'to_nice_yaml'
> 
So where's your index.html?

NB: "if bool(ANYTHING)" means exactly the same as "if ANYTHING".
So drop the "bool()", it's confusing (for people reading the code).

-- 
-- Matthias Urlichs

Fwd: [flask] Adding a custom jinja2 filter: I just can't get this to work!

From:
Guy Matz
Date:
2014-10-08 @ 16:41
Thank you!!  See index.html at:
http://pastebin.com/X0wKKWEw

On Tue, Oct 7, 2014 at 10:41 AM, Matthias Urlichs <matthias@urlichs.de>
wrote:

> Hi,
>
> Guy Matz:
> > Hi!  I've tried everything out there to try to add a custom jinja2 filter
> > but always get:
> > TemplateAssertionError: no filter named 'to_nice_yaml'
> >
> So where's your index.html?
>
> NB: "if bool(ANYTHING)" means exactly the same as "if ANYTHING".
> So drop the "bool()", it's confusing (for people reading the code).
>
> --
> -- Matthias Urlichs
>

Re: Fwd: [flask] Adding a custom jinja2 filter: I just can't get this to work!

From:
Jon Ribbens
Date:
2014-10-08 @ 17:07
On Wed, Oct 08, 2014 at 12:41:50PM -0400, Guy Matz wrote:
>    Thank you!!  See index.html at: 
>    http://pastebin.com/X0wKKWEw

That doesn't include any references to filters at all, let alone any
called "to_nice_yaml".

Re: Fwd: [flask] Adding a custom jinja2 filter: I just can't get this to work!

From:
Matthias Urlichs
Date:
2014-10-08 @ 18:31
Hi,

Guy Matz:
> Thank you!!  See index.html at:
> http://pastebin.com/X0wKKWEw
> 
So it seems we also need the example*html files which your template
includes, because that's where the actual filter call is which you have a
problem with, right?

Couldn't you, maybe, have thought of that yourself, before answering?

--
-- Matthias Urlichs

Re: Fwd: [flask] Adding a custom jinja2 filter: I just can't get this to work!

From:
Guy Matz
Date:
2014-10-08 @ 18:59
Sorry, maybe this is the cause of the confusion . . .  the filter is not
used in any html files or templates . . .  the web page takes in some
jinja2 code in one window, combines it with data from another window on the
same page, then renders it in a third window.  It's a way for me to verify
that my code is going to do what I expect . . .  The to_nice_yaml method
defined in http://pastebin.com/Hrnd2NpF gets called by me entering in
something like this into the "code" window:
Hello {{ people|to_nice_yaml }}

Then in my data window, I have something like:
{
  "people": [
    "john",
    "james",
    "henry"
  ]
}

the to_nice_yaml method is then called and renders the output in the output
window . . .  but it doesn't.

Again, sorry for the confusion.   I really thought I had put all necessary
information in the original post.  :-(

On Wed, Oct 8, 2014 at 2:31 PM, Matthias Urlichs <matthias@urlichs.de>
wrote:

> Hi,
>
> Guy Matz:
> > Thank you!!  See index.html at:
> > http://pastebin.com/X0wKKWEw
> >
> So it seems we also need the example*html files which your template
> includes, because that's where the actual filter call is which you have a
> problem with, right?
>
> Couldn't you, maybe, have thought of that yourself, before answering?
>
> --
> -- Matthias Urlichs
>

Re: Fwd: [flask] Adding a custom jinja2 filter: I just can't get this to work!

From:
Matthias Urlichs
Date:
2014-10-08 @ 23:01
Hi,

Guy Matz:
> Sorry, maybe this is the cause of the confusion . . .  the filter is not
> used in any html files or templates . . . 

So?

If we don't get complete code we can't reproduce the problem.

Anyway, apart from all the other obvious errors (like replacing "\n" with
"<br />" instead of simply wrapping everything with "<pre>", or trying to
convert the string "on" to a bool with int(), or dying because
"dummyvalues" does not exist) and the things the framework seems to be
doing which a real question should make explicit (like method='post' in
your <form>, or an actual type='submit' button, or giving the form a
target='/convert' attribute) …

… anyway, your main problem is that you added to_nice_json to your
Jinja environment, but didn't pass that environment to your template.

Use 
    #tpl = Template(request.form['template'])
    tpl = app.jinja_env.from_string(request.form['template'])

and the thing works.

Next time, please prepare your question better.

--
-- Matthias Urlichs

Re: Fwd: [flask] Adding a custom jinja2 filter: I just can't get this to work!

From:
Jon Ribbens
Date:
2014-10-08 @ 19:40
On Wed, Oct 08, 2014 at 02:59:01PM -0400, Guy Matz wrote:
>    the to_nice_yaml method is then called and renders the output in the
>    output window . . .  but it doesn't.
>    Again, sorry for the confusion.   I really thought I had put all
>    necessary information in the original post.  :-(

We're on your third post and we still seem to be about a million miles
away from having all the necessary information.