Advertisement
Guest User

twig

a guest
Apr 27th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1.  
  2. <?php
  3. // include and register Twig auto-loader
  4. include 'Twig/Autoloader.php';
  5. Twig_Autoloader::register();
  6.  
  7. // attempt a connection
  8. try {
  9. $dbh = new PDO('mysql:dbname=employedb1;host=localhost', 'test', 'test');
  10. } catch (PDOException $e) {
  11. echo "Error: Could not connect. " . $e->getMessage();
  12. }
  13.  
  14. // set error mode
  15. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  16.  
  17. // attempt some queries
  18. try {
  19. // execute SELECT query
  20. // store each row as an object
  21. $sql = "SELECT name, role, salary FROM employees";
  22. $sth = $dbh->query($sql);
  23. while ($row = $sth->fetchObject()) {
  24. $data[] = $row;
  25. }
  26.  
  27. // close connection, clean up
  28. unset($dbh);
  29.  
  30. // define template directory location
  31. $loader = new Twig_Loader_Filesystem('templates');
  32.  
  33. // initialize Twig environment
  34. $twig = new Twig_Environment($loader);
  35. //extension added
  36. $twig->addExtension(new Twig_Extensions_Extension_Text());
  37.  
  38. // load template
  39. $template = $twig->loadTemplate('employees.html');
  40.  
  41. // set template variables
  42. // render template
  43. echo $template->render(array (
  44. 'data' => $data
  45. ));
  46.  
  47. } catch (Exception $e) {
  48. die ('ERROR: ' . $e->getMessage());
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement