Passing data to HTML template How can I pass data from database to my HTML template without putting PHP code in my HTML file.

Thanks.

This is my controller

public function index($aRequest)
{
    $oModel = new todoModel();
    $aTodoList = $oModel-&gt;getTodoList();
    return array('type' =&gt; 'view', 'view' =&gt; 'view/index.tpl', 'data' =&gt; 
    array('aTodoList' =&gt; $aTodoList));
}


my Model

public function getTodoList()
{
    $aTodoList = [];
    $this-&gt;oConnection-&gt;setAttribute(PDO::ATTR_ERRMODE, 
    PDO::ERRMODE_EXCEPTION);

    $sQuery = 'SELECT * FROM t_todolist';
    foreach ($this-&gt;oConnection-&gt;query($sQuery) as $aTodo) {
        array_push($aTodoList, array(
            'sequence'    =&gt; $aTodo['sequence'],
            'todo'     =&gt; $aTodo['todo']                
        ));
    }
    print_r($aTodoList);
    return $aTodoList;
}


my tpl -(this is working but it has php code inside)

&lt;div id="todo_list"&gt;
&lt;ul id="todo_list_ul"&gt;
&lt;?php
foreach ($aTodoList as $aTodo) {
?&gt;
    &lt;!-- list here --&gt;
    &lt;li&gt;&lt;?php echo $aTodo['todo'];?&gt;  &lt;a href="" 
class="todo_update"&gt;update&lt;/a&gt; &lt;a class="todo_delete" href=""&gt;delete&lt;/a&gt;&lt;/li&gt;
&lt;?php
}
?&gt;
&lt;/ul&gt;
&lt;button id="todo_create"&gt;create&lt;/button&gt;
&lt;/div&gt;
First, let me review some of of the code you have

public function getTodoList()
{
    return $this-&gt;oConnection-&gt;query('SELECT * FROM t_todolist')-&gt;fetchAll();
}


This is actually all the code you need for this task. Just one line. Thanks PDO being not an ordinary database driver like mysqli. You can check other PDO wonders in my article

There is also one important issue that should be fixed in the controller

public function index($aRequest)
{
    $connection = new Database; // whatever you have to supply a database connection
    $oModel = new todoModel($connection);
    $aTodoList = $oModel-&gt;getTodoList();
    return ['type' =&gt; 'view', 'view' =&gt; 'view/index.tpl', 'data' =&gt; 
    ['aTodoList' =&gt; $aTodoList]];
}


You may check the above article in order to learn why it is very important to create a database connection only once instead of making each model to create its own.

Now, to the template.

First off, there is absolutely nothing wrong with having some PHP code in a template. PHP is a template language after all, and a good one. All you need is to keep an eye on your template and make sure that all PHP code does in it is actually related to the output only. You can only make it a bit prettier and safer

&lt;ul id="todo_list_ul"&gt;
&lt;?php foreach ($aTodoList as $aTodo): ?&gt;
    &lt;!-- list here --&gt;
    &lt;li&gt;
       &lt;?= htmlspecialchars($aTodo['todo']) ?&gt;
       &lt;a href="" class="todo_update"&gt;update&lt;/a&gt; &lt;a class="todo_delete"&gt;
         delete
       &lt;/a&gt;
    &lt;/li&gt;
&lt;?php endforeach ?&gt;
&lt;/ul&gt;


However, to answer your question literally, you will need a dedicated template engine, like Twig. Twig has many good features compared to raw PHP. One of them is autoescaping. There is one critical flaw in your current template, namely 

&lt;li&gt;&lt;?php echo $aTodo['todo'];?&gt;


The moment a malicious user will put some bad Javascript in the todo description, your site will be hacked. So you have to escape HTML when echoing a variable. 

Unlike PHP, Twig escapes your values automatically, so a code 

&lt;ul id="todo_list_ul"&gt;
{% for aTodo in aTodoList %}
    &lt;li&gt;
        {{ aTodo.todo }}
        &lt;a href="" class="todo_update"&gt;update&lt;/a&gt; &lt;a class="todo_delete"&gt;
          delete
        &lt;/a&gt;
    &lt;/li&gt;
{% endfor %}
&lt;/ul&gt;


will be automatically protected from XSS