Jump to content

Template & template file creation


mtn
 Share

Recommended Posts

I'm new to PW -- downloaded and went through tutorials -- and need advice in best approach to template creation.  I want to get started on the right track.  The website will consist of approx. 300 how-to articles, a list of which (with thumbnail image) is to be displayed on category pages (main menu - 6 categories) under various subtitles (not part of menu).

CATEGORY1

  -- Subtitle1:

    ---- Article1

    ---- Article2

  -- Subtitle2:

    ---- Article3

    ---- Article4

  -- Subtitle3:

    ---- Article5

    ---- Article6

CATEGORY2

  . . .

About 10% of the articles  need to be listed under multiple categories.  In creating Article template & template files for delayed output pages, what would it be the optimal way to organize template and template files?  (A. having two separate template/template files for single and multiple category articles -- simply placing the single-category articles in their respective admin hierarchical page tree and doing what with multiple-category pages?,  or B. -- having a single template for all pages that would have 3 category fields (and their respective subtitle dropdown selection fields), using only the first category field for single-category pages?, or C. something else.)

What would the resulting admin hierarchical page tree be?

What modules would be helpful in creating this site?

Thank you!

Link to comment
Share on other sites

I'm working on something like this. Here's how I did it so far.

Templates x 4:

/tutorials/ (blog-index)
/tutorials/tutorial-title/ (blog-entry)
/tags/ (tag-index)
/tags/tag-name/ (tag-entry)

Tree structure:

tree.thumb.png.c8b815392a2afd0f0479b1e61d576096.png

blog-entry has a multiple page ref field called 'tags'...

2.thumb.png.a3c2f0fa8fc1fcb6977510bbc24050cb.png

...like so:

4.thumb.png.28764eb1d37454a72ff12d3abc49b6cf.png

tag-entry has a multiple page ref field called 'blogPosts'...

tag-entry.thumb.png.509a59d09c6050d889c2f147eb86929a.png

...like so:

5.thumb.png.488c70bcc1a994487d60f77bb2005bae.png

Using:

https://modules.processwire.com/modules/connect-page-fields/

(this module has been very very useful so far, written by @Robin S and recommended to me by @abdus)

The fields are connected thus:

1.thumb.png.cf40f84b5e9ca04b58298ca95cacedb0.png

So when I create a tutorial (blog-entry template), I can select a bunch of tags (or create new ones):

tutorial.thumb.png.6196c3dca88ba10e0bd9e7ef7ee25967.png

...the page created with 'blog-entry' is automatically added to the associated tags in the 'blogPosts' page ref field:

59e642088bb80_ScreenShot2017-10-17at18_46_29.thumb.png.261248b498a719a838f615dce4311dc8.png

So now you have the posts listed under the actual tag, it's as simple as just iterating over the 'blogPosts' page ref field:

<?php namespace ProcessWire; ?>

<div class="container py-5">
  <div class="row">
  <?php
    $tags = $pages->get("/tags/")->children;
    foreach ($tags as $tag):
      if (count($tag->blogPosts)):
  ?>
  
  <div class="col-md-4 mb-5 px-md-5">
    <h2 class="display-4"><a href="<?= $tag->url; ?>"><?= $tag->title; ?></a></h2>
    <?php
      // return PageArray of all items in blogPosts ref field
      $posts = $tag->blogPosts;
      // sort the PageArray
      $posts->sort("-postDate");
      // only want first 3 items
      $posts = $posts->slice(0, 3);
      // loop through the 3 items
      foreach ($posts as $entry):
    ?>
    
    <p><a href="<?= $entry->url; ?>"><?= $entry->title; ?></a></p>

    <?php endforeach; ?>

  </div>

  <?php
    endif;
    endforeach;
  ?>

  </div>
</div>

Still not decided how to list them when there are multiple tags, but hope this gives you some ideas.

 

  • Like 3
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...