user3257693
user3257693

Reputation: 486

Silverstripe 3.1 Is the title/name of a field available in an ss template?

For example

static $db = array('CarName' => 'Text')
public static $summary_fields = array('CarName' => 'Car Name')

In the ss template this will show the value of the field called CarName

<p>$CarName</p>

Can I also display the title that I included in my $summary_field ie. 'Car Name' instead of just typing it in manually in the ss template?

Upvotes: 1

Views: 74

Answers (3)

user3257693
user3257693

Reputation: 486

Wrote this function which works, put it in the Page_Controller of the page your using -

public function getSummaryFlds() {
    $getSummFlds = $this->owner->summaryFields();
    $newArr = array();
    foreach ($getSummFlds as $dbfield => $title) {
        $value = $this->$dbfield;
        $newArr[] = ArrayData::create(array('Title' => $title, 'Value' => $value));
    }
    var_dump($getSummFlds);
    $newArray = ArrayList::create(
                $newArr
            );
    $result = $newArray;
    return $result;
}

Then in the ss template

<% loop $SummaryFlds %>
    <dt>$Title</dt><dd>$Value</dd>
<% end_loop %>

Result is all the $summary_fields with the title and value.

Upvotes: 0

Jono
Jono

Reputation: 166

I don't know if this will work but there's a getName() function on DBField so I would try $CarName.Name

Upvotes: 0

bummzack
bummzack

Reputation: 5875

As far as I know, there's no direct accessor to individual summary fields. You could write a simple extension that does that for you though. Something along the lines of:

<?php
class SummaryExtension extends DataExtension
{
    public function SummaryField($fieldName){
        $fields = $this->owner->summaryFields();
        if(isset($fields[$fieldName])){
            return $fields[$fieldName];
        }
        return $fieldName;
    }
}

Then add the extension to your DataObject either via _config.yml or directly as static variable:

private static $extensions = array('SummaryExtension');

In the template, you can then output the title by writing:

$SummaryField('CarName')

Upvotes: 2

Related Questions