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.

General Questions

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

Confused About New Page Layout and How to Call it


Reply

8 Posts   2935 Views

Avatar
Sophie

24 November 2009 at 2:51am Community Member, 33 Posts

I understand how to create a new page template that uses template/layout/page.ss. However, I'm confused about how I edit the code for mysite/code/PageHome.php if I want it to use a new layout I've created called template/layout/PageHome.ss instead of the Page laout.

This is the code for my mysite/code/PageHome.php - how do I edit this to use the PageHome layout rather than the Page layout?

<?php
/**
* Defines the PageHome page type
*/

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

}

class PageHome_Controller extends Page_Controller {
   
}
?>

Thank you!

Avatar
Sparrowhawk

24 November 2009 at 4:07am Community Member, 33 Posts

It looks fine to me (I'm only a recent Silverstripe convert, so I might be missing something)

2 things:
- have you rebuilt the database using http://yoursiteURL/dev/build
- have you ensured that the page you want to use this new layout for is based on PageHome rather than Page? You can do this via the CMS, under the bahaviour tab, change the Page Type from "Page" to "Page Home"

I hope I've understood your question correctly! :)

Avatar
Sophie

24 November 2009 at 6:12am Community Member, 33 Posts

Thanks for your reply. I don't think I've explained myself very clearly. I know how to apply a template to a page in the CMS and all that fun stuff. What I'm not sure about is how to tell my new template that I want to use the layout: template/layout/PageHome.ss I created instead of the standard layout of template/layout/Page.ss.

I'm sure this is pretty straight forward but it escapes me!

Sophie

Avatar
banal

24 November 2009 at 7:31am Community Member, 901 Posts

A Layout file is usually a partial template. This saves you from the need to add the page header and all that stuff into all the templates. So a basic setup looks like this:
templates/Page.ss Contains global stuff like css includes, header, footer etc.
templates/Layout/Page.ss contains layout specific to the Page class
templates/Layout/SomePage.ss contains layout specific to the SomePage class

In templates/Page.ss you would place the $Layout variable where your layout should appear. The $Layout variable will then be replaced with the processed content of the Layout file that matches the current Page class.

Avatar
Sophie

24 November 2009 at 8:31am Community Member, 33 Posts

banal, thank you for replying. I am not doing a good job of asking my question. For my new HomePage template I do not want it to use the Page class at templates/Layout/Page.ss, but rather this: templates/Layout/PageHome.ss

Here's the code for my mysite/code/PageHome.php file. My question is, do I need to edit this code in any way so that it will use the new PageHome class I created at templates/Layout/HomePage.ss? Instead of templates/Layout/Page.ss? Or, does it know to use the PageHome class simply because it shares the the name?

Logic tells me that I should somehow edit the following so that my template will use templates/Layout/PageHome.ss. Or maybe I'm making this more difficult than it is?

<?php
/**
* Defines the PageHome page type
*/

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

}

class PageHome_Controller extends Page_Controller {

}
?>

Sophie

Avatar
banal

24 November 2009 at 9:14am (Last edited: 24 November 2009 9:14am), Community Member, 901 Posts

The templating system works the same as class inheritance.
The HomePage class uses the HomePage.ss template if available, otherwise it uses the template of the base class, in most cases Page.ss. The same is true for Layouts. So, yes: The template with the same name as the Class takes precedence over other templates. It does so automatically, you don't need to change/add anything in your code for that.

If you're using layouts, your page will always be composed of the main template (in the template folder) and a sub-template from the layout folder.

Example for templates/Page.ss:

<html>
   <head>
      <title>Example</title>
   </head>
   <body>
      <div id="Content">
         $Layout
      </div>
   </body>
</html>

Example for templates/Layout/Page.ss:

<p>
   Hello from Page
</p>

Example for templates/Layout/HomePage.ss:

<p><strong>
   Hello from the HomePage
</strong></p>

Calling a page of type HomePage will result in the following output:

<html>
   <head>
      <title>Example</title>
   </head>
   <body>
      <div id="Content">
         <p><strong>
            Hello from the HomePage
         </strong></p>
      </div>
   </body>
</html>

And Page will output the following html

<html>
   <head>
      <title>Example</title>
   </head>
   <body>
      <div id="Content">
         <p>
            Hello from Page
         </p>
      </div>
   </body>
</html>

You're also confusing classes with templates. The code in mysite/code/PageHome.php is the class definition. The files ending in .ss are template files.
This is also covered in Tutorial 1: http://doc.silverstripe.org/doku.php?id=tutorial:1-building-a-basic-site#using_a_subtemplate

Avatar
Sophie

24 November 2009 at 9:19am Community Member, 33 Posts

Thank you! I DID read the tutorial, but still wasn't clear on that one part. Thank YOU for taking the time to lay it all out so clearly.

All the best!
Sophie

Avatar
Reegan

24 January 2011 at 1:17am Community Member, 1 Post

Hi All.

Thank you for this post, i was stuck for about three hours, until i stumbled on this specific post, and from browsing the tutorial banal mentioned I figured out my problem within 2 mins..... Thanks Guys.

Just to update, maybe my solution will help someone else..... :-)

1. Create new HomePage.ss in theme folder example (.... Themes/blackcandy/template/HomPage.ss )
2. locate file called "CODE" (....mysite/code)
3. Create .php doc and insert the following code. then save the document as HomePage.php in (....mysite/code)

<?php
/**
* Defines the HomePage page type
*/

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

}

class HomePage_Controller extends Page_Controller {
   
}
?>

4. flush site using yoursitename/dev/build/?flush (www.yoursitename.com/dev/build/?flush)
This will prompt your database to re-collaborate/restart/refresh and build your new HomePage.ss

5. Checking if HomePage.ss template is visible.
Open up the cms, click on home page, then click on behavior.... if you see the "Change to Home Page" Well done.

Cheers
Reegan