23/06/04
Textpattern notes Part 2
Please note the information in this entry is now superceded
For the second of my notes on Textpattern conversion, I’m going to cover just one subject : hacking the txp comment system.
Purpose of this hack:
Change the recorded comments to display as <div>’s rather than <li>’s, with alternating background colours and sequential numbering. Change the comment form to allow live previews to be used.
What you need to know first:
Upgrading Textpattern is a simple process, you simply upload all the new files (apart from the config.php file) and load _update.php in your browser to make sure all your database tables are updated. Its so quick. What I’m doing here is hacking the /textpattern/publish/comments.php file – a lot. This means that when you update, you’ll have your work cut out for you.
Still want to do it? Make a backup of that comments.php first, and then read on.
Background:
Textpattern, handles commenting a little differently to systems like Movable Type. A lot of the work is carried out behind the scenes, away from the page templates and forms, making personalisation harder. The comment form is laid out in presentation>forms>comment form, the display of comments is found in presentation>forms>comments. Then, as long as you have the <txp:comments_invite /> tag inside your article form (presentation>forms>default), Textpattern will add these elements together, along with the preview and remember details functions.
Step 1 – Change the existing preview method.
Dean Allen designed txp’s preview-first/submit-last routine to avoid spam flooding. This hack removes that safety net, and allows comments to be submitted directly, but I think this is less annoying for users. If we want to implement live comment previews, we have no choice – it has to go.
Justin Low worked out how to change this situation. Work through his instructions here, and you’re done.
Step 2 – changing the comments to display as <div>’s rather than <li>’s
I wanted to do this in order to have more control over my CSS. The ordered lists add comment numbers without any fancy coding, but restrict what markup can go inside the comment, and how much can be changed with CSS. Changing it to <div>’s gives that flexibility.
Still in /textpattern/publish/comments.php find the following code (around line 34):
f ($darr) {
$out.= '<ol>'.n;
$out.= formatComments($darr);
$out.= n.'</ol>';
}
and change it to:
if ($darr) {
$out.= '<div id="comment">'.n;
$out.= formatComments($darr);
$out.= n.'</div>';
}
then find this (around line 132):
$out .= n.t.'<li id="c'.$discussid.'"
style="margin-top:2em">'.$temp.'</li>';
and change it to:
$out .= n.t.'<div id="c'.$discussid.'"
class="'.$class.'">'.$cno.' '.$temp.'</div>';
There are 2 php values in here, which we’ll use in the next step:
Step 3 – alternating background colours and sequntial numbering
Obviously, I can’t use the MTFlipFlop plug-in here, so I had to find a different solution. There’s also no tag in txp to output a comment number (because it used the ordered lists – which I removed!).
Once you’ve set up your 2 CSS classes for the alternating rows (I’ve used ‘odd’ and ‘even’), find the end of the php function – function formatComments($darr) (around line 75) and insert this code:
// set up div class and numbering
$class=odd;
$cno=1;
These are our 2 values, $class for inserting the div class, and $cno for the sequential comment number. Then, returning to the line where we changed the <li> to a <div>:
$out .= n.t.'<div id="c'.$discussid.'"
class="'.$class.'">'.$cno.' '.$temp.'</div>';
place this after it (but before the closing brace):
// change the values
$cno=$cno+1;
if("odd" == $class) {
$class="even";
}
else {
$class="odd";
}
This adds 1 to number count, and the decides whether the next time it loops, the div class should be odd or even. We’re almost there…
Step 4. Implementing Live Previews
The original post on how to do this can be found here. Now that we’ve done all the changes listed above, we can hard code the comments form.
In presentation>forms>comment form change this tag:
<txp:comment_name_input />
to:
<input type="text" id="name" name="name" tabindex="1"
onkeyup="ReloadNameDiv();" value="<?php echo $aname; ?>" />
and then, back in comment.php, we need to change the <textarea> tag to:
$textarea = '<textarea name="message" id="message"
onkeyup="ReloadTextDiv();" cols="1" rows="1" tabindex="4">
'.htmlspecialchars($message).'</textarea>';
If users have already been on your site, chances are that a cookie has been set, allowing their details can be inserted automatically. By hard coding the name input, we have to do a bit more jiggery-pokery to get this working again. When we changed the <input> tag we added value="<?php echo $aname; ?>". This will output the cookie value into the text field, but we also need to grab the cookie values. To do that, place this somewhere above the form (I put this at the very top of my page template):
<?php
$aname = $_COOKIE['txp_name'];
?>
(Thanks to Justin for that bit). Then, still on that page template, you just need to add the preview div at the bottom, after the <txp:article /> tag:
<txp:if_individual_article>
<div class="odd">preview :<a href="#" id="NameDisplay">
<?php echo $aname; ?></a> on <?php $today=date("d.m.y");
print "$today"; ?>:
<div id="TextDisplay"></div>
</div>
</txp:if_individual_article>
The conditional tags stop the preview being displayed on the homepage where there are a number of articles, restricting it to a single entry page.
Step 5 – Textile preview
If you want to really finish things off, how about implementing Textile parsing in the preview? Jeff Minards javascript solution does exactly that, based on code by Stuart Langridge. Its genius!
You’re done at last. Have a drink, you deserve it!
17
Tags: 


Previous





Download our vCard
Sage said 1570 days ago:
Have you managed a way to get TXP to handle comment messages with paragraphs instead of breaks? That’s the one thing that’s really breaking my Web Standards heart right now.Anyway, love the tutorials! :-)
Sage said 1570 days ago:
BTW, by my above comment, I meant to enclose paragraphs separately, instead of just surrounding the whole message with a paragraph tag and having breaks in between.Douglas said 1570 days ago:
Will have a fiddle with that :)Nice job – now to find a JS version of textile %)
Douglas
Marshall said 1570 days ago:
Sage, and any others, you can make Comments use full Textile by replacing two lines in the publish/comment.php file.#87 : $message = trim($textile->TextileThis(strip_tags($message),0,#039;#039;,$im));
#301 : $message2db = addslashes($textile->TextileThis($message,0,#039;#039;,$im));
Basically all I did was change the second argument of the TextileThis function, then remove the “nl2br” function call.
Doing this will allow comments to have all Textile functions (table’s, headers, everything).
The Live Comment Preview is really awesome!
Sage said 1570 days ago:
Oh, cool, thanks! :-)Mark said 1569 days ago:
Jon,I want to thank you for making my mind up.
For months now i’ve been wrangling with MT, desperately trying to tell myself it’s the right tool for the job – a portfolio site with a blog, but predominantly a portfolio site.
TXP seems to be the way to go.
These tutorials are invaluable for helping me make the switch.
Ta
PS. Just noticed the live preview. That is very cool indeed.
Jon Hicks said 1569 days ago:
Marshall – thanks for that!Mark- I’ll be writing up about how I did the portfolio in TXP soon. I think TXP is ideal if you want to manage an entire site, although I used to use MT for my blog and portfolio OK.
Jeff Minard said 1569 days ago:
Textile is enabled but will not preview?Well fix that silly ;-)
Textile JS Function
Jon Hicks said 1569 days ago:
Jeff – you’re just a frickin’ genius!!Mark said 1569 days ago:
Cheers Jon,Looking forward to seeing the Portfolio writeup. I really need to stop farting around now and get on and design the thing! :)
I’m just trying to familiarise myself with TXP at the moment. One thing is concerning me though. I’ve just tried importing my old MT blog data (following the thread here: http://forum.textpattern.com/viewtopic.php?id=288) but it’s not working at the moment. (I keep getting “duplicate entry” errors)
Can you recommend a script that is reliable?
Jon Hicks said 1569 days ago:
I’ve had problems with duplicate entries. The best way is to start off with no entries in TXP, and then import all your MT entries. I had put in several entries first that conflicted with the MT entries. As a result, the entry doesn’t get imported. Apart from that, should be OK.Matt said 1569 days ago:
// change the values$cno++;
(“odd” == $class) ? “even” : “odd”;
Save those bytes, dude! ;o)
Mark said 1569 days ago:
Thanks Jon,I used the script here – http://forum.textpattern.com/viewtopic.php?id=1490, which looks like it was the one you used. All went well.
I really should be doing some work…
Jon Hicks said 1569 days ago:
Mark – no, I mostly used Dean’s script that took the data straight from the databaseSage said 1569 days ago:
Marshall:——Sage, and any others, you can make Comments use full Textile by replacing two lines in the publish/comment.php file.
#87 : $message = trim($textile->TextileThis(strip_tags($message),0,#039;#039;,$im));
#301 : $message2db = addslashes($textile->TextileThis($message,0,#039;#039;,$im));——
I just tried this, and I’m getting the following PHP error:
Parse error: parse error, unexpected ’}’ in /home/gtplanet/public_html/insider/textpattern/publish/comment.php on line 93
I’m assuming this is related to the hash marks being in those two lines of code. Also, I tried removing the hash marks, but then it reads the extra semicolons and gets confused.
David House said 1559 days ago:
Matt – even easier. Get rid of the $class variable, and instead:$out .= n.t.’<div id=”c’.$discussid.’”
class=”’.($cno % 2 ? ‘odd’ : ‘even’) .’”>’.$cno.’ ’.$temp.’</div>’;
Ben Smith said 1558 days ago:
Hey Jon, excellent walk-through very helpful!All ran smoothly except I still cant get cookies to work again