Implementation of my comments in Expression Engine
Some of you asked for an Expression Engine tutorial a while ago and I thought today would be a good moment to explain how I implemented the comment boxes on my site. There is a lot to talk about so lets move ahead and get on to it…
What's on the EE menu today:
- Alternating comment boxes and your own comment box
- The use of gravatars
- Permalink a comment
- Number the comments
- Hide the e-mail address and show URL
- Date and time stamp
Alternating comment boxes and your own comment box
Having the comment boxes alternate in style is a must. It gives a nice overview, it's more pleasing for the eye. Having your own comment box is even better. It's not that difficult to implement. I'll show you how.
The static XHTML code
Let's have a look first at our static code:

The outer div has a class "comment-box". This div will get another class named "comment-box-alt" for the alternate style of this box and a class "comment-box-v" for the style of the box when I enter a comment.
The EE code
And here is the EE code:

What I did is using the "switch" variable. BTW, you can add more then just 2 styles.
To give my box a special style, I used a conditional variable, which allows you to conditionally show or hide information based on criteria being met. In case you want to highlight your own comment box, you could check this by using your e-mail address. "!=" means "not equal to" "==" means "equal to".
Have a look at the complete EE code

The use of gravatars
For the gravatars I use Les Jenkins' plugin which works with gravatar.com. Just download the plugin and place it in EE's plugins folder on your server. Then add the proper EE code (shown in the image above).
Permalink a comment
To give each comment a permalink I use the comment_id variable. Because these are pure numbers and I want my code 100% W3C valid, I added "c_" in front.
Number the comments
Just add the variable count (the word count between curly brackets) to number your comments.
Hide the e-mail address and show URL
To hide the e-mail address of user, make sure that the "Automatically turn URLs and email addresses into links?" EE Admin is set to "No". You'll find this in Admin > Weblog Management > The name of your blog > Comment Posting Preferences.
As you can see I've used conditionals again: if the URL field in the form is not (!=) empty, then render the link and if not than the link won't be rendered and you'll only see the name. Notice how I added this conditional around the "a" element in 2 separate parts. I added it around the opening tag and a second time around the closing tag.
Date and time stamp
The date and time stamps are also pretty easy to do. Well, everything is, you just need to find the proper code :) I always browse to this overview of the date and time formats you can use in EE.
Additional info
The word "comment" between curly brackets will render the comment. The "link" between curly brackets is a variable I've created that replaces my URL. So each time I want to use this 'path' I just type "link".
Hope you enjoyed my mini EE tutorial. Don't forget to browse the user manual. You'll find a bunch of information there. And if you just start on implementing your templates, make sure you copy the default templates in a separate template group and use them to learn from. Well, that's how I did it ;)
Permalink Comments off






