Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2009-09-28 04:24:18
- chr1spy
- Member
- From: United States
- Registered: 2009-07-21
- Posts: 11
How to set up more than two templates in a single page
I understand how to output two different templates using if and else statements, but how do you set things up so that a page displays in three or four different ways depending upon the content called up?
In my case what I’d like to do is have a page that displays a single article if a link to the article is selected, displays a list of articles, either by category or by date, if a link to the category or date (month and year) is selected, shows a list of related articles when a search is made, or if none of these is the case defaults to a template that displays the full content of the most recent article, followed by excerpts of past articles.
Thanks,
Chris
Offline
Re: How to set up more than two templates in a single page
i dont see why nested if’s or even multiple if statements one after the other wouldn’t work.
txp:if_individual_article for single article view
txp:if_category for displaying whatever if user clicks on a category
txp:if_search if the user searches, then do whatever
txp:_if_article_list
Offline
Re: How to set up more than two templates in a single page
Hi Chris and welcome,
You more or less have it already: the standard txp:article tag that’s used to output articles is already context sensitive – that means if the link in the url is to an individual article it displays an individual article and if a link is to a standard section or to a category page it will output a list of articles in the respective section or category. To tell it how to display the article you set up a form for how an individual article should be displayed (enter this is the form="my-single-article"
attribute) and a form for how an article should appear in a list (enter this is the listform="my-list-article"
attribute). If I remember rightly you can use month details in the url to restrict to a certain time.
If you want special cases for a particular sections or combinations of criteria you can use conditional tags (the if … else’s) along with txp:article or txp:article_custom (not context-sensitive but more finely controllable) to control things more precisely.
Search works exactly the same way by having an if_search section in your page template. See the overview of search tags in the docs. Again you can control how each set of results appear with a form.
For special cases such as different sorting methods, you can use a plugin such as adi_gps to take a value from the link (e.g. mysection/?order=date
) and use this to change the sort='...'
attribute for txp:article or custom.
TXP Builders – finely-crafted code, design and txp
Offline
Re: How to set up more than two templates in a single page
chr1spy wrote:
In my case what I’d like to do is have a page that displays a single article if a link to the article is selected, displays a list of articles, either by category or by date, if a link to the category or date (month and year) is selected, shows a list of related articles when a search is made, or if none of these is the case defaults to a template that displays the full content of the most recent article, followed by excerpts of past articles.
As jakob says, the article
tag is context-sensitive (i.e., its behavior changes based on the page URI), so you get your first three requests handled automatically just by having the tag in your page template. To fine tune that use conditionals, as you already know and as jakob and Steve have reinforced. Are you working from the page templates that come installed, or have you started afresh?
You have a choice as to where to put your conditionals: in the page template or in the article form, or a mix—it’s a question of taste and convenience. An article form is a sort of mini-template that gets applied to each article received from an article
or article_custom
tag. As a general approach most article forms will include if_article_list
and/or if_individual_article
conditionals, while conditionals such as if_search
go in the page template. Note that article
uses the article form “search_results” on a search results page (i.e., http://mysite.com/?q=search_term
).
Your final request is easily solved using “ if_first_article
in the article form. A simple example article form:
<txp:if_article_list> <h2><txp:permlink><txp:title /></txp:permlink></h2> <txp:if_first_article> <txp:body /> <txp:else /> <txp:excerpt /> </txp:if_first_article> <txp:else /> <h1><txp:title /></h1> <txp:body /> </txp:if_article_list>
Code is topiary
Offline
#5 2009-09-28 16:55:36
- chr1spy
- Member
- From: United States
- Registered: 2009-07-21
- Posts: 11
Re: How to set up more than two templates in a single page
Thanks everyone. Your feedback has been helpful and provided a more elegant solution than I’d thought of (thanks jsoo for suggesting the conditional statements in the article form). But I don’t think it will quite take care of what I want. Let me explain.
The site I’m designing has a blog section. As the default landing page, I’d like the blog section to show the complete most recent post, followed by excerpts of older posts. If the user selects an article, I would like it to show the individual article. If the user selects a group of articles, by category or date, in other words a list of articles, I want the page to show a list of article excerpts. jsoo’s article form solution takes care of the last two scenarios. The problem I’m having is getting the default or landing blog page to show up with the full text of the most recent article followed by excerpts of all following articles. Since the landing page is a list of articles, I have only been able to get it to show a list of excerpts, or if I get it to show the full text of the most recent article, then all article lists display the full text of the most recent article. I haven’t been able to figure out how to have both solutions – one as the default landing page, and another for all other article lists.
Offline
Re: How to set up more than two templates in a single page
Jeff’s solution should actually show the full text of the first article and the excerpt for all the others on an article list page and the full article on an individual article page. The form he shows is an article form (e.g. one you use in the form="..."
attribute with txp:article). Putting it in the article form as Jeff suggests is quite elegant as it’s control by one txp:article tag.
Another way of achieving this is in your page template using the limit and offset attributes of txp:article which allow you to split a list in two parts by using two txp:article tags, the first with limit="1"
to show just one article and the second with an offset="1"
to continue from article two onwards.
TXP Builders – finely-crafted code, design and txp
Offline
Re: How to set up more than two templates in a single page
Following on from Jakob (and Jeff’s) post, see this Nora Brown TXP Tip which explains how to format the first item in an article differently from the balance of them. It gives you blow by blow instructions.
Offline
Re: How to set up more than two templates in a single page
chr1spy wrote:
I haven’t been able to figure out how to have both solutions – one as the default landing page, and another for all other article lists.
You could do it with core Txp tags but for one lack: there’s no if_date
conditional available. But:
allows this variation on the article form I showed earlier:
<txp:if_article_list> <h2><txp:permlink><txp:title /></txp:permlink></h2> <txp:if_first_article> <txp:soo_if_frontpage> <txp:body /> <txp:else /> <txp:excerpt /> </txp:soo_if_frontpage> <txp:else /> <txp:excerpt /> </txp:if_first_article> <txp:else /> <h1><txp:title /></h1> <txp:body /> </txp:if_article_list>
Code is topiary
Offline
#9 2009-09-29 01:35:17
- chr1spy
- Member
- From: United States
- Registered: 2009-07-21
- Posts: 11
Re: How to set up more than two templates in a single page
I’d like to thank everyone for their help. I took jsoo’s code and modified it slightly:
<txp:if_article_list> <txp:if_first_article> <txp:soo_if_frontpage section="blog"> <txp:article form="article_blog_full" limit="1" /> <txp:else /> <txp:article form="article_blog_excerpt" limit="1" /> </txp:soo_if_frontpage> <txp:else /> <txp:article form="article_blog_excerpt" offset="1" limit="7" /> </txp:if_first_article> <txp:else /> <txp:article form="article_blog_full" /> </txp:if_article_list>
and linked to this in my page with:
<txp:if_search> <txp:article form="article_blog_list" limit="12" /> <txp:else /> <txp:article form="article_blog" /> </txp:if_search>
Everything works. Again, thanks for all the help and patience with a newbie.
Last edited by chr1spy (2009-09-29 01:40:15)
Offline
Re: How to set up more than two templates in a single page
With Textile, always add a space after the dot following a tag name. That is,
bc. <txp:foo />
instead of
bc.<txp:foo />
Glad you’ve got it working. As you’ve already found, there are usually various ways of piling up the conditionals to get the desired result.
Code is topiary
Offline