Skip to content

Instantly share code, notes, and snippets.

@CNDLS
Created March 19, 2018 18:36
Show Gist options
  • Save CNDLS/2c5a127c815a494ac14911d03cd634f8 to your computer and use it in GitHub Desktop.
Save CNDLS/2c5a127c815a494ac14911d03cd634f8 to your computer and use it in GitHub Desktop.
Shows the PHP template file for a custom Elementor widget.
<div class="schedule">
<div class="schedule__inner">
<h3 class="schedule__heading"><?php echo $sample_schedule['title']; ?></h3>
<table class="schedule__table">
<thead class="schedule__table-header">
<tr class="schedule__table-header-row">
<?php foreach ( $sample_schedule['column_headings'] as $column_index => $heading ) : ?>
<?php
$column_visibility_classes = '';
if ( $column_index === 2 ) {
$column_visibility_classes = ' d-none d-sm-table-cell';
}
?>
<th class='schedule__column-heading schedule__column-heading--first-column<?php echo $column_visibility_classes; ?>' scope='col'>
<?php echo $heading['heading']; ?>
</th>
<?php endforeach; ?>
</tr>
</thead>
<tbody class="schedule__table-body">
<?php
/*
* Visually, the schedule table is made of thick rows with alternating background colors. Each thick row has the name of a semester as
* as row header. In reality, each thick row is made of of several interior rows, one for each requirement, since there can be several requirements per
* semester, with each requirement taking up a new <tr>.
*
* We can tell when a new semester row starts by when the $row has a non-empty value in the $row['semester'] field.
*/
$semester_index = -1; // Count of semester rows.
$requirement_index = 0; // Count of the number of requirements in each semester
$row_index = 0; //Overall rows (<tr>) in the table body.
$requirements_per_semester = []; // This will store how many requirements there were in each semester
// Start the PHP output buffer, so that we can go back in and adjust the rowspan attribute of the semester
// row headers later, after we know how many requirements are in each semester.
ob_start();
?>
<?php foreach ( $sample_schedule['rows'] as $row_index => $row ) : ?>
<?php
if ( empty( $row['semester'] ) ) {
$requirement_index++;
if ( $row_index === 0 ) {
ob_end_clean();
echo "<pre>The first row of the schedule table must have a value for the Semester field.</pre>";
break;
}
} else { // If $row['semester'] has content
$semester_index++;
if ( $row_index > 0 ) { // If it's not the first row in the table
// Record how many requirements were in the previous semester
$requirements_per_semester[] = $requirement_index + 1;
$requirement_index = 0;
}
}
// If we are on the last row, add a final value to $requirements_per_semester (since no additional semester values will
// come after the last row.
if ( $row_index === ( count( $sample_schedule['rows'] ) - 1 ) ) {
$requirements_per_semester[] = $requirement_index + 1;
}
// Alternating background colors when semester row changes.
if ( ($semester_index % 2) === 0 ) {
$table_bg_color_class = 'table-md-bg-gray';
} else {
$table_bg_color_class = '';
}
?>
<tr class='schedule__table-body-row <?php echo $table_bg_color_class; ?>'>
<?php if ( ! empty( $row['semester'] ) ) : ?>
<?php
// Since I don't know how many requirements will be in each semester, I placehold the rowspan value
// of the semester row heading with '%{$semester_index}$d',
// and then insert the number using vprintf once I know what it is.
// Needed because format placeholder indices are 1-based.
$format_placeholder_index = $semester_index + 1;
?>
<th class='schedule__row-heading' scope='row' rowspan='%<?php echo $format_placeholder_index; ?>$d'>
<?php echo $row['semester']; ?>
</th>
<?php endif; ?>
<td class="schedule__body-cell">
<?php echo $row['requirement']; ?>
</td>
<td class="schedule__body-cell d-none d-sm-table-cell">
<?php echo $row['type']; ?>
</td>
</tr>
<?php endforeach; ?>
<?php
$html_output = ob_get_clean();
// Insert the rowspan values now that we know how many requirements are in each semester and output the result
vprintf( $html_output, $requirements_per_semester );
?>
</tbody>
</table>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment