Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Building Websites with PHP Slim Basics & Twig Templates Layouts with Twig

maximleguennec
seal-mask
.a{fill-rule:evenodd;}techdegree
maximleguennec
Full Stack JavaScript Techdegree Student 4,777 Points

Why using TWIG?? And what about : include and require ??? its more easy with... no??

For me its more logic to do this :

<?
require_once('public/php/header.inc.php');

$id = strtolower(strip_tags(trim($_GET['id'])));
switch ($id){
    case 'frais':
        $title = "some title";
        $data = "some data to change"
        include_once ('about.php')';
        break;
et etc...... 

require_once('public/php/footer.inc.php');?>

and in links just writing this code ===>

<a href="index.php?id=frais</a>

So i would like to know what do you think about it? is my version of code is old and nobody do this anymore? For me i have a lot of difficults to understand Twig and Slim...

3 Answers

Matt Trask
Matt Trask
10,027 Points

So the "require/include" functions of old, are that, old. We, PHP, have moved towards autoloading code via an autoloader. Look at PSR-4 for an example to what the PSR autoloading standards are. The PHP world is currently at this level. Using require should really only be used once, and that is to require your autoloader. Like you did with Slim, or if you have a bootstrap.php file.

As for twig, its currently the most used and standard for templating. PHP doesnt handle templating well natively, so the Symfony people wrote Twig. Since then, Smarty, Blade, and Plates have all come alive as an alternative to Twig. But Twig is one of the best tools Ive used as a developer.

Matt Trask
Matt Trask
10,027 Points

Also, looking at the "a href" line you added below the code, thats horrible practice, and is very open to SQL injection, which would allow your database to be attacked.

Alex Flores
Alex Flores
7,864 Points

Maximleguennec I'm kind of with you, as in I don't understand the importance of Twig. I get composer and Slim, but Twig just seems like added wait. I don't know. Maybe I'm missing something.