Jump to content

Entire page content into another page section


Speed
 Share

Recommended Posts

I've spend couple of hours figuring out how to have entire page content  display into another page section for example...

Page 1 
-------------------------------   

   $page -> title;
   $page-> summary;
   $page->message;

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

into...

Page 5    
------------------------------------ 
<section>
  <!-- Other codes here-->
</section>

<section class="page1">  
   $page -> title;
   $page-> summary;
   $page->message;

</section>

<section>
  <!-- Other codes here-->
</section>
--------------------------------------

I've tried this code inside Page 5.

<section>  
  <h1><?php echo page->title; ?></h1>
  <p><?php echo page->summary; ?>
    <br>
    <?php echo page->message; ?> </p>
</section>

None of content show up on page 5 from page 1. I later realize $page->fields inside page 5 would only show fields on current page (page 5) not  from page 1. And certainly page 5 does not have page 1's fields.  Any suggestions and your help is truly appreciated.

Link to comment
Share on other sites

You've already understood that $page is always the current page. To output contents of another page, you have to retrieve that page first and store it into a variable. You can do that with $pages->get().

<?php
$page1 = $pages->get('/page1/');
/* must be the path to your page from home,
   e.g. simply '/' if you want to retrieve
   the home page itself
*/
?>
<section>  
  <h1><?= $page1->title ?></h1>
  <p><?= $page1->summary ?>
    <br>
    <?= $page1->message ?> </p>
</section>

As always, there are multiple ways to make things easier and more intuitive in PW, depending on the exact requirement. One important thing to understand about ProcessWire is that you store content in pages but define both structure and output in templates (forgive me if I'm being overly verbose here. I always try to point things out in detail for others stumbling upon a forum page later on with a similar question in mind). In most cases, you'll have multiple pages with the same template, and what additional content you want to show depends on the template. You may have a page 5, page 12 and page 34 that are similar and all need to include content from page 1 - in that case, you can give them all the same template and hard-code page 1 in the template like I did above.

Or they all need to include content from another page, but that page varies. Instead of defining a different template for each one, you add a page field to their template definition, choose the page to include in the page editor and, instead of retrieving a page explicitly, you simply use the properties of the page field as it will be a full-fledged page object.

<section>  
  <h1><?= $page->yourpagefield->title ?></h1>
  <p><?= $page->yourpagefield->summary ?>
    <br>
    <?= $page->yourpagefield->message ?> </p>
</section>

You can even go one step further and define your own PHP template for outputting a page's contents in a different context. There's a forum thread with good points and examples illustrating different ways of page and field rendering and the use of wireRenderFile. These open up tons of possibilities to organize your template structure, even allowing you to build flexible widget systems with very little code.

  • Like 7
Link to comment
Share on other sites

I can't compete with BitPoet's knowledge, but I'm just going to add here that, if you haven't yet already, you'd want to read over the documentation on ProcessWire's selectors. It'll help you understand the different ways you can retrieve Page objects from the system (using $pages->get() or $pages->find()).

Selectors:

http://processwire.com/api/selectors/

Obviously I'd recommend to read through the entirety of the documentation, but Selectors are probably (in my opinion) the most important thing to understand. :)

  • Like 2
Link to comment
Share on other sites

11 hours ago, Speed said:

<section>  
  <h1><?php echo page->title; ?></h1>
  <p><?php echo page->summary; ?>
    <br>
    <?php echo page->message; ?> </p>
</section>

 

As others have said, you have to retrieve the page first. Also, it might have just been a typo here, but don't forget the "$" before your variables!

  • Like 2
Link to comment
Share on other sites

@bitpoet: Thank you for taking time to answer. Retriving page worked! I had that retrieve page code at the first time but without adding fields <?= echo $page->summary ?> and etc which is why I thought it didn't work first time.  Actually, I thought retrieving page string would just would just grab entire page at once just like mirroring.   Yes, I've went through these example with links you have suggested... I got really more confused after trying string from this  link Here's the code...

<?php
   $getPages = $pages->get('/contact/'); /*from: contact page*/
   $renderPage = $getPages->render('/');/*to: home page (current)*/
   echo $renderPage; /*render contact page in home page */
?>

and I end up getting internal server error. I couldn't ID the problem with the code.  I hope this one you can help me out clearing this up for me. Thank you.

@BrendonKoz:  I can't tell you how many times I have read or went through entire document on selectors. I always end up struggling with it,  I've perform many tries and test by following document. I always face  server error or not getting any results, there was no way I could troubleshoot or ID them without explanation  unlike when I've  done it with JS and Ruby. Since Php isn't my strong area of expertise, Its not that I don't understand what those documents are saying, I just cannot connect the dots or get picture in my head when they aren't much of examples. No way I could know rules or proper way to write code from API documents. For example I didn't know I need to retrieve the page first in order to view other page contents in this page and add specific fields, type and value. I couldn't find any example or document in this forum or anywhere or at least, maybe I've missed it.  Without much of examples on API syntax or rules. I get lost or confused. I didn't have problem picking up syntax and API after I took tutorial written by Ryan Cramer, Joss Sanglier, and Ben Byfor, The step by step examples  are clear and after that I am able to understand API document while comparing to tutorial examples. What would be your suggestion for me to be able to understand how to write API syntax? Truly appreciated your advice, thank you.  

@thetuningspoon:  I am aware of syntax error... However the main error was not retrieving page. Thank you. 

 

Link to comment
Share on other sites

When you're on a development system, enable display_errors and html_errors and set error_reporting to E_ALL in php.ini. This way, you'll get a sensible output for most errors. If you still end up with an internal server error and your http server's error log isn't cluing you in, you can try bootstrapping PW from the command line and rendering the page that causes the error from there.

<?php
// Needs to reside in PW's main directory where index.php is, otherwise
// the path to index.php needs to be passed to include().
include('index.php');
echo $pages->get('/your/faulty/page/')->render();

In your example, you use the Page::render function. Admittedly, it's a bit unfortunate that there isn't a more detailed description linked in the 3.x API docs for $page/Page. The (optional) first argument to that function is either the name of a field on the page (in which case it searches for a field template with a matching name) or the name / path (relative to the templates directory) of the PHP template to use. The '/' you pass here apparently doesn't do what you think.

<?php
   $contact = $pages->get('/contact/'); /* retrieve contact page*/

   // Renders output using the template file assigned in the backend
   // that $contact uses:
   $contactRendered = $contact->render();

   // Or: use a different PHP file than the template's default,
   // relative to site/templates. This call will look for a PHP
   // template site/templates/contact_simple.php
   $contactRendered = $contact->render('contact_simple.php');

   echo $contactRendered; /* output rendered contact page here */
?>

One little thing that makes development infinitely easier in the long run is to always be as explicit as possible when you name your variables. Try to avoid generic names like $getPages (unless you're really writing a generic function that deals with all kinds of pages), make them descriptive about what they contain and let singular and plurals forms tell you whether they hold a single instance or multiple. I've adapted the snippet of code accordingly.

  • Like 2
Link to comment
Share on other sites

@Bitpoet:  The error worked now, I am able to see it in PW when I encounter error. Now I am able to troubleshoot better. Now with these  $pages->get() and ->render Finally, I am seeing dots connecting together... The more I play around, the more I understand how it work. I didn't realize its not that difficult.  It just remind me a little bit of Javascript. API is getting clearer for me now after spending hour playing around with it. I wanted to thank you so much for your help, I truly appreciate it. 

I wish I knew how to add + to your reputation, I couldn't find anywhere in this fourm. Once again Thank you. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...