You are here: Home » Web Advice » Textpattern : Articles and Sections

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.

We saw in Pages and Sections 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.

Adjust the internal links so that they follow the Textpattern formula, and 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:

  • Insert the material directly into the appropriate Textpattern page, as you would with a static HTML page;
  • Place the material in a Textpattern article, and insert a Textpattern form into the Textpattern page, specifying which parts of the article are to be used.

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. Again, create a new page, create a 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 a form that contains your choice of Textpattern tags and HTML tags.

For example, you could display the title and body of each article by creating a form that includes this code:
<h2><txp:title /></h2>
<txp:body />

Or you could display the title, the body and the date when each article was posted:
<h3><txp:title /></h3>
<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 earlier, 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" />

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:

  • landing pages, containing lists of articles;
  • and separate pages for each individual article.

This is achieved by using 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 goes here -->
</txp:if_individual_article>

Alternatively, you could do it like this:
<txp:if_article_list>
  <!-- code for the landing page goes here -->
<txp:else />
  <!-- code for the individual article page goes 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 goes 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 goes 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.

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

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

Over–Riding the Default Settings

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 these 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 so that a Textpattern page displays part of an article that has been assigned to a different section. To do this, use this tag:
<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 a Textpattern article or article_custom tag will depend on how the page in question relates to the particular article you want to display on that page:

  • <txp:article />
    The txp:article tag will display articles from the section to which the Textpattern page has been assigned.
  • <txp:article_custom />
    The txp:article_custom tag must be used if you want to display articles from sections other than the one to which the Textpattern page has been assigned.

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 pages. Within the code for the individual article page, you may insert tags for parts of an article, like this:
<txp:if_individual_article>
  <h2><txp:title /></h2>
  <txp:body />
</txp:if_individual_article>

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

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

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

One Template = Many Pages

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

  • <txp:if_article_list>
    </txp:if_article_list>
    The txp:if_article_list pair of tags allows you to use parts of a Textpattern article to create an HTML landing page with links to individual article pages;
  • <txp:if_individual_article>
    </txp:if_individual_article>
    The txp:if_individual_article pair of tags allows you to display parts of a Textpattern article on a separate, dedicated HTML page.

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 by Jeremy Bojczuk intended to introduce the Textpattern content management system to web designers who have no knowledge of PHP or databases.]

Textpattern Links

⇧ Top

This website uses cookies. Find out more.