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.

Template Questions

Big issue for me, - renderWith


Go to End
Reply

11 Posts   921 Views

Avatar
George Botley

6 December 2013 at 5:18am Community Member, 16 Posts

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.

Avatar
George Botley

11 December 2013 at 10:55am Community Member, 16 Posts

Bump..

Please help...

Avatar
Willr

11 December 2013 at 8:25pm Forum Moderator, 5511 Posts

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.

Avatar
George Botley

12 December 2013 at 5:19pm Community Member, 16 Posts

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.

Avatar
Willr

12 December 2013 at 9:41pm Forum Moderator, 5511 Posts

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

Avatar
George Botley

13 December 2013 at 11:07am Community Member, 16 Posts

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?

Avatar
kinglozzer

14 December 2013 at 4:16am Community Member, 146 Posts

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

Avatar
George Botley

14 December 2013 at 4:18am Community Member, 16 Posts

Thanks Loz,

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

Go to Top