SilverStripe: Subtemplates in Layout folder processed twice

From FVue
Jump to: navigation, search

Problem

We're having problems with this structure:

We've put a form inside a WidgetArea inside a subtemplate. The form sets a session variable which is cleared upon reading. When rendering the subtemplate, the session variable is always empty. This is how we noticed the subtemplate is processed twice. During the first process, the session variable is read ok, but cleared, so that upon displaying during the second process the session variable is empty...

Environment

  • SilverStripe-2.4.5

Solution

Move the subtemplate outside the `Layout' folder. (And do a ./dev/build to indicate the new location.)

I think what happened is this:

Because we put the subtemplate inside the templates/Layout folder, it appears that SSViewer.php is creating a $Layout variable on the parent template (./sapphire/core/SSViewer.php, lines 414-423):

// Makes the rendered sub-templates available on the parent item,
// through $Content and $Layout placeholders.
foreach(array('Content', 'Layout') as $subtemplate) {
	if(isset($this->chosenTemplates[$subtemplate])) {
		$subtemplateViewer = new SSViewer($this->chosenTemplates[$subtemplate]);
		$item = $item->customise(array(
			$subtemplate => $subtemplateViewer->process($item, $cache)
		));
	}
}

Both the calls `customise()' and `process()' however, cause the subtemplate to be processed - the effect being all subtemplate-variables/methods called twice.