Answer the question
In order to leave comments, you need to log in
How to loop data from a database into a Twig template?
Hello, Amshu from scratch site and decided to use the Twig template engine.
Here is the template, main.tmpl:
<html>
{% for item in items %}
{{ desc }}
{% endfor %}
</html>
<?php
include_once ("settings/db.php");
$query1 = mysql_query("SELECT * FROM ".$db_prefix."sites LIMIT 15");
$items = mysql_fetch_assoc(
'desc' => $description = $items[description],
'url_s_t' => $url_s_t = $items[link],
'title_s_t' => $title_s_t= $items[title]
);
include 'lib/Twig/Autoloader.php';
Twig_Autoloader::register();
try {
$loader = new Twig_Loader_Filesystem('tmpl');
$twig = new Twig_Environment($loader);
$template = $twig->loadTemplate('view_sites.tmpl');
echo $template->render(array (
'top15' => $items
));
} catch (Exception $e) {
die ('ERROR: ' . $e->getMessage());
}
?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question