r44673 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r44672‎ | r44673 | r44674 >
Date:14:07, 16 December 2008
Author:demon
Status:ok (Comments)
Tags:
Comment:
Add two new XML methods for building tables (buildTable and buildTableRow). Accepts associative arrays where the key is the row/cell id and the value is its value. Non-string keys are skipped.
Modified paths:
  • /trunk/phase3/includes/Xml.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Xml.php
@@ -660,6 +660,47 @@
661661
662662 return $form;
663663 }
 664+
 665+ /**
 666+ * Build a table of data
 667+ * @param array $rows An array of arrays of strings, each to be a row in a table
 668+ * @param array $attribs Attributes to apply to the table tag [optional]
 669+ * @param array $headers An array of strings to use as table headers [optional]
 670+ * @return string
 671+ */
 672+ public static function buildTable( $rows, $attribs = array(), $headers = null ) {
 673+ $s = Xml::openElement( 'table', $attribs );
 674+ if ( is_array( $headers ) ) {
 675+ foreach( $headers as $id => $header ) {
 676+ $attribs = array();
 677+ if ( is_string( $id ) ) $attribs['id'] = $id;
 678+ $s .= Xml::element( 'th', $attribs, $header );
 679+ }
 680+ }
 681+ foreach( $rows as $id => $row ) {
 682+ $attribs = array();
 683+ if ( is_string( $id ) ) $attribs['id'] = $id;
 684+ $s .= Xml::buildTableRow( $attribs, $row );
 685+ }
 686+ $s .= Xml::closeElement( 'table' );
 687+ return $s;
 688+ }
 689+
 690+ /**
 691+ * Build a row for a table
 692+ * @param array $cells An array of strings to put in <td>
 693+ * @return string
 694+ */
 695+ public static function buildTableRow( $attribs, $cells ) {
 696+ $s = Xml::openElement( 'tr', $attribs );
 697+ foreach( $cells as $id => $cell ) {
 698+ $attribs = array();
 699+ if ( is_string( $id ) ) $attribs['id'] = $id;
 700+ $s .= Xml::element( 'td', $attribs, $cell );
 701+ }
 702+ $s .= Xml::closeElement( 'tr' );
 703+ return $s;
 704+ }
664705 }
665706
666707 class XmlSelect {

Comments

#Comment by Brion VIBBER (talk | contribs)   23:40, 16 December 2008

While fairly simple looking, IMHO this is too limited, as the contents are limited to raw text. It seems to me that we more often be sticking in links or form elements when we build tables...

Status & tagging log