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.

Widgets /

Discuss SilverStripe Widgets.

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

RandomContentWidget


Go to End


8 Posts   2164 Views

Avatar
njw

Community Member, 7 Posts

19 March 2009 at 6:46am

I am not sure whether this belongs here or in the Themes forum, so apologies if it is in the wrong place.

I am new to Silverstripe and I am using an amended version of the Dark Ritual theme (only the CSS is amended to date). I would like to put the RandomContentWidget onto the sidebar, but when I go to the Home page, the enabled widget sidebar does not show.

Is there a way to code the widget in to the page.ss template, replacing the hardcoded "Lobontis" text? And if so, how please!

Many thanks for any help.

Avatar
TotalNet

Community Member, 181 Posts

21 March 2009 at 11:23am

As per the other topic:

It can be done as far as I know, instructions here:
http://doc.silverstripe.com/doku.php?id=widgets#adding_widgets_to_other_pages

Cheers,

Rich

Avatar
Double-A-Ron

Community Member, 607 Posts

25 March 2009 at 6:00pm

Edited: 25/03/2009 6:01pm

I do this manually for displaying random product pages on the homepage. Quite easy:

In HomePage.php inside HomePage_Controller class:

	// Get featured tour data
	function getFeaturedTour() {
		return DataObject::get_one('Tour', 'TourLengthDays>0', false, 'RAND()');
	}

Where 'Tour' is the page type and the second argument is a WHERE statement.

Then in the HomePage.ss template:

<% control getFeaturedTour %>
    <div class="featured">
        <div class="featured-top">
            <div class="featured-btm">
                <h4>Featured Tour</h4>
                <a href="$Link">
                    $Photo2.SetWidth(205)
                </a>
                <h5 style="margin: 0px">
                    <a href="$Link" style="color:#023107">$Title</a>
                    <small>$TourLeavesFrom to $TourEndsAt - $TourLengthDays Days</small>
                </h5>
                <% if TourTeaser %>
                <div style="font-weight: bold; text-align: justify; margin-right: 5px"><b>$TourTeaser</b></div>
                <p><a href="$Link">Click for details</a></p>
                <% end_if %>
            </div>
        </div>
    </div>
<% end_control %>

As you can see I have access to all fields in that Page Type now.

Cheers
Aaron

Avatar
njw

Community Member, 7 Posts

28 March 2009 at 4:53am

Thanks Aaron. That looks to solve my problem - and perhaps a few more!

One question: your code references a field Page Type. I am using standard pages for the text I want to display. I've looked at the database, on the SiteTree table and can't find that field. The closest would appear to be ClassName, but that has a fixed list. What am I missing?

Many thanks

Neil

Avatar
Double-A-Ron

Community Member, 607 Posts

30 March 2009 at 9:20am

Yes, "Tour" is a custom page type that I made. PageType is not a data element.

If you are just using standard pages for your content, just replace "Tour" with "Page" in my code, and remove the "TourLengthDays>0" select query.

Cheers
Aaron

Avatar
njw

Community Member, 7 Posts

31 March 2009 at 1:35am

Thanks again Aaron. In case anyone finds it useful, this is how I amended Aaron's code.

In Page.php

// Get featured testimonial
   function getFeaturedTestimonial() {
      return DataObject::get_one('Page', 'ParentID=13', false, 'RAND()');
   }

I have all the pages I want to display under the same Parent Page. The page ID is 13 - this works, but it would be nice to have it work off the page title instead.

In Page.ss

<% control getFeaturedTestimonial %>
	<h4>What Our Clients Think</h4>
	<blockquote>
		<h4 style="margin: 0px">$Title</h4>
		$Content
	</blockquote>
<% end_control %>

Thanks again Aaron

Neil

Avatar
Double-A-Ron

Community Member, 607 Posts

31 March 2009 at 8:40am

but it would be nice to have it work off the page title instead

An even more organised way of doing this would be to go back to what I said about Page Types.

I assume you only want the code to work of the parent title because you want it to say something like "ParentTitle = 'Testimonials'" so there is never any risk of accidently loading content from an incorrect Page.

You could instead create a custom "TestimonialPage" for these pages, and that would be as easy as this:

/mysite/code/Testimonial.php

class Testimonial extends Page {
	static $db = array(
	);
	static $has_one = array(
	);
	
}

class Testimonial_Controller extends Page_Controller {

	
}

Change the testimonials to be this page type on the behaviors tab (at this stage, there is no difference between this and the original Page).

Then your random code can be like this:

// Get featured testimonial
function getFeaturedTestimonial() {
return DataObject::get_one('Testimonial', null, false, 'RAND()');
}

Easier to follow, you will always only get Testimonials returned by the code, and you also get the option of adding specialized fields and styling to this page type should you wish.

Just alot more organised.

Cheers
Aaron

Avatar
njw

Community Member, 7 Posts

31 March 2009 at 9:34am

Yep, that was what I was looking for!

Many thanks

Neil