Lab 99 Web Design

Skip the navigation

Textpattern : Articles and Sections

Textpattern logo

To understand how to use Textpattern as a content management system, we need to understand how articles and sections work together. Let’s begin by examining a very basic website.

In the Pages and Sections tutorial, we learned how to create a static website using Textpattern. Let’s assume that this website consists of three HTML pages:

  • a Home page;
  • an About Us page;
  • and a Contact page.

This is what you do:

  1. Create a Textpattern page for each of these HTML pages.
  2. Create a Textpattern section for each Textpattern page, and assign each page to the appropriate section.

Incidentally, you don’t have to create the page before you create the section, but it is quicker to do it in this order.

Once you adjust the internal links so that they follow the Textpattern formula, you will have a fully functioning website.

A Textpattern Page is a Template

Each Textpattern page contains the HTML code, and each Textpattern section anchors the relevant Textpattern page to the database. To place the actual material of the website (the text, images, and so on) in the correct Textpattern page, you have two options:

To take advantage of the power of the Textpattern database, you will need to follow the second option. This treats the Textpattern page not as a static entity but as a template which can accommodate an unlimited variety of material.

A Simple Template

Let’s imagine that you want to expand your three-page static website to include a Latest News page, which will have articles added to it over time. Again, create a new Textpattern page, create a new section, and assign the page to the section.

You will probably also want to adjust the navigation menu on each page. Of course, if you have placed the navigation menu in a Textpattern miscellaneous form, you only have to adjust it once.

Templates and Articles

You can use the new page as a template to display a series of articles containing your latest news. Just create a new article, assign it to the Latest News section, and it will appear on the correct HTML page. Create another, and now both articles will be displayed.

The Textpattern page remains the same, but the HTML page changes. The Textpattern page functions as a template for the HTML page.

Templates and Forms

How you choose to display this series of articles is up to you. Textpattern has a variety of article tags that allow you to select various elements of an article. Simply create an article form that contains your choice of Textpattern tags and HTML tags.

Returning to a previous example, you could display the information contained within the title and body fields of one or more articles by creating a form that includes this code:
<h1><txp:title /></h1>
<txp:body />

Or you could display the title, the body and the date when each article was posted:
<h1><txp:title /></h1>
<txp:body />
<p class="date"><txp:posted /></p>

There are many Textpattern tags and HTML tags to choose from, so the possibilities are endless.

Templates and Pages

Give the form a name and insert it into the Textpattern page at the appropriate place:
<txp:article form="whatever" />

You will have noticed that this form, unlike the one we mentioned in the previous tutorial, does not specify the ID number of the article. If you do not specify which article is to be displayed, Textpattern’s default behaviour is to display all the articles that have been assigned to that particular section.

You can limit the number of articles displayed, like this:
<txp:article limit="5" form="whatever" />

For details of how to use the many options available within the article tag, see https://docs.textpattern.com/tags/article.

Many Articles = One HTML Page

No matter how many Textpattern articles you choose to display on your Latest News HTML page, only one Latest News page will appear on the website.

Many HTML Pages = One Textpattern Page

But what if you want to copy the standard blog format, and use the Latest News HTML page as a landing page, displaying a list of Textpattern articles, each of which is displayed in full on its own HTML page? This is exactly what Textpattern is good for.

To do this, we need to make the Textpattern page a little more complex.

A Complex Template

Textpattern allows you to use the same Textpattern page as a template for both types of HTML page:

This is achieved by inserting into the Textpattern page two pairs of conditional tags, one pair for each type of HTML page:
<txp:if_article_list>
   <!-- code for the landing page goes here -->
</txp:if_article_list>
<txp:if_individual_article>
   <!-- code for the individual article page here -->
</txp:if_individual_article>

Alternatively, you could do it like this, using another handy Textpattern tag:
<txp:if_article_list>
   <!-- code for the landing page goes here -->
<txp:else />
   <!-- code for the individual article page here -->
</txp:if_article_list>

You could change the order in which the conditional statements appear, like this:
<txp:if_individual_article>
   <!-- code for the individual article page here -->
</txp:if_individual_article>
<txp:if_article_list>
   <!-- code for the landing page goes here -->
</txp:if_article_list>

Or you could do this:
<txp:if_individual_article>
   <!-- code for the individual article page here -->
<txp:else />
   <!-- code for the landing page goes here -->
</txp:if_individual_article>

All four options perform exactly the same tasks. Use whichever one you find the easiest to follow.

Landing Page or Individual Article?

When someone visits any Textpattern page, the Textpattern software will read the code on that page and enact the first instruction that applies. The default setting is that the visitor will be taken to an HTML landing page, even if they have activated a link to an individual article.

If you want a separate HTML page to be generated for the individual article, it is necessary to specify this on the Textpattern page using the conditional tags mentioned above. Only then will the visitor be taken to that HTML page.

Landing Page URLs

The landing page will reflect the name of the section. This is how it might look with clean URLs:
http://www.mywebsite.com/section-name/

And this is the messy URL version:
http://www.mywebsite.com/?s=section-name/

Over-Riding the Default Settings

Again, Textpattern’s default setting is to use each Textpattern page as a landing page, and to list every article that has been assigned to that section.

So you only need to use the conditional tags if you want to over-ride the default setting and generate separate HTML pages to display individual articles.

Article and Article_Custom Tags

You may also want to over-ride the default setting in another way, so that a Textpattern page displays part or all of an article that has been assigned to a different section. To do so, this is the tag to use:
<txp:article_custom />

You will need to specify which section the article comes from, and you may need to specify the article’s ID number and the form that is to be used:
<txp:article_custom section="section-name" form="whatever" />

Whether you use an txp:article or txp:article_custom tag will depend on how the Textpattern page in question relates to the particular article you want to display on that page:

For more information about these two very useful tags, see:

Article Forms

In the previous tutorial, we mentioned that it is necessary to use a Textpattern form to specify which parts of an article should be used in a particular context.

That is true for landing pages, but not for individual-article HTML pages. Within the code for the individual-article page, you may insert tags for parts of an article, like this:
<txp:if_individual_article>
   <h1><txp:title /></h1>
   <txp:body />
</txp:if_individual_article>

In practice, however, the code will usually be easier to follow if it is contained within a Textpattern article form.

This, on the other hand, will not work:
<txp:if_article_list>
   <h1><txp:title /></h1>
   <txp:body />
</txp:if_article_list>

On a landing page (i.e. an article_list page), you must use a Textpattern article form to specify which parts of an article are to be used.

One Template = Many Pages

Textpattern is able to generate both HTML landing pages and HTML individual-article pages from the same template:

With these tags you can create an infinite variety of complex, dynamic websites.

In the next tutorial, we will see how to create one.

Next …

Continue with the next article: Textpattern : Example Website.

[This tutorial is part 5 of a series intended to introduce the Textpattern content management system to web designers who have no knowledge of PHP or databases.]