T
T
themailvnk2014-07-10 21:38:16
Twig
themailvnk, 2014-07-10 21:38:16

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>

Here is the top15.php handler:
<?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());
}
?>

I do not know what is wrong and why it is not displayed, thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lemburov, 2015-02-01
@lemburov

item.desc like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question