Table of contents
1.
Introduction
2.
Functions in views
3.
Blocks in Views
4.
Javascript in Views
5.
Frequently Asked Questions
5.1.
What is DAL in Python?
5.2.
What is the role of DOM?
5.3.
What is pyDAL?
5.4.
Is web2py an MVC?
5.5.
What is Falcon API?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Functions, Blocks, and Javascript in views in Web2py

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Web2py is a Python-based, accessible, and open-source web framework enabling agile development of database-driven online applications. It is a full-stack framework that includes all the elements a developer needs to create a fully functional web application.

web2py cover

In this blog, we will discuss Functions, Blocks, and Javascript in views in Web2py.

Functions in views

Consider the layout.html given below:

<html>
  <body>
    {{include}}
    <div class="sidebar">
      {{if 'sidebar' in globals():}}{{sidebar()}}{{else:}}
        my default sidebar
      {{pass}}
    </div>
  </body>
</html>


And this extending view:

{{def sidebar():}}
My new sidebar!
{{return}}
{{extend 'layout.html'}}
Hello Ninjas!


Because the function is declared before the {{extend...}} statement, it is generated before the layout.html code is performed, and it may be called anywhere inside layout.html, including before the {{include}}. Also, note that the extended view does not use the = prefix when including the function.

<html>
  <body>
    Hello Ninjas!
    <div class="sidebar">
      My new sidebar!
    </div>
  </body>
</html>

Blocks in Views

The primary method for making a view more modular is through the use of {{block ...}}s.

Consider applications built on the scaffolding app welcome, which contains a view layout.html, to learn how this works. The view default/index.html extends this view via {{extend 'layout.html'}}. The contents of layout.html predefine certain blocks with default content, which are then included in default/index.html.

Blocks in Views

You can override these default content blocks by surrounding your own content within the same block name. The placement of the block in layout.html is unchanged, but the content is.

Consider that this is layout.html:-

<html>
  <body>
    {{include}}
    <div class="sidebar">
      {{block sidebar}}
        My default sidebar (this content to be replaced)
      {{end}}
    </div>
  </body>
</html>


And this is a simple extending view default/index.html:

{{extend 'layout.html'}}
Hello Ninjas!
{{block sidebar}}
My new sidebar!
{{end}}


It gives the result seen below, where the enclosing DIV and class are from layout.html, but the overriding block in the extending view provides the content. This provides consistency across views:

<html>
  <body>
    Hello Ninjas!
    <div class="sidebar">
        My new sidebar!
    </div>
  </body>
</html>


Several helpful blocks are defined in the actual layout.html file, and you can easily add more to create the layout you want.

If a block is present in the extended view but not in the extending view, the content of the extended view is used. You can have numerous blocks. Additionally, keep in mind that blocks do not need to be declared before the {{extend ...}} statement, unlike with functions. In fact, even if defined after the extend statement, they can be used to substitute anywhere in the extended view.

To include the content of the parent within a block, use the expression {{super}}. For example, if we replace the above extending view with:

{{extend 'layout.html'}}
Hello Ninjas!
{{block sidebar}}
{{super}}
My new sidebar!
{{end}}


We get the output as:

<html>
  <body>
    Hello Ninjas!
    <div class="sidebar">
        My default sidebar!
        My new sidebar!
    </div>
  </body>
</html>

Javascript in Views

Helpers can be used within external code by enclosing them in a template and then including the template wherever it is required. For example, if some javascript code is in the file "/views/my.js," it may be incorporated in a view file:-

<script>
{{include 'my.js'}}
</script>
Man using computer


However, this will be inefficient if there are several lines of javascript code but just a few lines of dynamically created web2py material, such as helpers. A different approach would be to define the dynamically generated web2py variables in a single block of javascript within the template and then load a static javascript file that simply refers to those variables (this is how "web2py ajax.html" functions; it defines a number of JS variables that are then used by "web2py.js"). In the view file, then:-

<script>
var someVar = "{{=T('Some phrase need to be translated')}}";
var someURL = "{{=URL('default', 'myfunction')}}";
</script>
<script src="{{=URL('static', 'js/my.js')}}"></script>


or equivalently using the web2py ASSIGNJS helper:

<script>
{{=ASSIGNJS(someVar = T('Some phrase need to be translated'),
            someURL = URL('default', 'myfunction'))}};
</script>
<script src="{{=URL('static', 'js/my.js')}}"></script>

then in "my.js", someVar and someURL can be used as normal javascript variables.

Must Read Static Blocks In Java.

Frequently Asked Questions

What is DAL in Python?

Database Abstraction Layer (DAL), a feature of web2py, is an API that converts Python objects into database objects, including queries, tables, and records.

What is the role of DOM?

Programming languages can communicate with a page by interacting with the nodes and objects that the DOM uses to represent the document. A web page is a document that can be used as the HTML source or displayed in the browser window.

What is pyDAL?

An entirely Python database abstraction layer is called pyDAL. For the database back end, the pyDAL module dynamically creates the SQL for the database back end in the chosen language.

Is web2py an MVC?

The Model View Controller (MVC) pattern is one of the best practices for software engineering that web2py is intended to help web developers adhere to.

What is Falcon API?

Falcon is a lightning-quick, lightweight Python web API framework for creating reliable app backends and microservices. The framework performs admirably with both gevent/meinheld and asyncio (ASGI) (WSGI).

Conclusion

In this article, we have extensively discussed Functions, Blocks, and Javascript in views in Web2py with the help of code examples. I hope you enjoyed reading this article on Functions, Blocks, and Javascript in views in Web2py. 

If you want to learn more, check out our articles on What Is Web2Py?Why To Use Web2py?Postbacks and Internationalization in web2pyThird Party Modules In Web2pyTasks In Web2py, and  XML in Web2py.

Do upvote our blog to help other ninjas grow.

Happy Coding!

Thankyou image
Live masterclass