New html_writer shortcut functions

Re: New html_writer shortcut functions

by Davo Smith -
Number of replies: 1
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Make sure you use get_string() for the labels, instead of hard-coding the text strings.

$row = new html_table_row([ ... ]); // Insert your data here.
$row->attributes['class'] = 'nameofcssclass'; // Choose a name for the CSS class.
$table->data[] = $row;

Then, in your styles.css file you can write:

.nameofcssclass {
  background-color: #f00;
}

You can similarly add extra CSS classes to your whole table (all tables have 'generaltable' by default, unless you override it). If you examine the output, you also get a number of generic classes added, which can also be used for styling.


In reply to Davo Smith

Re: New html_writer shortcut functions

by Raymond Mlambo -

Hi Davo,


You're awesome. I just used what you mentioned and it its working well. How can I put vertical borders in my table though?

The table in the screenshot attached is outputting the rows beautifully, but I need vertical borders to separate the items and make things look neat. How can I accomplish? I have specified borders in my css class, and I'm not getting the results that I want.

Here is a sample of the code for this table:

// Display the course rubric so that the students know what they'll be graded on
$courserubric = $DB->get_record('rubric', array('courseid' => $course->id));

$table = new html_table();
$table->head = array($courserubric->h11, $courserubric->h12, $courserubric->h13, $courserubric->h14, $courserubric->h15);

$row = new html_table_row(array($courserubric->n11, $courserubric->n12, $courserubric->n13, $courserubric->n14, $courserubric->n15));
$row->attributes['class'] = 'courserubric-tablerow';
$table->data[] = $row;

$row = new html_table_row(array($courserubric->n21, $courserubric->n22, $courserubric->n23, $courserubric->n24, $courserubric->n25));
$row->attributes['class'] = 'courserubric-tablerow';
$table->data[] = $row;

echo html_writer::table($table);