Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Big issue for me, - renderWith

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

Page: 1 2
Go to End
Author Topic: 897 Views
  • George Botley
    Avatar
    Community Member
    16 Posts

    Big issue for me, - renderWith Link to this post

    Hi Guys,

    I am creating a book site for a customer using the controller to have a layout like this...

    www.site.com/books - the books listing page

    www.site.com/books/view/1 - view book with id in database of 1.

    I haven't setup a custom route as this is handled as standard with the controller where the parent page exists in the CMS.

    Now here is the code that i have in my books.php file, pay particular attention to the view() function.

    http://coedit.me/doc/L2hu1=7BV0kg=n0X

    My problem is that, whilst the page in question (http://torinet.co.uk/ldnsecrets/books/view/1) loads in the template, the top level menu does not function. What have I done here, or missed rather, to prevent this from appearing... You'll also see the browser title loses the $SiteConfig.Title too.

    Please help, this has been fussing me for many days.

  • George Botley
    Avatar
    Community Member
    16 Posts

    Re: Big issue for me, - renderWith Link to this post

    Bump..

    Please help...

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Big issue for me, - renderWith Link to this post

    Your view() method needs to either return a response directly or an array of properties for the template.

    For example

    public function view() {
    $id = $this->request->ID;

    if($id && ($book = Book::get()->byId($id))) {
    if($book->canView()) {
    return array('Book' => $book);
    }
    else {
    return Security::permissionFailure();
    }

    return $this->httpError(404);
    }

    You can then use $Book in your template.

  • George Botley
    Avatar
    Community Member
    16 Posts

    Re: Big issue for me, - renderWith Link to this post

    Willr,

    Thanks for your input, but that didn't solve my question,

    If you take a look here...

    www.torinet.co.uk/ldnsecrets - the menu appears...

    But if you take a look here...

    http://torinet.co.uk/ldnsecrets/books/view/1

    It doesn't.. This has happened for me whenever i've tried to use renderWith, on any site I've developed.

    What is causing this???

    Your provided code is merely an alternative to what I've already created.

    It's beginning to annoy me... What's the point of RenderWith if your menus don't appear?

    George.

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Big issue for me, - renderWith Link to this post

    Strange that the header and other template appears though. What does your template look like for the menu?

  • George Botley
    Avatar
    Community Member
    16 Posts

    Re: Big issue for me, - renderWith Link to this post

    Here's the menu code:

    <ul class="TopMenu">

       <% loop Menu(1) %>

       <li><a href="$Link" class="$LinkingMode" title="View the $MenuTitle Page">$MenuTitle</a></li>

    <% end_loop %>

    </ul>

    When i do renderWith('book') only the book template appears with no CSS styling or anything.. I have to pass it an array of page and book so it takes the base template of page and then using the layout template of book, is this correct?

    Also, if you look in the source code on the rendered page, it shows an li item for the menu, so it does look as though its going through the loop, although it terminates at list item 1, Furthermore, $SiteConfig.Title is not available in the <title> tags either...

    Any tips?

  • kinglozzer
    Avatar
    Community Member
    142 Posts

    Re: Big issue for me, - renderWith Link to this post

    Hi George,

    You're returning a Book DataObject, which is why 'Menu' isn't working - it doesn't exist on your Book DataObject. Rather than just returning a 'Book' object to render, you need to add it to the already existing data. I've made some amends to your view() method on your Controller that should work: http://coedit.me/doc/L2hu1=7BV0kg=n0X.

    Note that all your 'Book' code in your template will need to be inside a <% with Book %> tag or prefixed with $Book. For example:

    <% with Book %>
    Title: {$Title}
    <% end_with>

    Or:

    Title: {$Book.Title}

    Hope this makes sense,
    Loz

  • George Botley
    Avatar
    Community Member
    16 Posts

    Re: Big issue for me, - renderWith Link to this post

    Thanks Loz,

    So customise, if i'm right, add the data from an object to the pre-existing one?

    897 Views
Page: 1 2
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.