Jump to:

23545 Posts in 19310 Topics by 2888 members

General Questions

SilverStripe Forums » General Questions » Showing multiple formatted page types on a single page

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: 1
Go to End
Author Topic: 606 Views
  • Tama
    Avatar
    Community Member
    130 Posts

    Showing multiple formatted page types on a single page Link to this post

    Howdy Everyone

    I've got a scenario where I want to show multiple formatted pages on a single page. For example I might have 4 different page types:

    IntroductionPage.php
    StoryPage.php
    PhotoGalleryPage.php
    FeedbackFormPage.php

    The templates attached to each of these output HTML without <html><head><body> tags. Each page could be viewed independently but I also want to be able to join them into one long page and retain any formating or interactive elements.

    Is there a relatively easy way of going about this?

    Pages can be referenced via the "ID" and "Classname" fields. What would be the correct way of calling an ID so the HTML is returned using the templates defined by "Classname"?

    I hope this makes sense, thank you in advance.

    Cheers
    Tama

  • simon_w
    Avatar
    Forum Moderator
    474 Posts

    Re: Showing multiple formatted page types on a single page Link to this post

    Every subclass of ViewableData (DataObject extends this, and SiteTree extends DataObject, then your Pages extends from SiteTree) have a forTemplate() method, which renders the object in its default manner.

    There is also renderWith(), which takes either a single template name or a list of template names, that works similar to forTemplate(), but uses the specified template.

    There's the static method DataObject::get_by_id($class, $id), that returns the object of $class with the ID $id.

    So you could do things like:

    $ids = array(…);
    $content = '';
    foreach($ids as $id) {
    $content .= DataObject::get_by_id('Page', $id)->forTemplate();
    }
    return $content;

    Here I'm assuming all your page types extend from Page.

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: Showing multiple formatted page types on a single page Link to this post

    Hi Simon

    Thank you for that, it's got me going in the right direction.

    I used your code as a base and got the following error:

    [User Error] Uncaught Exception: Object->__call(): the method 'fortemplate' does not exist on 'Page'

    Any ideas?

    I'm going to have a look at "renderWith()" to see if that might suit what I'm trying to achieve.

    Cheers
    Tama

    606 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.