質問

I'm getting to grips with the Silverstripe framework and I've come across a strange error.

Say for example I want to create a new 'membership' page. Within mysite/code I have set up a membership.php page as follows:

class Membership extends Page {

}

class Membership_Controller extends Page_Controller {

}

Then I have created a membership.ss file within my templates/layout folder with some test output. I then do a dev build and create a page in the CMS of type 'membership'. On the front end if I click the new page form the nav bar membership I don't see the test text so it seems that the template is not being read?

Any ideas?

Thanks.

Alan.

役に立ちましたか?

解決 3

I found the answer to the problem.

My .php was missing the following:

function getInfo() {
return $this->renderWith('Media');
}

ithout this the Media.ss file will not be used! Hopefully this will help other who might be getting to grips with SS!

他のヒント

There are several common pitfalls regarding templates:

  • how flushing works has changed several times in the past versions.
    I will not explain the details here, as those are prossibly subject to change soon again.
    However there are 2 things in the current version (3.1) that is of relevance here:

    1. /dev/build does NOT flush at all
    2. /dev/build?flush=1 does ONLY flush manifest and config (NO templates)

    (dev build does not use the template, so there is no flushing the template performed)
    this means that you have do do a ?flush=1 on a normal page, not just on dev/build

  • The Template file has to be named exactly like the class (I think its case sensitive)
  • check that the template file is not overwritten by another template file in another location. (eg if you have moduleName/templates/Foo.ss and themes/simple/templates/Foo.ss than the template of the theme will overwrite the module template
  • make sure the template is not empty (this causes an error in SilverStripe, at least in version 3.1)
  • Actions on a Controller can overwrite template ussage. here some examples:

    // this will not use a template at all, it will just print "some string"
    public function index() { return "some string"; } 
    
    // this will not use a template at all, it will output an empty string
    public function index() { return; } 
    
    // this will use template named "Bar.ss"
    public function index() { return $this->renderWith(array('Bar')); } 
    

SilverStripe also provides a debug option to see what templates are used. you can active it by 2 ways:

  1. set source_file_comments in your yml config:

    SSViewer:
      # display template filenames as comments in the html output
      source_file_comments: true
    
  2. use the "URL Variable Tools": just add ?showtemplate=1 when viewing your website

when enabled, see the HTML source (CTRL+u in firefox) of the page
silverstripe will add comments to let you know what templates are used.

Make sure your class has a Page_Controller extension declared and named correctly. I recently had this issue. The page controller extension had a typo, so the template file was not being used.

So for example, if your page class is RidiculouslyNamedPage

class RidiculouslyNamedPage extends Page {

}

class RidiculouslyNamedPage_Controller extends Page_Controller {

}

Then in your themes/[theme-name]/templates/Layout/ folder you would have your RidiculouslyNamedPage.ss.

If you misspell RidiculouslyNamedPage_Controller the template will not get called.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top