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.

General Questions

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

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

Page property access in Controller


Go to End
Reply

10 Posts   1672 Views

Avatar
Lazarus404

27 May 2011 at 12:06am (Last edited: 27 May 2011 9:28am), Community Member, 71 Posts

Hi guys,

Okay, so, I've decided, to make life easy for the site admin, to default custom properties in a new page to match that of the home page. This way, the page doesn't look nasty if they've forgotten to add anything. This works great for single items, as I can just use

<% if Property %>
$Property
<% else %>
<% control Page(home) %>
$Property
<% end_control %>
<% end_if %>

However, a problem arises if the property is an array, as I can't nest control statements, and using

<% control Page(home).Property %>

doesn't work. The question is, then; how can I use the property of some other page in a control statement in a template?

Thanks,
Lee

Avatar
swaiba

27 May 2011 at 12:17am Forum Moderator, 1796 Posts

how about replacing...

<% control Page(home).Property %>

with...

<% control Page(home) %>
<% if Property %>
do stuff
<% end_if %>
<% end_control %>

I'd expect that to work...

Avatar
Lazarus404

27 May 2011 at 9:25am Community Member, 71 Posts

Hi Swaiba,

Yes, that would work if the property was a string or somesuch, but it's an array of items (DataObjects), thus I'd need to use "control" to iterate through it. As controls can't be nested, it means I can't access the data.

Thanks for commenting, though :)
Lee

Avatar
swaiba

27 May 2011 at 7:36pm (Last edited: 27 May 2011 7:39pm), Forum Moderator, 1796 Posts

As controls can't be nested??

I don't follow that, I think I missed the "an array" part. A simple solution is to place your array into a DataObjectSet...

<% control Page(home) %>
   <% if PropertyAsDataObjectSet %>
      <% control PropertyAsDataObjectSet %>
         do stuff
      <% end_control %>
   <% end_if %>
<% end_control %>

function PropertyAsDataObjectSet() {
   $dos = null;
   if (count($this->Property)) {
      $dos = new DataObjectSet();
      foreach ($this->Property as $do) {
         $dos->push($do);
      }
   }
   return $dos;
}

Avatar
Lazarus404

27 May 2011 at 8:19pm Community Member, 71 Posts

Hi Swaiba,

Thanks, but I can't do that either, because I'd still be nesting control expressions, which isn't allowed. Otherwise, I could use it as it :)

Regards,
Lee

Avatar
swaiba

27 May 2011 at 8:31pm Forum Moderator, 1796 Posts

because I'd still be nesting control expressions, which isn't allowed

How is that? Where did you get that idea?
I nest controls all the time...

Avatar
Lazarus404

27 May 2011 at 8:59pm Community Member, 71 Posts

Doh! And therein lies my problem :) I'm not sure where I read that, but I've been avoiding it like soap on the floor of a communal shower.

Thanks buddy, I'll give that a go :)

Lee

Avatar
swaiba

27 May 2011 at 9:09pm (Last edited: 27 May 2011 9:10pm), Forum Moderator, 1796 Posts

Good luck Lee, all silverstripe Bristolians together!

Go to Top