librelist archives

« back to archive

How to access class attribute of element inside jinja2 block?

How to access class attribute of element inside jinja2 block?

From:
Viral Sangani
Date:
2012-08-03 @ 16:35
Hi All,

In a web page we want to access the class attribute of the element inside a
jinja2 block.

I tried following assignment block to fetch the class name using jquery and
passing it to jinja variable which didn't work.

{% set eleClassName = $('#ID_OF_ELEMENT').attr('class') %}
We are just starting out with this project and evaluating if jinja2 will
have good interoperability with Java Script and allow using variables from
and to Java script.

Highly appreciate your help in advance!

regards,
Viral

Re: [flask] How to access class attribute of element inside jinja2 block?

From:
Simon Sapin
Date:
2012-08-03 @ 18:22
Le 03/08/2012 18:35, Viral Sangani a écrit :
> evaluating if jinja2 will have good interoperability with Java Script
> and allow using variables from and to Java script.

No.

Jinja typically runs on the server side, and JavaScript on the client 
side. The former can give stuff to the latter:

     var stuff = {{ stuff|tojson }};

but not the other way around. At least not without AJAX, but that means 
several separated requests that you have to handle explicitly.

Here is the typical workflow:

1. The browser sends an HTTP request to the server
2. Flask handles the request, and Jinja2 generates HTML
3. The response is sent back to the browser
4. The browser parses the HTML into a DOM, renders it, executes any 
JavaScript
5. JavaScript code in the browser can make other requests to the server. 
Steps 1 to 3 are repeated separately for *these* requests, but instead 
of 4 the JavaScript code does whatever it wants with the response, such 
as updating the live DOM.

-- 
Simon Sapin

Re: [flask] How to access class attribute of element inside jinja2 block?

From:
Viral Sangani
Date:
2012-08-05 @ 08:02
Thanks much Simon!

On Fri, Aug 3, 2012 at 11:22 AM, Simon Sapin <simon.sapin@exyr.org> wrote:

> Le 03/08/2012 18:35, Viral Sangani a écrit :
> > evaluating if jinja2 will have good interoperability with Java Script
> > and allow using variables from and to Java script.
>
> No.
>
> Jinja typically runs on the server side, and JavaScript on the client
> side. The former can give stuff to the latter:
>
>      var stuff = {{ stuff|tojson }};
>
> but not the other way around. At least not without AJAX, but that means
> several separated requests that you have to handle explicitly.
>
> Here is the typical workflow:
>
> 1. The browser sends an HTTP request to the server
> 2. Flask handles the request, and Jinja2 generates HTML
> 3. The response is sent back to the browser
> 4. The browser parses the HTML into a DOM, renders it, executes any
> JavaScript
> 5. JavaScript code in the browser can make other requests to the server.
> Steps 1 to 3 are repeated separately for *these* requests, but instead
> of 4 the JavaScript code does whatever it wants with the response, such
> as updating the live DOM.
>
> --
> Simon Sapin
>