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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Sliverstripe 3.1 Loop Dataobjects does not work


Go to End


5 Posts   748 Views

Avatar
Praveen

Community Member, 49 Posts

9 October 2014 at 9:51pm

Edited: 10/10/2014 2:13am

Existing Code in the Silverstripe 2.4 template

Header.ss
<% control Objects(QuickLink) %>
<li><a href="$Link" target="_blank">$LinkText</a></li>
<% end_control %>

this no longer works on 3.1. I replaced the control with loop

Can I use a DataObject in a template which has no relation with Page. Like here QuickLink is not having relation with any of the Dataobjects. And it must be displayed in the frontend on all the pages. It works fine in 2.4 version and not working in 3.1.

<% loop Objects(QuickLink) %>
<li><a href="$Link" target="_blank">$LinkText</a></li>
<% end_loop %>

class QuickLink extends DataObject
{
static $db = array(
'Link' => 'Varchar(255)',
'LinkText' => 'Varchar(100)'
);

function getCMSFields()
{
$fields = new FieldList();

$fields->push(new TextField('Link'));
$fields->push(new TextField('LinkText'));

return $fields;
}
}

Avatar
martimiz

Forum Moderator, 1391 Posts

10 October 2014 at 2:36am

I wasn't aware that you could do <% control Objects(SomeObject) %> in a Template in 2.4 out of the box?

Aside from that: in v3 you can absolutely do that, by creating a function in your Page_Controller class like for instance:

	public function getCustomObject($classname) {
		if (class_exists($classname)) {
			return $classname::get();
		}
	}

And in your template:

<% loop getCustomObject(QuickLink) %>
...	
<% end_loop %>

Avatar
Praveen

Community Member, 49 Posts

10 October 2014 at 4:45pm

Thanks martimiz,
getCustomObject was missing on Page Controller. I was thinking it something related template syntax.I could not figure it out.

Avatar
kinglozzer

Community Member, 187 Posts

10 October 2014 at 9:05pm

Hi Praveen,

I think the syntax you may have been referring to is:

<% loop List(QuickLink) %> 
...    
<% end_loop %>

Loz

Avatar
martimiz

Forum Moderator, 1391 Posts

10 October 2014 at 10:01pm

@kinglozzer: I had been looking for this, but never found it :( Thanks, learning new things every day!