Creating Your First Page

by Mike Takahashi last modified Feb 04, 2009 02:34 AM
In this example, we will be displaying data within a page template from our MySQL database.

This already assumes that there is a simple table in your MySQL database that exists called users, and that it has the following data:

Table Name: users

id    first_name  last_name
----------------------------
1 Rivers Cuomo
2 Eddie Vedder
3 Thom Yorke

First, we’ll need to add a new Z SQL Method within the same folder as our Z MySQL database connection called select_all_users, which will select all of the names from the table users. To make sure your query works correctly, you should choose “Add and Test” to test it.

select_all_users.gif



Page Template (ZPT)

Next, we’ll need to add a new page template from the drop down menu located at the top right corner of the ZMI.



pagetemplate.gif


Under the id field of the page template enter listnames.

Insert the following code:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en"
metal:use-macro="here/main_template/macros/master"
i18n:domain="plone">

<body>

<div metal:fill-slot="main">

<h1>My List of Names</h1>

<ul>
<li tal:repeat="records context/select_all_users">
<span tal:replace="records/first_name">First Name</span>&nbsp;<span tal:replace="records/last_name">Last Name</span>
</li>
</ul>

</div>

</body>
</html>

*Note – I’ve included a metal tag here so that this page will be displayed within the main content area of a Plone site.


By using the tal:repeat command, we loop through each record result of the select_all_users query and assign it to the variable records. Since our table contains 3 records, it will loop through 3 times. Each time it loops through, a call is being made for to retrieve the values of first_name and last_name and is displayed via the tal:replace command.

The rendered HTML will look like this:

...
...

<h1>My List of Names</h1>

<ul>
<li>Rivers&nbsp;Cuomo</li>
<li>Eddie&nbsp;Vedder</li>
<li>Thom&nbsp;Yorke</li>
</ul>

...
...

A few notes to consider:

Since this is a page template, it will not show up within Plone’s “Content” tab even though it exists in a folder in the ZMI. You will need to access it via a hard link. For example, if you created a folder called myfolder and placed listnames in that folder, you would be able to access it via /myfolder/listnames.