Express List - Custom Template: individual CSS classes for ColumnValues

I'm working on a custom template for the express_entry_list block, and i'm trying to add CSS classes to the attribute items of the results, because i want to be able to style the output differently. I have changed the default table markup to div containers, and now i want to have each attribute output as div container with a separate CSS class, named after the handle of the attribute, like so:

<div class="[attribute_handle]"><?php echo $column->getColumnValue($item);?></div>

So the output of an item shoud look like this:

<div class="ccm-block-express-entry-list-row-a">
<div class="firstname">Joe</div>
<div class="lastname">Mayer</div>
<div class="profession">Teacher</div>
<div class="birthdate">05.10.1966</div>

Unfortunately i don't know how to get the attribute handle for each item. I have tried to get it by

<?php echo $ak->getAttributeKeyHandle();?>
but i don't know how to integrate that into the loop, but maybe it's a wrong approach anyway.

Can someone please help?

View Replies:
MrKDilkington replied on at Permalink Reply
Hi okapi,

Here is something to try:
<?php defined('C5_EXECUTE') or die('Access Denied.');
// Concrete\Core\Entity\Express\Entity
$personEntity = Express::getObjectByHandle('person');
// Concrete\Core\Express\EntryList
$personEntryList = new Concrete\Core\Express\EntryList($personEntity);
// $people is an array of Concrete\Core\Entity\Express\Entry objects
$people = $personEntryList->getResults();
if ($people) {
    echo '<div class="ccm-block-express-entry-list-row-a">';
    foreach ($people as $person) {
        // $attributes is an array of Concrete\Core\Entity\Attribute\Value\ExpressValue objects
        $attributes = $person->getAttributes();
        foreach ($attributes as $attribute) {
            echo '<div class="' . $attribute->getAttributeKey()->getAttributeKeyHandle() . '">' . $attribute->getDisplayValue() . '</div>';

- This loops through the "Person" express object entries.
- The person entry attributes are looped over to display the attribute handle and attribute display value.
- The "get" magic methods are not used, so you don't need to know/enter the attribute handles.
- This should allow for a generic template to be created.
okapi replied on at Permalink Reply
I found a simple solution, that also works as long as i'm building an Express Entry List template for each Data Object. So, when knowing the handles, actually i don't need to call them by a function, i can just dirty hardcode them as CSS class names, since this template can only be used for this specific Data Object anyway.

foreach ($result->getItems() as $item) { ?>
<div class="firstname"><?php echo $item->getEntry()->getFirstname(); ?></div>
<div class="lastname"><?php echo $item->getEntry()->getLastname(); ?></div>
<div class="profession"><?php echo $item->getEntry()->getProfession(); ?></div>
<?php } ?>

But what i'm looking for is a more elegant solution, a template for universal usage, without having to know the handles of the attributes, a function that retrives the attribute handles from inside the foreach loop, if this is possible.
PixelFields replied on at Permalink Reply
@okapi This was exactly what I was looking for to format a really specific express object list as blocks of individual data instead of a table format. Thank you!!
okapi replied on at Permalink Reply
By the way, for image attributes (example handle here: photo_person) you would need something like this:

$photoFileObject = $item->getEntry()->getPhotoPerson();
if (is_object($photoFileObject)) {
    $photoFileObjectVersion = $photoFileObject->getVersion();
    $photoRelativePath = $photoFileObjectVersion->getRelativePath();
    <div class="photo-person"><img src="<?php echo $photoRelativePath; ?>"></div>
<?php } ?>
okapi replied on at Permalink Reply
Thank you so much, this works great now for text attributes!

I think it would get quite complex, if it also should check what type of attribute is processed, because an image for example needs some HTML around the output, in order to have it displayed as image, instead of just showing it's path, or a "mailto" parameter for email links...
MrKDilkington replied on at Permalink Reply

You could get the attribute type handle, then based on the handle decide how to process. A switch might work for that.