Fill an HTML template with mysql data and append the item to the view with Javasript

Viewed 76

I'm struggling with a problem. I need to make an about us page for a website. I use a template for the page and i want to fill the template with MySQL data. I use a <script> tag around the whole <div> of the personcard. The page is empty when i do this. Here is my HTML code:

<script type="text/html" id="aboutUsTemplate">
<div class="container" id="aboutUsTemplateCard">
    <div class="row">
        <!-- Team Member 1 -->
        <div class="col-xl-6 col-md-6 mb-4">
            <div class="card border-0 shadow">
                <img id="personPicture" src="https://source.unsplash.com/TMgQMXoglsM/500x350" class="card-img-top" alt="">
                <div class="card-body text-center">
                    <h5 id="personName" class="card-title mb-0">Team Member</h5>
                    <div id="personJob" class="card-text text-black-50">Web Developer</div>
                </div>
            </div>
        </div>
    </div>
</div>
</script>



<div id="personLocation">

</div>

I want to add cards to the view filled with data from MySQL here is my javascript code:

addPerson(aboutUsView, personData) {
        const scriptTagAboutUsTemplate = $("#aboutUsTemplate", aboutUsView);
        const aboutUsTemplateTarget = $("#personLocation", aboutUsView);

        for (let i = 0; i < personData.length; i++) {
            const aboutUsTemplate = $(scriptTagAboutUsTemplate[0].text);
            const person = personData[i];


            aboutUsTemplate.find("#personName").text(person.name);
            aboutUsTemplate.find("#personJob").text(person.job);
            aboutUsTemplate.find("#personPicture").attr("src", person.picture);


            aboutUsTemplateTarget.append(aboutUsTemplate);
        }
    }

an issue I'm struggling with is that in the javascript line:

const aboutUsTemplate = $(scriptTagAboutUsTemplate[0].text); 

is being readed as undefined, so the whole page doesn't load. Does anyone know what my problem is?

1 Answers

You need create a template class, pass your HTML to it and fill variables with data from database. Read my example carefully to understand how do it.

class EmailTemplate
{

    protected $_file;

    protected $_data = array();

    public function __construct($file = null)
    {
        $this->_file = $file;
    }

    public function set($key, $value)
    {
        $this->_data[$key] = $value;
        return $this;
    }

    public function render()
    {
        extract($this->_data);
        ob_start();
        include ($this->_file);
        return ob_get_clean();
    }
}

// get $token from database
$template = new EmailTemplate('email-confirmation.php');
$template->set('id', $token);
$html = $template->render();

// echo $html or use it anywhere you want

email-confirmation.php is something like below:

<!DOCTYPE html>
<html>
<head>

  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <title>Email Confirmation</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style type="text/css">
  </head>
  <body style="background-color: #b8d6f4;">
  ....
  ....
 <td>
    <a href="https://www.your.site/user-verify.php?id=<?php echo $id; ?>">Confirm Account</a>
 </td>
  ....
  ....


  </body>
</html>

You must replace email-confirmation with your HTML template and set your desired values and fill it from database when you call it.

Related