Textpattern: Context in depth Page 2 of 8

Behind the curtain

Summary:

A not-too-technical glimpse at Txp’s inner workings

Warning: Gross simplifications ahead.

Terms introduced on this page:

Context
Conditions (i.e., state) affecting the output of Txp templates and tags.
Global context
Page context
Context provided by the URL.
Local context
Tag context
Context provided by Txp tags.
Context-setting tag
Tag that establishes a local context
Context-sensitive
Responsive to global or local context
Static tag
A Txp tag that is not context-sensitive

A traditional, “static”, website consists mainly of HTML files on a web server, each file matching a URL. If a web browser requests example.com/about/staff.html, the example.com server looks for a file named “staff.html” inside a top-level directory named “about”. If that file exists, the server simply sends its contents back to the web browser. If not, it sends the familiar “File Not Found” error message.

A server hosting a Txp site works the same way, with one essential difference: if the browser requests a missing file, the server doesn’t return an error message. Instead, it passes the request along to Txp, leaving Txp to decide what to send back to the browser.

Txp is a script (i.e. program, or application) for manufacturing web pages. Send Txp a URL and it replies with a stream of HTML. To the browser, this looks exactly like the server output from a real HTML file. But there’s no such file: Txp generates the HTML stream on the fly, every time it receives a URL. (If you’re wondering why one would bother using a script to generate HTML, instead of simply putting the HTML files on the server, see What is Textpattern CMS, and why should I use it?.)

Txp is a typical CMS in that it stores its content, templates, and other design elements in a database. A Txp template is a mix of standard HTML and Txp tags, special placeholders for performing Txp functions (most of which involve pulling content from the database). When Txp receives a URL, it selects a template from the database and streams it back to browser, after replacing any Txp tags with their corresponding function output. The end result, from the browser point of view, is a normal HTML page.

To Txp, a URL represents not a file request, but a set of conditions. Txp makes many decisions while generating an HTML page, and throughout the process Txp repeatedly refers back to those base conditions. That is, the URL provides the context in which Txp works.

Developer note: the term “global” is used throughout this article; unless otherwise noted it is used in its general sense, not the PHP sense. I.e., it refers to the state provided by the URL, not the state of any particular Txp global variable at a given point.

The URL-provided context is a general state that applies throughout the page. In addition to this global context (or page context), some Txp tags create their own local context (or tag context), potentially overriding some aspects of global context — I will call such tags context setting. Some Txp tags base their output on the context, whether global or local; this class of tags is called context sensitive. For non-context-sensitive tags I will use the term static tags. Nearly all Txp tags are either context-setting or context-sensitive; some are both. Finally, there are a few Txp tags that neither affect context nor respond to it.

We can think of a Txp site not as a collection of pages, but as a set of global contexts. The job of the Txp architect is to decide which contexts to offer the user, to create navigation elements that allow the user to choose among those contexts, and to construct templates and forms that deliver content suitable to each available context. The last step entails selecting, arranging, and configuring Txp tags to respond appropriately to global context while interacting through local contexts. This system of layered contexts is Txp’s killer feature, the reason it is such a powerful and flexible system.

In the we’ll start applying examples to these abstract concepts.

Posted 2011-01-07 (last modified 2011-01-17)