Build views in Plone only with Python (without TAL, JQuery)?

Hello dear Plone Community,

I have a beginner question :slight_smile:

There is a way to write a view in Plone only with Python so without TAL Expressions. But can I also fill a table dynamically (without using JQuery, Ajax) and display it. Because I have a table whose content changes occasionally and I want this to be displayed dynamically in the view of Plone.

Thanks in advance.

Greetings

You need at least a template using the main_template where you can fill any arbitraty HTML generated through a browser view method…That’s where you put your own Python code generating your own HTML in whatever way you need or want.

I am not sure if I understand what you are trying to do.

It is possible to create the views in different ways, but I doubt that there is a reason to do it differently than the standard way:

The best way is to use plonecli to make the view, but you can also do it manually. You should then have:

1 template.pt file
1 template.py file

The .pt file should be something like this (note: you dont need to use the 'tal syntax anymore')

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:metal="http://xml.zope.org/namespaces/metal"
  xmlns:tal="http://xml.zope.org/namespaces/tal"
  xmlns:i18n="http://xml.zope.org/namespaces/i18n"
  i18n:domain="acentoweb.mediaflows"
  metal:use-macro="context/main_template/macros/master">
  <body>
 <metal:block fill-slot="content-core">
    <h1>${context/somefield}</h1>
    <div>${ view/some_calculation}</div>

</metal:block>
</body>
</html>

the content of <h1> will be the value of your content types field 'some field' while the content of ${view/some_calculation} will be whatever is returned from your view.py, which would be something like:

from Products.Five.browser import BrowserView

class PersonView(BrowserView):
    def some_calculation(self):
           build / calculate your table here
           return table

It is not often you need to use so much TAL, you can do things like (instead of tal:attributes etc)

<a href="${context/somefield}" class="${view/get_my_class}">

Hi, I would like to know if you can create a view without using tal expression etc. like with swing in the java world.

You can implement the __call__ method in the BrowserView derived classes and just return a string, so you can keep the view completely in Python code.

1 Like

${context/some_calculation} can return 'all the body html'. I am not sure what the benefits of doing everything in python would be, but it is possibl as tiberiuichim said. I have only done that for 'download views', 'javascript/css' helper views so I have no examples.

Is there a reason why you want to do it like this? Is it not better to keep the logic and the html apart?

How can I pass a json object to the view ?

Send JSON via a BrowserView:

from Products.Five.browser import BrowserView
import json

class MyView(BrowserView):
    
    def __call__(self):
        state = {"message": "your message"}
        self.request.response.setHeader("Content-Type", "application/json")
        self.request.response.setStatus(200, state["message"])        
        return json.dumps(state)
1 Like

I found an example how to make forms completely in Python without writing a line of html thought you could do that with aklen Veiws so

Mario,

Views are not static. Every time you view a page it is generated from scratch, so you can make a view using a template and render dynamic content into a table.

I'm still a bit confused on what you want to accomplish or why you don't want to use a template?

Because I heard about something like that and I wanted to test it^^