The Template File

Step 8 – Outputting dynamic data in your template file

While still in the page editor from step 7, click the "View" link to see your page. Note that it still says "Happy planet" for type (rather than "Terrestrial planet") and "Millions of years" rather than "4.54 billion years". That's because the page is still being rendered with just the static data in it. We need to update the template file so that it recognizes the fields we added and outputs the values of those fields.

Edit /site/templates/planet.php and replace the static text in there with tags like this, replacing field_name with the name of the field:

<?php echo $page->field_name; ?>

If supported by your server, you may also use this shorter format which some people find easier to look at and faster to enter:

<?=$page->field_name?>

Here is the /site/templates/planet.php file updated to output the content of the page using tags like the above:

<html>
  <head>
    <title><?php echo $page->title; ?></title>
  </head>
  <body>
    <h1><?php echo $page->title; ?></h1>
    <h2>
        Type: <?php echo $page->planet_type; ?>,
        Age: <?php echo $page->planet_age; ?> years
    </h2>
    <p><?php echo $page->planet_summary; ?></p>
  </body>
</html>

After making these changes, save your planet.php template file. Now view your Earth page again. You should see it properly outputting all of the content you entered on the page, including "Terrestrial planet" for Type and "4.54 billion years" for age. Any changes you make from this point forward should be reflected in the output.

Next: More Pages »


  1. Introduction and First Steps
  2. Create a Template and a Page
  3. Setting up Custom Fields
  4. The Template File
  5. More Pages
  6. Conclusion

Comments

  • Max

    Max 4 years ago 43

    Great stuff Ryan.

    How about adding this as the next step: now that folks have learned how to build the template that will show individual pages for the earth, jupiter, etc it would be nice to show them how to output all the planet outputs on a single page as a list?

    Thanks!

    • Patrick t.

      Patrick t. 3 years ago 65

      Hi Max, i think you need to create some template specific for pages. I can assume from this tutorial, that template file created here is similar to a template for planet details. To get all planets, you need to create a planets.php template file, create a page that act as parent of single planet, and in this page template use a foreach loop with the $page->children array, to list subpages.

  • marc

    marc 4 years ago 33

    Thanks ryan for this easy and simple to understand tutorial.

    +1 for additional tutorial in displaying all the planets on a single page as a list

  • Sito

    Sito 4 years ago 43

    Hello, just new to this CMS, and after following the tuts here I get a 'Earth' page but the with the Title & Content above the navigation menu...
    And also a double title, can't figure this out...any advice please?

    • Dionys

      Dionys 3 years ago 11

      @SITO

      I had the same Problem like you.
      Im not shur if i understand and solve the problem correct, but it works at the end.

      If you use $content .= "".$page->title.""; and this for every planet Variable. It works. Look at _main.php there is the output of this $content var. Im not shure but at the install process i take not the beginner template. At readme.txt i read something about two posibilities to give the page out. The first is direct, i think this was used at this tutorial. The second way is complexer but mor scaleable. I think this is the reason and solution.

      Maybe somebody can correct me.

  • Christophe

    Christophe 3 years ago 62

    Nice tutorial and awesome CMS/CMF ;)
    Great work!!!

  • Marco

    Marco 3 years ago 33

    If we leave the _init and the _main template-references in place (see tab Files when editing template "planet"), wouldn't it be more in line with the Processwire way of thinking to work with the following content of the template file?

  • Ed Preston

    Ed Preston 2 years ago 13

    "Create a new file"?
    How?
    Where?
    I can do cut-and-paste real good, that's not a problem.
    But since this is a beginner level tutorial it would help *a lot* if you could be a little more specific. This "tutorial" assumes we beginners already know the how and the where, but if we did, we wouldn't be beginners

    • Ed Preston

      Ed Preston 2 years ago 00

      OK, commenting to my own reply: I gave up looking for a way to create the new file *in* ProcessWire, and just created it outside of ProcessWire through cPanel, since my site is hosted, and not on my own server.

  • Vik

    Vik 12 months ago 00

    Thank you Ryan!
    Perfect tutorial, very clean and simple
    :)

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.