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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

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

Render Userforms Module In a Template Control or Loop


Go to End


4 Posts   1332 Views

Avatar
Babalou

Community Member, 21 Posts

26 May 2015 at 2:28am

Using SilverStripe 3.1 I have laid out a FrontPage page type that loops through its children to produce a big tall scrolling page. It has all kinds of different page types in it and I access their templates by creating their controllers on the fly by adding on to the Page class:

class Page extends SiteTree {

.....

function RenderAsChild($templateName = null) {
if(!$templateName) $templateName = $this->ClassName;
if(!$this->pageController){
$class = $this->ClassName . "_Controller";
$this->pageController = new $class($this);
}
return $this->pageController->renderForHolderPage($templateName);
}

and in the controller:

class Page_Controller extends ContentController {

....

function renderForHolderPage($templateName = null) {
if(!$templateName) $templateName = $this->ClassName;
return $this->renderWith(array($templateName));
}

In this way I can render pages and manage their templates and special features easily while still treating them all the same way in the template, something like:

<% loop $Children %>
<% if $ClassName = 'FancyPage' %>
$RenderAsChild
......

The thing is I want to use the userforms module this way but in the template in a loop or control it shows up just as a page. It does not appear to know anything about the form or the UserDefinedForm object.

Is there a way to get userforms to render as a child in a template?

Avatar
Babalou

Community Member, 21 Posts

26 May 2015 at 5:01am

The answer was that I had to get away from the Page template. This was rendering both the main Page.ss template and the Page.ss layout causing some confusion.
In fact the method i describe works I simply had to create a "BasicPage" layout and then force the page to render using that.

Avatar
Babalou

Community Member, 21 Posts

26 May 2015 at 10:35pm

Actually that didn't fully solve the problem.
It is coming back with a javascript error" "TypeError: $(...).validate is not a function"
Which tells me that I may not be including all the .js files.

Is there some published documentation for userforms? Or is there some better way of pulling this trick off?

Avatar
Nightjar

Community Member, 28 Posts

27 May 2015 at 12:49am

Edited: 27/05/2015 12:49am

It may need debugging, but something like this should let you define a Page_singlepage.ss in either main or Layout (overriding whichever you prefer for this task, even both).
It will print all vars, including controller types when you call something like <% loop $AllTheThings %>$Me<% end_loop %>

Nb: You may need to add 'forTemplate()' to Page_Controller (or relevant subclasses) for $Me to work. You could try instead $handleAction('index'), or find some other trigger the controller's default output method.

class CombinedPage_Controller extends Page_Controller {
	public function AllTheThings() {
		$pages = ArrayList::create();
		foreach(Page::get()->filter('ParentID'=>$this->ParentID) as $page) {
			$controller = ModelAsController::controller_for($page);
			$controller->templates = $controller->getViewer('singlepage')->templates();
			$pages->push($controller);
		}
		return $pages;
	}
}

Basically, use what's already available to avoid repeating code.