Jump to:

22972 Posts in 11596 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Please help! How to pass parameters from one page to another?

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: 1911 Views
  • NtM
    Avatar
    Community Member
    39 Posts

    Please help! How to pass parameters from one page to another? Link to this post

    I'm trying to publish newsletters online (I create all the newsletters using Newsletter module)

    1. I added additional field called "Publish_Online" to the NewsletterType.php and Newsletter.php

    2. I created new page types called NewsletterHolder and NewsletterPage.

    3. In the NewsletterHolder I included a function:

    public function NewsletterTypes() {
          return DataObject::get("NewsletterType","");
       }

    to get all the data from the table NewsletterType from db.

    In the NewsletterHolder template I have this code:

    <% if NewsletterTypes %>   
          <% control NewsletterTypes %>
              <% if Publish_Online="true" %>
                   <% if DraftNewsletters %>
                      <% control DraftNewsletters %>
                          <% if Publish_Online="true" %>
                            <p>$Title<br>Date: $Created<br><a href="$baseURL/newsletter/?id={$ID}">Read</a></p>
                          <% end_if %>
                      <% end_control %>
                   <% end_if %>
                   
                   <% if SentNewsletters %>
                      <% control SentNewsletters %>
                          <% if Publish_Online="true" %>
                            <p>$Title<br>Date: $Created<br><a href="$baseURL/newsletter/?id={$ID}">Read</a></p>
                          <% end_if %>
                      <% end_control %>
                   <% end_if %>
              <% end_if %>
          <% end_control %>
        <% end_if %>

    4. Now when I click on the link <a href="$baseURL/newsletter/?id={$ID}">Read</a> a go to the page (with type "NewsletterPage").
    And I need to read this parameter id={$ID}? How to do that?
    Or there are other ways to pass parameters between pages?

  • NtM
    Avatar
    Community Member
    39 Posts

    Re: Please help! How to pass parameters from one page to another? Link to this post

    This what I found in newsletter module to preview newsletters.

    Parameter is being passed in this way:
    $previewLink = Director::absoluteBaseURL() . 'admin/newsletter/preview/' . $this->ID;

    And here is how $this->ID is processed:

    /**
        * Preview a {@link Newsletter} draft.
        *
        * @param HTTPRequest $request Request parameters
        */
       public function preview($request) {
          $newsletterID = (int) $request->param('ID');
          $obj = DataObject::get_by_id('Newsletter', $newsletterID);
          $templateName = ($obj && ($obj->Parent()->Template)) ? $obj->Parent()->Template : 'GenericEmail';

          // Block stylesheets and JS that are not required (email templates should have inline CSS/JS)
          Requirements::clear();

          // Set template specific variables before passing it to the template
          $obj->Body = $obj->Content;

          return $this->customise($obj)->renderWith($templateName);
       }

    How is that all connected?

  • NtM
    Avatar
    Community Member
    39 Posts

    Re: Please help! How to pass parameters from one page to another? Link to this post

    I know that I'm talking to myself here.
    But I discovered, that to pass parameters between page1 and page2 you need to define a function in the page1 controller,
    for example like in the newsletter module:

    public function preview($request) {
    $newsletterID = (int) $request->param('ID');
    $obj = DataObject::get_by_id('Newsletter', $newsletterID);
    return $this->customise($obj)->renderWith("page2");
    }

    and than the link like "http://www.mysite.com/page1/preview/12" inside "http://www.mysite.com/page1/"
    would go through that function "preview", and the parameter $request would be "12".
    Using renderWith("page2") in the function "preview" you can define a new page based on "themes/mytheme/Layout/page2.ss".

    -----------------------------------------------

    Please advise where to read about this process more? And did I understand everything correctly?

    thanks

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