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.

Archive /

Our old forums are still available as a read-only archive.

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

Using Variables in Controls... I think


Go to End


3 Posts   1528 Views

Avatar
Alex S

Community Member, 30 Posts

9 September 2008 at 6:25am

Hey, this might be a dumb question, but...

I've just finished tutorial 2 and thought I'd extend the news on the homepage functionality to check that I'd understood everything. I added a check box which determines if the news shows, and a number to define how many news items show. The check box works fine and the number is stored correctly, I can view it on the page using the variable '$NumTopNews'. So far I've tried to use the '$NumTopNews' variable as an argument to the 'LatestNews' function, without any luck. E.g.

...
<% control LatestNews($NumTopNews) %>
...

My guess is that this means it should go into the controller, and the number be written directly into the 'LatestNews' function, trouble is, I'm not really sure how to do that.

Could someone give me a nudge in the right direction?

Cheers,

Alex

Avatar
Alex S

Community Member, 30 Posts

9 September 2008 at 6:48am

Yup, I was being dumb, turns out it was the controller that needed to be changed:

...
function LatestNews() {
$news = DataObject::get_one("ArticleHolder");
return ($news) ? DataObject::get("ArticlePage", "ParentID = $news->ID", "Date DESC", "", $this->NumTopNews) : false;
}
...

Works great :-)

Nice CMS by the way.

Alex

Avatar
Willr

Forum Moderator, 5523 Posts

9 September 2008 at 9:42am

As you probably realised with <% control LatestNews($NumTopNews) %> you cant use variables as parameters to methods, its a limitation of the template syntax so you can do <% control LatestNews(10) %> rather then $NumTopNews