Table of contents
1.
Introduction
2.
Page Layout
3.
Frequently Asked Questions
3.1.
What is DAL in Python?
3.2.
What is SQLite used for?
3.3.
What is pyDAL?
3.4.
What is pylon framework?
3.5.
What is Falcon API?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

What is Page Layout 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 what is page layout in Web2py with the help of examples.

Page Layout

Views can extend and incorporate other views in a tree-like structure. Consider the view index.html, which extends layout.html and contains body.html. At the same time, layout.html may contain header.html and footer.html.

Page layout in web2py

A layout view is the root of the tree. Using the web2py administration interface, you may change it just like any other HTML template file. The file name layout.html is just a convention.

Here is a simple page that contains both the page.html view and the layout.html view:

{{extend 'layout.html'}}
<h1>Hello Ninjas!</h1>
{{include 'page.html'}}


There must be a {{include}} directive in the extended layout file, something like:

<html>
  <head>
    <title>Page Title</title>
  </head>
  <body>
    {{include}}
  </body>
</html>


When a view is called, the extended (layout) view is loaded, and the calling view takes the place of the layout's {{include}} directive. Recursive processing continues up until all include and extend directives have been processed. The template that results is then converted to Python code. Note that this Python code, not the original view files themselves, is what is compiled when an application is converted to bytecode. Therefore, the bytecode compiled version of a particular view is a single .pyc file that contains the Python code for the full tree of extended and included views in addition to the original view file.

Man using computer

Any content or code that comes before the {{extend ...}} directive will be added (and consequently executed) prior to the start of the extended view's content or code. Although this isn't frequently used to put actual HTML text before the content of the extended view, it might be helpful to declare variables or functions to which you want the extended view to have access. Consider the following view index.html:-

{{sidebar_enabled=True}}
{{extend 'layout.html'}}
<h1>Home Page</h1>


and an excerpt from layout.html:

{{if sidebar_enabled:}}
    <div id="sidebar">
        Sidebar Content
    </div>
{{pass}}


The sidebar_enabled assignment from index.html is placed before the extend, which causes that line to be added before the start of layout.html, making sidebar_enabled accessible throughout the layout.html code.

It's also important to note that the variables produced by the controller function are accessible not just in the primary view of the function but also in all of its extended and included views.

A Python variable may be used as the parameter for an extend or include (i.e., the name of the expanded or included view). This introduces a restriction, though—bytecode compilation is impossible for views that use variables to extend or include statements. The specific extended and included views must be known at compile time because, as was previously said, bytecode-compiled views include the full tree of extended and included views. If the view names are variables, this is not feasible (whose values are not determined until run time). Using variables in extend and include should typically be avoided because bytecode compiling views can offer a large speed boost.

In some scenarios, using a conventional {{include ...}} directive inside of an if...else block might be an alternative to utilizing a variable in an include.

{{if some_condition:}}
{{include 'this_view.html'}}
{{else:}}
{{include 'that_view.html'}}
{{pass}}


Since there aren't any variables involved, the above code does not cause any compilation issues for bytecode. Though the Python code for both "this view.html" and "that view.html" will be included in the bytecode-produced view, only one of those views will actually be run, depending on the value of some condition.

Just keep in mind that {{extend ...}} directives cannot be used inside of if...else blocks; this only applies to include.

Layouts are used to encapsulate page commonalities (headers, footers, and menus), and while they are not required, they will make your application easier to create and maintain. We recommend developing layouts that use the variables listed below, which may be adjusted in the controller. Your layouts will become more interchangeable if you use these widely used variables:-

response.title
response.subtitle
response.meta.author
response.meta.keywords
response.meta.description
response.flash
response.menu
response.files

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 SQLite used for?

Desktop applications like version control systems, financial analysis tools, media categorization and editing suites, CAD packages, record-keeping programs, and others frequently use SQLite as the on-disk file format.

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.

What is pylon framework?

Python-based Pylons Framework is an open-source Web application framework. The Web Server Gateway Interface standard is heavily utilized to encourage reusability and divide functionality into independent modules.

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 Page Layout in web2py with the help of code examples. I hope you enjoyed reading this article on Page Layout 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