Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2005-11-04 19:20:34
- aesop1
- Archived Plugin Author
- Registered: 2004-09-19
- Posts: 142
TXP Templating Tip
When I started using TXP, I found myself constantly duplicating HTML templates for the various pages (and sections) on my site. While I found reuse in forms for common elements like headers, footers, navigation, etc., I still had large portions of HTML that I wished I didn’t have to duplicate in the TXP page templates.
Having experimented with various PHP templating systems, I wondered if I couldn’t mimic the use of a templating system like Smarty to eliminate some of the redundancy. So, I came up with this general process:
1. Create your HTML template and save it as a misc. form rather than a page. You can still include other forms within your page template form by using txp:output_form. Insert your commonly used headers, footers within your page template form. You can also still use other common “page template tags” like txp:page_title within your template form. For purposes of this example, let’s say I named this page template form, “common_template.”
Here is a simplified example:
<code>
<html>
<head>
<title><txp:page_title /></title>
<link href=”/css/master.css” rel=“stylesheet” type=“text/css” media=“screen” />
<txp:output_form form=“meta” />
</head>
<body id=”<txp:s />”>
<div id=“main_nav”>
<txp:output_form form=“headernav” /></div>
<div id=“main_content”>
<?= $maincontent ?>
</div>
<div id=“sidenav”><h3><txp:section title=“1” /></h3>
<?= $sidemenu ?>
</div>
<div id=“footer”><p><txp:output_form form=“footer” /></p></div></body>
</html>
</code>
2. Create your page using the usual “Presentation > Pages” page template feature in TXP, but your page template should look something like this:
<code>
<?php
$maincontent = ‘<txp:article limit=1 />’;
$sidemenu = ‘<ul class=“Menu”>
<txp:article_custom section=“about-us” sortdir=“asc” form=“sectionlist” sortby=“custom_1” />
</ul>’;
?>
<txp:output_form form=“common_template” />
</code>
As you can see, PHP variables are set via the page template, and once those variables are set, the “common_template” form is called at the end and the PHP variable calls are filled in. The common_template can thus be reused for several different pages. This can also result in better content vs. presentation separation and make your page templates easier to read.
There are some caveats to be aware of, notably that you should take care to single quote your variable contents in your page template and look out for string quoting collisions. I had some hesitation about implementing this (worried about PHP within forms within PHP, etc.) but so far it has worked out great.
Last edited by aesop1 (2005-11-04 20:27:06)
Offline
#2 2005-11-04 19:45:16
- rsilletti
- Moderator
- From: Spokane WA
- Registered: 2004-04-28
- Posts: 707
Re: TXP Templating Tip
Two questions:
1. Are you using standard php open and close tags with this, or TXP`s <code><txp:php></txp:php></code> tags?
2. Which version of TXP are you using?
I`m not sure exactly when <code><txp:php></txp:php></code> started being used, but I think using standard php open and close tags is going to be version sensitive; or at least optional(I`d have to sit down and kick at it a little to be certain).
<a href=“http://textpattern.net/wiki/index.php?title=Txp:php”>Txp:php</a>
Offline
#3 2005-11-04 20:10:42
- aesop1
- Archived Plugin Author
- Registered: 2004-09-19
- Posts: 142
Re: TXP Templating Tip
I am using the latest version of TXP (4.0.2) and I have also used this method in versions going back to rc3. Opening and closing PHP tags are used as you see them in the examples.
You bring up a good point about the use of <code>txp:php</code> tags: Is the use of standard PHP tags within TXP truly deprecated? Unsafe? I was aware of the txp:php tag, but just haven’t tried it. I believe I implemented this before the tags were available. And if it ain’t broken . . .
Please do kick the tires — I am always happy to be informed when I am tap dancing blindfolded near a cliff’s edge. ;)
Last edited by aesop1 (2005-11-05 01:22:53)
Offline
Re: TXP Templating Tip
Using php-style delimiters for code blocks is indeed deprecated, but wasn’t taken completely out. However I always recommend against using them, because they are evaluated counterintuitively with respect to all other tags, which you can see when you read the textpattern()-function. First the calls to parse() are made, which evaluates all txp:-style tags/functions. After everything else is done, evalString() is called which executes the php-style delimeted blocks. This lead to trouble for lots of uses, which is why the txp-style php delimeters were introduced in the first place. It looks like the whole premise off this working resides on side-effects, implementation details and pure luck, and I wouldn’t be suprised if it stopped working one day because of some internal changes.
So I would advise against mingling templating up with php like that. It seems backwards and harder readable to me. Alternatively you can use one default page-template for all sections, and then conditionals for the changing parts. It will make it easier to maintain and less errorprone. Having said that – use whatever floats your boat. ;)
Offline
#5 2005-11-04 22:38:37
- zem
- Developer Emeritus
- From: Melbourne, Australia
- Registered: 2004-04-08
- Posts: 2,579
Re: TXP Templating Tip
For variables to persist across blocks of code, you’ll need to explicitly make them global:
<txp:php>
global $var;
$var = 'something';
</txp:php>
then later:
<txp:php>
global $var;
echo $var;
</txp:php>
The reason the old method worked was more luck than anything else – it deferred all PHP execution till the last step, and then ran everything together as if it was part of the same block (which caused more problems than it solved).
Alex
Offline
#6 2005-11-05 01:17:12
- aesop1
- Archived Plugin Author
- Registered: 2004-09-19
- Posts: 142
Re: TXP Templating Tip
Good advice, Sencer/Alex. Thanks.
I don’t know if sprinkling conditionals in one page template would do much to improve readability (depends largely on the size of the site and the number of sections), but being less error prone is reason enough for me to switch back to the more standard way of doing things.
<blockquote>
It seems backwards [and hardly readable] to me.
</blockquote>
Sencer, I don’t know what I would do without your “tough love.” ;)
Note to moderator: feel free to delete this thread as the templating solution appears to be problematic.
Last edited by aesop1 (2005-11-05 01:21:39)
Offline
#7 2005-11-05 03:55:07
- rsilletti
- Moderator
- From: Spokane WA
- Registered: 2004-04-28
- Posts: 707
Re: TXP Templating Tip
I think the template approach is still a good idea and should remain here, not to mention the follow on information which deals with questions others may have as well. A revised idea might be nice too :)
Offline
#8 2005-11-05 04:04:12
- KurtRaschke
- Plugin Author
- Registered: 2004-05-16
- Posts: 275
Re: TXP Templating Tip
Agreed…it would be nice to have a way to pass variables into a form, so that a form could be reused in multiple situations, just changing certain items as needed (the things that would be passed in variables).
-Kurt
kurt@kurtraschke.com
Offline
Re: TXP Templating Tip
Sencer, I don’t know what I would do without your “tough love.” ;)
Well, er, it’s very creative and out-of-the-box thinking, too! ;)
Offline
#10 2005-11-05 18:43:33
- aesop1
- Archived Plugin Author
- Registered: 2004-09-19
- Posts: 142
Re: TXP Templating Tip
<blockquote>
rsilletti: I think the template approach is still a good idea and should remain here, not to mention the follow on information which deals with questions others may have as well. A revised idea might be nice too :)
</blockquote>
Agreed, I hate to use the flexibility and brevity of the templating approach. But Sencer does raise a good point in that it does work backwards from the way TXP is designed. Although the <code>txp:php</code> tags and global variable approach mentioned by Alex might be the way to go, it also makes the code a little more verbose.
Perhaps there is a plugin solution(s) that may make this fly in a more “safe and sane” manner.
<blockquote>
Sencer: Well, er, it’s very creative and out-of-the-box thinking, too! ;)
</blockquote>
Don’t go soft on me now, Sencer, this forum needs your no-nonsense Spaghetti Western analysis. It’s a tough job, hombre, but somebody has to do it.
Offline
#11 2006-01-04 05:19:53
- the005
- New Member
- Registered: 2006-01-04
- Posts: 2
Re: TXP Templating Tip
hello,
i’m not sure where to put my question so i’ll ask it here anyway:
i’m using TXP 4.0.3 and < ?php ? > tags is not working, so i use < txp:php>< /txp:php> instead.
but, why this following code doesn’t work ?
< txp:php>
$var=”< txp:title/>”;
echo base64_encode($var);
bq. < /txp:php>
can we use TXP tag inside < txp:php> ?
thanks
sorry if this is out of topic
Last edited by the005 (2006-01-04 05:22:17)
Offline
#12 2006-01-04 06:51:53
- zem
- Developer Emeritus
- From: Melbourne, Australia
- Registered: 2004-04-08
- Posts: 2,579
Offline