Turbo Gears Serving Templates - TurboGears

What is Turbo Gears Serving Templates?

An event though HTML content can be returned to the browser, for more advanced output, using template engine is constantly favored. In a complete stack mission ‘quickstarted’ via gearbox, Genshi is enabled because of the default template renderer. In a minimal utility, but Genshi (or every other template engine, together with jinja) desires to be established and enabled. Genshi template engine permits to write down templates in natural XHTML and validates them to stumble on problems at compile time and prevent serving damaged pages.

Templates are noted by way of the use of a dotted notation. In our key task, a templates directory is provided to keep template net pages. For this reason, sample.html could be referred as hi there.templates.pattern (extension not noted). TurboGears renders this template via an expose decorator to link controller technique to it with the aid of tg.render_template() function.

The exposed controller characteristic returns a Python dictionary object. This dictionary object is, in turn, surpassed directly to the related template. Placeholders in the template are filled with dictionary values.

initially, let us display an internet page with plain HTML script. The exposed controller returns a null dictionary object as we do no longer intend to send any data to be parsed by the HTML script.

How to Create a Sample HTML

Our sample.html is given below. Ensure that it is stored in templates directory of project.

Add sample() function in root.py and expose sample.html through it.

The following result will be displayed in the browser when a URL http://localhost:8080/sample is entered after starting the web server.


As stated above, a dictionary object is sent as group of parameters to a Genshi template. This template contains ‘place holders’, which are dynamically filled with parameters received from the controller.

Let us change the sample() function to send a dictionary object to the sample template.

Create sample.html in templates folder (templatessample.html)

In the above HTML code, ${person} is the placeholder. Enter http://localhost:8080/sample?name=MVL as URL in the browser. This URL is mapped to sample() method in our root controller. It returns a dictionary object. This is picked by linked template page sample.html in templates directory. The ${person} is then substituted by MVL in the web page.

It is also possible to access the HTML form data in a controller function. HTML form uses to send form data.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

TurboGears Topics