SilverStripe: Rendering a userforms page type in a template loop
Asked Answered
C

1

2

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 extension 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?

Comanchean answered 22/5, 2015 at 19:25 Comment(1)
Possible duplicate of Silverstripe Multiple Userforms on one pageNewborn
E
1

Did a quick and dirty test and it seems to work only when you have

$Form 

in the theme file

It wont replace the $UserDefinedForm as it´s not invoking the render with the index() that has the necessary scrips to replace $UserDefinedForm bit as far as I can tell.

Elseelset answered 26/5, 2015 at 14:53 Comment(4)
Actually what I did was I created a separate page template called BasicPage.ss and forced the Page class to render to that using the method above. That worked, but not all the javascript parts got included which tells me I am still doing it wrong.Comanchean
You can add those js inclusions manually for now to your main template github.com/silverstripe/silverstripe-userforms/blob/master/code/…Consent
why the javascripts are missing: we are just missing the necessary place on the template for SS to render the data or the more plausible thing: as the controller render is i invoked without the right the logical order: init / index it won't do all the necessary bits > find a way to do that OR do it the quick and dirtyway just add the js requirements manually if its just the userfom that you have to worry aboutConsent
I am pretty sure that this is all correct. The combination of a subclass and overriding the init() function with a test for something like is_a('UserDefinedForm') and the inclusion of the js FinBoWa linked above this can be made to work. I have not yet put it all to the test though.Comanchean

© 2022 - 2024 — McMap. All rights reserved.