我试图将mysql查询中的值设置为Mustache中的模板。 我使用mustache.php是否可以这样做?我无法找到有关模板查询的任何文档。有没有人有更好的解决方案?
<?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);
?>
答案 0 :(得分:0)
尝试这样
$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);