title: Keeping presentation (HTML) and logic (PHP) separate
body: <p>I am trying to keep presentation and logic separate without using a template engine like Smarty.  What I have so far is working, but I am not sure how to do certain things without putting more PHP into my presentation than I would like to.  For example, right now I have something like this:</p>
  
  <h1>product_list.php</h1>
  
  <pre class="lang-php prettyprint-override"><code>    try {
          $query = $conn-&gt;prepare("SELECT p.id, p.name, p.description, IFNULL(url, title) AS url, GROUP_CONCAT(c.category SEPARATOR ', ') AS category,
              FROM products p
              LEFT JOIN product_categories pc ON p.id = pc.product_id
              LEFT JOIN categories c ON pc.category_id = c.id
              WHERE p.active = 1
              GROUP BY p.id");
          $query-&gt;execute();
          $result = $query-&gt;fetchAll(PDO::FETCH_ASSOC);
      }
      catch (PDOException $e) {
              echo $e-&gt;getMessage();
      }
  include('templates/product_list_tpl.php');
  </code></pre>
  
  <h1>product_list_tpl.php</h1>
  
  <pre class="lang-php prettyprint-override"><code>&lt;div class="card"&gt;
      &lt;div class="product-list with-header"&gt;
          &lt;div class="product-list-header center-align"&gt;
              &lt;h2&gt;&lt;?= $header_title; ?&gt;&lt;/h2&gt;
          &lt;/div&gt;
          &lt;?php foreach ($result as $row): ?&gt;
              &lt;!-- Some Product Info --&gt;
              Category:&amp;nbsp;&lt;?= $row['category']; ?&gt;
          &lt;?php endforeach; ?&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>In the above example some products will have one category, some will have multiple.  They display nicely in a comma separated list, but I would like to make the category names into links.  I know I can do something like below, but it seems messy to me.</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;div class="card"&gt;
      &lt;div class="product-list with-header"&gt;
          &lt;div class="product-list-header center-align"&gt;
              &lt;h2&gt;&lt;?= $div_title; ?&gt;&lt;/h2&gt;
          &lt;/div&gt;
          &lt;?php foreach ($result as $row): ?&gt;
              &lt;?php $categories = explode(', ', $row['category']); ?&gt;
              &lt;div class="product-list-item avatar"&gt;
                  &lt;img src="img/product/&lt;?= $row['id']; ?&gt;.jpg" alt="&lt;?= $row['title']; ?&gt;" class="square"&gt;
                  &lt;a href="product/&lt;?= generate_link($row['url']); ?&gt;" class="title bold"&gt;&lt;?= $row['title']; ?&gt;&lt;/a&gt;
                  &lt;p class="caption"&gt;&lt;?= $row['caption']; ?&gt;&lt;/p&gt;
                  &lt;div class="item-bottom"&gt;
                      &lt;span class="responsive"&gt;&lt;?= $row['description']; ?&gt;&lt;/span&gt;
                      &lt;p&gt;
                          Category:&amp;nbsp;
                          &lt;?php foreach ($categories as $key =&gt; $category): ?&gt;
                              &lt;a href="category/&lt;?= strtolower($category); ?&gt;"&gt;&lt;?= $category; ?&gt;&lt;/a&gt;
                              &lt;?= (sizeof($categories) &gt; 1 &amp;&amp; $key == end($categories)) ? ',&amp;nbsp;' : ''; ?&gt;
                          &lt;?php endforeach; ?&gt;
                      &lt;/p&gt;
                      &lt;p&gt;
                          &lt;span&gt;Rating:&amp;nbsp;&lt;?= $row['rating']; ?&gt;&lt;/span&gt;
                          &lt;span class="right"&gt;Rated&amp;nbsp;&lt;?= $row['rated']; echo ($row['rated'] == 1) ? '&amp;nbsp;time' : '&amp;nbsp;times'; ?&gt;&lt;/span&gt;
                      &lt;/p&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;?php endforeach; ?&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Thank you in advance for any suggestions.  Also, if anyone has any input on the general separation format I used on the example code I would love to hear it.  I am just getting back into coding after an 8 year break.</p>
  
  <p>EDIT: Added missing <code>endforeach</code> and improved indentation on third code block as per @Devon suggested in comments.</p>
  
  <p>EDIT: I updated the third code block to include HTML I previously left out and added all PHP functionality needed to achieve the output I am looking for.  It works, but IMO doing it this way removes the little separation I had.  I now basically just have one file with my database call and another file with this mess.  I feel like I am not going in the right direction for proper business logic / presentation logic separation.</p>
  
  <p>Where am I going wrong?</p>