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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Transferring post data between templates


Go to End


6 Posts   989 Views

Avatar
Yokelassence

Community Member, 15 Posts

24 May 2012 at 2:17am

Hi, I'm starting out on Silverstripe and for starters I want to do something simple: construct a form on one template (call it menu.ss) then display the results you choose on another template (call it order.ss) but I'm having trouble figuring this out

My form is standard fare:

        $fields = [array of fields]
         
        $actions = new FieldSet(
            new FormAction('order', 'Place Order')
        );

        return new Form($this, 'order', $fields, $actions);

I thought I could use this to load a separate template for the forms post data but this just gives me a blank screen

    function order($data, $form) {
        return $this->customise(array('data' => $data))->renderWith('order');
    }

I'm sure there is a simple way to get Silvertripe to display post data to a seperate template. What have I overlooked?

I searched beforehand but I couldn't find a viable answer

Avatar
Willr

Forum Moderator, 5523 Posts

26 May 2012 at 8:04pm

You may need to use an ArrayData object for returning data to the template.

function order($data, $form) {
return $this->customise(new ArrayData(array(
'Data' => $data
)))->renderWith('order');
}

Then you can use $Data.Something in the template or to simplify

function order($data, $form) {
return $this->customise(new ArrayData($data))->renderWith('order');
}

Avatar
Yokelassence

Community Member, 15 Posts

29 May 2012 at 8:38pm

Edited: 29/05/2012 8:45pm

Thanks for the reply Willr

I gave the Arraydata object a shot, following your example. Though upon execution it seems another issue has cropped up:

[Warning] Missing argument 2 for Menu_Controller::order(), called in C:\xampp\htdocs\ss_testing\sapphire\core\control\Controller.php on line 193 and defined"

I'm not sure what why it is suddenly complaining about the $form variable in "function order($data, $form)". The form follows the Silverstripe form tutorials and they use the same syntax I am using.

I tried shortening the function to "function order($data)", this resulted in a different error:

[User Warning] None of these templates can be found in theme 'blackcandy': order.ss

This comes despite the order.ss template being in the layouts folder and is used without problem by another page.

Did I miss something?

Avatar
Yokelassence

Community Member, 15 Posts

31 May 2012 at 2:55am

Never mind this thread contained the answer I was looking for in regards to that bug I came across

        return new Form($this, 'order', $fields, $actions);

Needed to be

        return new Form($this, 'createsubmit', $fields, $actions);

Now the order template appears and a little test code displays the post data on it.

However only the HTML on the template is returned, I lost the rest of the theme. It seems that "RenderWith" quite literally renders only the template you give it and nothing else.

I'm sure I don't need to include all the headers and footers inside the order template, is there a way to tell silverstripe to load the whole theme and only change the page of the theme?

If not, perhaps I will need to use AJAX

Avatar
Willr

Forum Moderator, 5523 Posts

31 May 2012 at 5:31pm

renderWith() takes an array of template names to build up it's view so you can get it to fallback like renderWith(array('Page_Order', 'Page'))

Avatar
Yokelassence

Community Member, 15 Posts

2 June 2012 at 7:54pm

"renderWith(array('Order', 'Page'));" was the correct solution. So far so good.

I should add that one thing that added to the confusion was leaving a duplicate of the Order template inside the includes folder. That preventing RenderWith from finding the order template inside the layouts folder. Deleting the duplicate fixed that quickly. So for those reading this...yea...don't do that.