0

I'm trying to set values from a mysql query to a template in Mustache. I'm using mustache.php Is it possible to do this? I can't find any documentation on templating queries. Does anyone have a better solution?

    <?php
/*
 * PHP Mustache - Basic Mustache example in PHP
 */
$servername = "localhost";
$database = "laravel";
$username = "root";
$password = "root";

try {

    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the PDO error mode to exception

} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

$query = $conn->query('SELECT * FROM users');

//register mustache library
require 'Mustache/Autoloader.php';
Mustache_Autoloader::register();


//set the template string (obviously complex application will use files)
$template = 'Hello, {{name}},<br /> Your last name is {{lastname}}';


//set the template values
$values = array(
    $query = $conn->query('SELECT * FROM users');
    'name' = $row['name'];
    'lastname' = $row['lastname'];
);


//start the mustache engine
$m = new Mustache_Engine;

//render the template with the set values
echo $row['name'];
echo $row['lastname'];
echo $m->render($template, $values);
?>
user2003341
  • 73
  • 1
  • 9
  • There's so much wrong with this code, that I don't know where to start. This is not a valid way to create an array! $row is never assigned. – Markus Müller Feb 11 '15 at 12:11
  • @MarkusMüller I've encluded the rest of my code. Can you tell me where I'm going wrong? I'm new to php – user2003341 Feb 11 '15 at 12:17

1 Answers1

0

Try it like this

$statement = $conn->query('SELECT * FROM users');
$row = $statement->fetch(PDO::FETCH_ASSOC);

//start the mustache engine
$m = new Mustache_Engine;
echo $m->render($template, $row);
Markus Müller
  • 2,611
  • 1
  • 17
  • 25