Beginners Tutorial for Views 2 Theming: Creating an alternative block for recent comments

After I got my first actual comment last night (yeah!), I wanted to add the "most recent comments" as a block in the sidebar. Drupal comes with a block for that purpose, but I wasn't happy with the way the block displayed the comments. I remembered that I very much liked the way Typepad displays comments (e.g. like this on a friends blog).

I wanted to display the name (linking to the person's homepage), the word "comment" (linking to the actual comment), the title of the story (linking to the story itself), and the date. So in toto, something like this: "Daniel Hanold added a comment to Story Name X (5 days ago)".

Obviosuly, I created a view that displays comments that were recently created. I attached that view to this story for reference. However, I couldn't get the ouput of that view to display what I wanted. First of all, all the labels were added before the data and were followed by a colon. Views theming, here I come. Usually, there's tons of great documentation and examples for pretty much any module. For views theming however, I didn't find anything that explained the concept well and gave some easy to understand examples, so here's a tutorial to the easiest possible theming of a view output.

Like I said, the output I wanted was "Daniel Hanold added a comment to Story Name X (5 days ago)". The output views gave me by default was "added a: Daniel Hanold to comment Story X 5 days ago". Using views theming, I wanted to do a couple of things:

  • Remove the colon
  • Have the label show up behind the data
  • Add parenthesis around the date

To do that, go to your view and click on "Theme: Information". This gives you a lot of theming information about all the things you can theme. Since this is going to be a basic tutorial, I'll only talk about the row style output of the data fields.

On this page, you can find one link that's called "Row style output". With a file such as views-view-fields--stories-recent-comments.tpl.php (this goes into your theme directory), you can style the general output of all fields of this view. Using that template, I was able to remove the colon and have the labels show up behind the data fields. I attached this file for reference as well. The one remaining thing was adding parenthesis around the data. With a file called views-view-field--stories-recent-comments--block--timestamp.tpl.php, you can style the output of just that one data field, in this case the date (see attached file).

Upload those two files to your server, go back to your view, click on "Theme: Information" and then click on "Rescan template files" for the new templates to be activated. After you've added all those steps, you have a nice "recent comments" block that looks like this:

Recent Comments Example

AttachmentSize
stories_recent_comments.txt2.79 KB
views-view-fields--stories-recent-comments.tpl_.php.txt1.73 KB
views-view-field--stories-recent-comments--block--timestamp.tpl_.php.txt910 bytes

Comments

Hi. I found this blog post via a Google search, and you're describing almost exactly what I'm trying to do. I'm still failing, though. There's something I'm not understanding, and I'd like to look at the example files you said you attached, but I can't find them. Where are they?

Thanks for the mini-tutorial.

- Mike

Hi Mike, So sorry for the really late reply. You should be able to see the attachments now. Since writing that tutorial, I've looked at advanced theming in much more detail, so let me know if you have more questions.

Really like your view. Some real matter's are discuss in here. Thanks for sharing with us. The information which you have provided in here in utterly important.

__________________________

Nick From Amaderblog

I especially like the entension of header and footer of views since you definitely have problems of accessing the views variables from there and as Views are often used as blocks sexy lingerie or pages one wants to put all kinds of stuff there (a button that leads to signon to the event of the event participants displayed in the block? Not so easy - go and try).

Hi Daniel,

I am going through the same problem now.
trying to theme a view displayed as a block.
I am confused with the naming conventions.
Can you Please tell me what the above file are meant for
views-view-fields--stories-recent-comments.tpl_.php.txt and
views-view-field--stories-recent-comments--block--timestamp.tpl_.php.txt.

I am not able to download your files.
I am good with uderstanding this but got confused with the naming conventions!

help appreciated!
Thanks in advance

Hey hema, To be quite honest: Since these theming templates are almost a year old, I'm not so sure that they still work with the latest version of views, but they should. The two files you mentioned above are the theming files that overwrite the standard way the (1) comments and (2) comments in a block are displayed. The naming conventions are specified by the Views module itself. If you import the attached view (see above) and click on "Theme: Information", you can see a variety of theme file templates that are used for theming; depending on which template you choose, you can style a field more specific or general.

wao it is very good interface in this , I am very like it Sir ^^

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
More information about formatting options
CAPTCHA
This question tests whether you are a human visitor and to prevent automated spam submissions.