Michael (masterslacker) wrote in component_help,
Michael
masterslacker
component_help

Customising your Page Summaries

pyaari asked here how the RecentPage summary component could be altered to include a comment count next to each subject, so I decided to make this into a general post for everyone to see as this technique could be used to modify any of the other summary components as well.

Component uses the layout-defined function lay_print_summary() to print the summary component. If you would like to modify the way any of them print, you need to override the function on it's respective page. For example, to change the way the summary prints on the Recent Page, you need to override RecentPage::lay_print_summary() as shown in the code below. Because a FriendsPage is an extension of a RecentPage, overriding this function will affect both pages, so there are cases within the code to handle both page. Other page types are {DayPage, EntryPage, ReplyPage, MonthPage, YearPage}.

I wanted to show how to add more than just a comment count, so the code below formats the RecentPage entries in the form:
  <date> <time> : <security_icon> <subject> [+<comment_count]
and FriendsPage entries in the form:
  <poster_name> : <journal_name> : <security_icon> <subject> [+<comment_count>]

You can change the punctuation symbols by altering code in red, and you can change the ordering by swapping the lines around. If you do this, be careful of where you place whitespace between elements on each line. Note also that the link on the subject takes you to the entry on the current page, and the link on the comment count takes you to the entry page (the one with comments) on that person/community's journal.

# Change the way RecentPage and FriendsPage summaries print. (FriendsPage inherits from RecentPage)
function RecentPage::lay_print_summary() {

   # print the header of the component and determine the size of the entries array
   print_comp_header($*page_summary_text);
   var int size = size $.entries - 1;
   if ($size<0) { return; }

   # loop on the entries
   foreach var int pos (0..$size) {

      # Get the Entry and start the summary line
      var Entry e = $.entries[$pos];
      var string subject = ($e.subject!=""?$e->plain_subject():"<i>(no subject)</i>");
      """<div>""";

      # On the Friends page print: <poster_name> : <journal_name> : <security_icon> <subject> [+<comment_count>]
      if ($.view=="friends") {
         """$e.poster """;
         if ($e.journal.username!=$e.poster.username) { """: $e.journal : """; }
         if (defined $e.security_icon) { """$e.security_icon """; }
         """<a href="#$e.journal.username$e.itemid">$subject</a>""";
         if ($e.comments.count>0) { """ <a href="$e.comments.read_url">[+$e.comments.count]</a>"""; }

      # On the Recent page print: <date> <time> : <security_icon> <subject> [+<comment_count>]
      }else{ # recent page
         print $e.time->date_format()+" "+$e.time->time_format()+" : ";
         if (defined $e.security_icon) { """$e.security_icon """; }
         """<a href="#item$e.itemid">$subject</a>""";
         if ($e.comments.count>0) { """ <a href="$e.comments.read_url">[+$e.comments.count]</a>"""; }

      # Close if..else loop and end the summary line
      }
      """</div>""";

      # If it's not the last summary line, print a seperator bar
      if ($size!=$pos) { 
        """
        <div class="medLine"><img src="http://www.livejournal.com/palimg/component/clear.gif" width="1" height="1" alt="" border="0" /></div>
        <div class="ltLine"><img src="http://www.livejournal.com/palimg/component/clear.gif" width="1" height="1" alt="" border="0" /></div>
        """; 
      }

   # End the loop for entries
   }

   # Print the component footer when done.
   print_comp_footer();
}
Tags: !tutorial, *account level: paid/perm, class: recentpage, function: lay_print_summary, user: masterslacker
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic
  • 33 comments