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.

General Questions /

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

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


Reply


3 Posts   2027 Views

Avatar
NtM

Community Member, 39 Posts

18 November 2009 at 10:20am

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?

Avatar
NtM

Community Member, 39 Posts

18 November 2009 at 11:12am

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?

Avatar
NtM

Community Member, 39 Posts

19 November 2009 at 5:12am

Edited: 19/11/2009 5:13am

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