Difference between revisions of "PHP115"

From mi-linux
Jump to navigationJump to search
Line 128: Line 128:
 
<pre>
 
<pre>
 
<?php
 
<?php
 
require_once 'Twig/Autoloader.php';
 
Twig_Autoloader::register();
 
  
 
// set-up environment without cache (for development)  
 
// set-up environment without cache (for development)  
$twig = new Twig_Environment($loader);
 
 
 
// Connect to server/database
 
// Connect to server/database
$mysqli = mysqli_connect("localhost", "bdtuser", "bdtuser", "bdtheque");
 
if (mysqli_connect_errno($mysqli)) {
 
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
 
}
 
  
 
// Run SQL query
 
// Run SQL query
Line 159: Line 150:
 
?>
 
?>
 
</pre>
 
</pre>
 +
 +
Result: [http://mi-linux.wlv.ac.uk/~in9352/twig/pets.php http://mi-linux.wlv.ac.uk/~in9352/twig/pets.php]

Revision as of 16:13, 12 July 2016

Main Page >> Web Application Development >> Workbook >> Template Engines

Please note that this is an advanced topic. Make sure that you have worked through all the previous sections of this workbook before proceeding.

Template engines

There are many PHP template engines out there, there are some of the most popular ones:

  • Twig - probably the best current, framework-independent template engine.
  • Mustache - also very popular, but very simple and so not suitable for more complex projects.
  • Smarty - very old, and the website looks very dated, but the product itself is very good and still actively maintained!
  • Blade - Blade comes with Laravel, a very popular PHP framework, and cannot easily be used without Laravel.
  • Volt - Volt is packaged with the Phalcon PHP framework, but can be used as a standalone product.

Here is an interesting comparison of the template engines above.

In this tutorial we will look at Twig

Installing Twig

Please read the installation notes on the official website.

Simple example

The template - index.html

<!DOCTYPE html>
<html>
    <head>
        <title>My first template</title>
    </head>
    <body>
        <h1>My Webpage</h1>
        
        {{ include('navigation.html') }}
        
        <p>{{ a_variable }}</p>
        <p>{{ another_variable|upper }}</p>
        
        <ul>
        {% for item in people %}
            <li>{{ item.FirstName }} {{ item.Surname }}</li>
        {% endfor %}        
        </ul>
        
    </body>
</html>

And the navigation.html template, included in the file above:

<ul id="navigation">
  <li><a href="#">Page 1</a></li>
  <li><a href="#">Page 2</a></li>
  <li><a href="#">Page 3</a></li>
</ul>

The home file - index.php

<?php

require_once 'Twig/Autoloader.php';
Twig_Autoloader::register();

// Set-up environment with cache (faster)
$loader = new Twig_Loader_Filesystem('templates');
/*$twig = new Twig_Environment($loader, array(
    'cache' => 'compilation_cache',
));*/

// set-up environment without cache (for development) 
$twig = new Twig_Environment($loader);

// Array of data
$people[0]['FirstName'] = "Alix";
$people[0]['Surname'] = "Bergeret";
$people[1]['FirstName'] = "Chris";
$people[1]['Surname'] = "Dennett";

// Load and render template
echo $twig->render('index.html', 
                   array('a_variable' => 'Alix', 
                         'another_variable' => 'Bergeret',
                         'people' => $people));



?>

The result: http://mi-linux.wlv.ac.uk/~in9352/twig/

Database example

The template - pets.html

<!DOCTYPE html>
<html>
    <head>
        <title>My pets</title>
    </head>
    <body>
        <h1>My pets webpage</h1>
        
        {{ include('navigation.html') }}
        
        <p>{{ num_rows }} record(s) were returned...</p>
        
        <table border=1>
        {% for item in results %}
          <tr>
            <td>{{ item.name }}</td>
            <td>{{ item.species }}</td>
          </tr>
        {% endfor %}        
        </table>        
        
    </body>
</html>

The main file - pets.php

<?php

// set-up environment without cache (for development) 
// Connect to server/database

// Run SQL query
$res = mysqli_query($mysqli, "SELECT name, species, age FROM pet");

// How many rows were returned?
$num_rows = mysqli_num_rows($res);

// Loop through resultset and put in array
while($row = mysqli_fetch_assoc($res)) {
  $results[] = $row;
}

// Load and render template
echo $twig->render('pets.html', 
                   array('num_rows' => $num_rows, 'results' => $results));


?>

Result: http://mi-linux.wlv.ac.uk/~in9352/twig/pets.php