Contemplate demo site: main content

##welcome

Welcome

Welcome to Contemplate! This page demonstrates the embed tags most commonly used to build a site. This is the main content, which is selected based on the values specified for the "main" argument in the config/pages.txt file.

Above and below, we have a menu and a footer that come from the content/misc.html file. Since these are the same on every page of the template, their values don't need to be included into config/pages.txt … they're just hard-coded directly into the template.

To your right are two sidebars. The first is randomly selected from content/sidebars.html, so you can refresh the page to see it change. The second is optional: a conditional tag in the template only displays it if a value is given for it in config/pages.txt.

##functionality

Functionality

At its most basic, Contemplate separates design from content. But if your site involves any significant programming, it's a good idea to separate that, too, and use Contemplate to bring all three parts together. In this page, the content (text and layout) comes from content/main.html, but the functionality (querying a user database and looping through the results) comes from scripts/functionality.php.

Since the scripts folder is not part of Contemplate's basic functionality, it is listed in the READ_FILES_FROM setting in config/constants.php so that Contemplate is authorized to read it.

Once embedded into the page, the script has access to the content, so it can manipulate it and merge data into it before displaying it. This includes taking the table below (which has a single, empty row in the content file) and outputting a new row for each user.

Username Name City
$username $name $city
There are no users to display

The script can also generate individual pieces of data and merge them into the content file. For example, this date is calculated by the script: $date. If you look in the content file, you'll just see the variable $date.

##navigation

Navigation

This page demonstrates some extra functionality that Contemplate offers. The config/pages.txt file contains a block of four page definitions set off from the others; this is one of them. The Next and Previous links below contain embed tags that automatically find the next and previous pages in that block. Go ahead and try them:

##navigation_2

Navigation

Now you're on page of . Those numbers were automatically generated, of course. Keep going:

##navigation_3

Navigation

This content could come from anywhere or be organized any way you like. It's the organization of the page definitions that drives this navigation.

Also, if you look in the content file, you'll see that each of these pages refers to the navigation buttons that are stored in another content field, to avoid duplication of code. You can put embed tags into content as well as templates and Contemplate will parse it all recursively.

One more:

##navigation_4

Navigation

Notice that since we're on the last page, the Next button is disabled. We use a conditional tag to handle that. Alternatively, we could make the Next button loop back around to the first page.

##navigation_buttons

Previous Previous | Next Next  

##navigation_buttons_no_rewriting

Previous Previous | Next Next  

##extras_1

Extras 1

Contemplate includes several extra features that come in handy when building complex templates. Here are some examples:

Strip tags

Take content from another file and remove its HTML tags before displaying it:

Replace characters

Take content from another file and remove its line breaks before displaying it:

This feature supports regular expressions. For example, we could also remove all vowels:

Embed values from page definitions

We can easily embed invidual values from page definitions into our content or templates. Here are the three elements used to build this page:

Embed type:
File name:
Field name:

This allows us to write scripts, for example, that are aware of the page details. This button runs a script that displays the current page name:

Embed environment variables

Contemplate can access the server's environment variables and display those in the same way as values from page definitions. For example, here's the server's software version:

A special environment variable contains the last modified date of the newest element making up an assembled page:

Display conditional content

At the template or content level, conditional tags can help reduce duplicate code. The conditional expression can include any PHP code, or any of the values displayed above. For example, the following label tells us whether this page has one sidebar or two: one sidebartwo sidebars.

##extras_sample

This is some content used to demonstrate some features on the Extras page.

It consisted of two paragraphs spanning several lines with some colors, before those lines and colors were stripped out.

##extras_2

Extras 2

Here are some more obscure features that still come in handy for some projects:

Split page names

By default, an embed tag with a value of page[0] will display the complete name of the current page. But if you set up a site with multi-part pages, like main_home, main_contact and clients_references, you can optionally access the individual parts of those names using the SPLIT_PAGE_NAMES_ON setting in config/constants.php. This site specifies an underscore for that setting, so we can easily say that this is the (page[0]) page number (page[1]). Or we can say that the complete page name is (page[]).

Search content files

Even if you use Contemplate's HTML or XML storage options, you can still use most third-party search tools to add a search function to your site. But Contemplate also includes a built-in search function designed specifically for its flat-file options. You can try it here:

Pre-render embedded files

Normally Contemplate will assemble all the pieces of your page, then run any included PHP code all at once. This means that code in your templates can interact with code in your content files as if they are one seamless file. But in some cases you might want your code to run before it is assembled into the page … or you might want to embed scripts from other languages besides PHP. You can use the PRE_RENDER_FILES option in config/constants.php to do this. For example, PRE_RENDER_FILES is set to "cgi" for this site, so you can click here to reload the page with an embedded Perl script.

(If that didn't work, you can run the script directly to see if Perl is configured for your server.)

Store content in HTML forms

If you want to access small pieces of content, such as an employee's birth date, and can't create a more complex database back-end and user interface, an alternative is to create an HTML form inside of a content field and enter data with an HTML editor. For example, we can extract the following data from the content/forms.html file:

George's full name:
Martin's status:
Lucy's role:

 

##search_form

 

##search_form_no_rewriting

 

##search_results

Search Results