Logic/Presentation separation I have the following code:

&lt;div id="page-content"&gt;
&lt;div class="container-fluid"&gt;
    &lt;div class="row"&gt;
        &lt;div class="col-lg-11"&gt;
            &lt;div id="featured_users&gt;&lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row"&gt;
        &lt;div class="col-lg-11"&gt;
            &lt;h1&gt; Visitors &lt;/h1&gt;
            &lt;?php while($visitor = $visitors-&gt;fetch_object()) { ?&gt;
            &lt;?php $time = $visitor-&gt;time; ?&gt;
            &lt;?php $visitor = new User($visitor-&gt;viewer_id); ?&gt;
            &lt;?php 
            $this-&gt;insert('visitors/visitor',[
            'id' =&gt; $visitor-&gt;id,
            'profile_image' =&gt; $visitor-&gt;profile_image,
            'name' =&gt; $visitor-&gt;name,
            'time' =&gt; $time-&gt;convertToAgo($time);
            ]); 
            ?&gt;
            &lt;? } ?&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;


I've been trying to think of a way to better separate the logic from the presentation. I'm already using a template engine (Plates, native php templating), this code is from one of my template files. However, as it stands, it won't easy for anyone who has no understanding of PHP to edit my template file without getting confused with all of the PHP code.

Can anyone help me with ideas on the way to do this logic/presentation separation?
&lt;?php
    // this code goes into your controller
    $visitor_rows = [];

    // lets do all the hard work here
    while($visitor = $visitors-&gt;fetch_object()) {
        $time = $visitor-&gt;time;
        $visitor = new User($visitor-&gt;viewer_id);
        $visitor_rows[] = [
            'id' =&gt; $visitor-&gt;id,
            'profile_image' =&gt; $visitor-&gt;profile_image,
            'name' =&gt; $visitor-&gt;name,
            'time' =&gt; $time-&gt;convertToAgo($time),
        ]);
    }

    // the template only needs to know about $visitor_rows now
?&gt;


&lt;!-- the template is now a lot simpler to follow and understand --&gt;
&lt;!-- i prefer the foreach: endforeach; loop syntax when embedded in html --&gt;
&lt;div id="page-content"&gt;
    &lt;div class="container-fluid"&gt;
        &lt;div class="row"&gt;
            &lt;div class="col-lg-11"&gt;
                &lt;div id="featured_users"&gt;&lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="row"&gt;
            &lt;div class="col-lg-11"&gt;
                &lt;h1&gt; Visitors &lt;/h1&gt;
                &lt;?php foreach ($visitor_rows as $row): ?&gt;
                    &lt;?= $this-&gt;insert('visitors/visitor', $row) ?&gt;
                &lt;?php endforeach ?&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;