Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Transferring post data between templates

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

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

Page: 1
Go to End
Author Topic: 557 Views
  • Yokelassence
    Avatar
    Community Member
    15 Posts

    Transferring post data between templates Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Transferring post data between templates Link to this post

    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');
    }

  • Yokelassence
    Avatar
    Community Member
    15 Posts

    Re: Transferring post data between templates Link to this post

    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?

  • Yokelassence
    Avatar
    Community Member
    15 Posts

    Re: Transferring post data between templates Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Transferring post data between templates Link to this post

    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'))

  • Yokelassence
    Avatar
    Community Member
    15 Posts

    Re: Transferring post data between templates Link to this post

    "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.

    557 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.