templateServiceLocation()¶
Dynamic Column Template¶
Here’s a clean and powerful example of creating a dynamic column template in PHP. This method enables conditional rendering, customization of labels, and callback functions for generating HTML output. It's all about efficiency and flexibility!
protected function templateServiceLocation(array $options = []): void
{
$showID = array_key_bool('showID', $options);
$showInstructions = array_key_bool('showInstructions', $options);
$pre = array_key_string('pre', $options, '');
$this->tabu1q->templateColumn([
'slid' => ['export' => false],
'name' => ['colname' => $pre . 'site_company_name', 'label' => 'Service Location Name'],
'keywords' => ['colname' => $pre . 'keywords', 'label' => 'Keywords', 'export' => false],
'pid' => ['colname' => $pre . 'pid', 'label' => TERM_CLPID, 'export' => $showID,
'callback' => function ($val, $json) use ($showID) {
if (!$showID || !($val = $val ?: $json->keywords)) return '';
return sprintf('<span style="color:grey"><i class="fa-light me-1"></i>%s</span>', $val);
}
],
'address' => ['colname' => $pre . 'site_address1', 'label' => 'Site Address'],
'city' => ['colname' => $pre . 'site_city', 'label' => 'Site City'],
'county' => ['colname' => $pre . 'site_county', 'label' => 'Site County'],
'state' => ['colname' => $pre . 'site_state', 'label' => 'Site State'],
'zip' => ['colname' => $pre . 'site_zip', 'label' => 'Site Zip'],
'contact' => ['colname' => 'contact.full_name', 'label' => 'Contact Name'],
'phone' => ['colname' => 'contact.bus_phone', 'label' => 'Contact Phone',
'callback' => function ($val) {
return $val ? sprintf('<span style="color:royalblue"><i class="fa-light fa-phone me-1"></i>%s</span><br>', $val) : '';
},
],
'email' => ['colname' => 'contact.bus_email', 'label' => 'Contact Email',
'callback' => function ($val) {
return $val ? sprintf('<span style="color:royalblue"><i class="fa-light fa-mailbox me-1"></i>%s</span><br>', $val) : '';
}
],
'instructions' => ['colname' => 'followup_notes', 'label' => 'Special Notes & Instructions', 'export' => $showInstructions,
'propertyName' => $pre . 'meta',
'callback' => function ($val) use ($showInstructions) {
return $showInstructions && $val ? sprintf('<br><span style="color:firebrick;font-size:0.8em"><i class="fa-duotone fa-comment-lines me-1"></i>%s</span>', $val) : '';
}
]
], [
'label' => 'Service Location',
'expand' => true,
'template' => <<<HTML
<p style="margin:1px;line-height:1.1">
<b>{field:name}</b> {field:pid}<br>
<b style="color:royalblue">{field:contact}</b><br>
<span style="font-size:90%">
{field:phone}
{field:email}
</span>
<span style="font-size:85%">
{field:address}, {field:city} {field:state} {field:zip}
</span>
{field:instructions}
</p>
HTML
])->pxWidth(400);
}