Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Displaying enum in templates


Go to End


2 Posts   1859 Views

Avatar
LeeSylvester

Community Member, 11 Posts

29 November 2015 at 2:15pm

Hi All,

I'm using SS 3.2.1. I want all pages in my site to have a navigation colour. I'm doing this in my page code:

	private static $db = array(
		'NavColour' => "Enum('Red,Orange,Green,Blue,Pink,White','Red')"
	);

	function getCMSFields() {
		$fields = parent::getCMSFields();

		$fields->addFieldToTab('Root.Content.Main', new DropdownField(
			'NavColour',
			'Navigation button colour',
			singleton('Page')->dbObject('NavColour')->enumValues()
		), 'Content');

		return $fields;
	}

This works great. I see the dropdown in the CMS and I see the database being populated. However, if I try to output this value in the template with:

<a href="#" class="$NavColour">Nav Item</a>

Nothing is printed... Is there a special way to output enum values?

Many thanks,
Lee

Avatar
Devlin

Community Member, 344 Posts

2 December 2015 at 4:33am

Maybe you didn't save the DataObject and it doesn't have the value yet? I don't think the default value of a Enum field is automatically populated.