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 Converting an HTML Site to a PHP Micro Framework

Sam Deacon
Sam Deacon
2,650 Points

Newbie (silex/twig specific) questions

EDIT I now realise I was fundamentally incorrect in the way I was approaching twig (a templating engine, not so much a platform to inject raw php).

1- as above (using

{% include 'test.php' %} 

//test.php
<?php echo "hello"; ?>

doesn't seem to do anything). Fair enough

2- you can pass variables to a controller/route such as

$app->get('/hello/{name}', function ($name) {
    return "Hello, {$name}!!";
});

EDIT: to pass variables to a .php.twig templated page, it seems to like you passing some sort of associated array. you can do:

$app->get('/contact/{name}/{lname}', function($name, $lname) use($app) {
    return $app['twig']->render('contact.php.twig', array(
        'name' => $name,
        'lname' => $lname
    ));

});

//contact
<h3> My name is {{ name }} {{lname}}</h3>

or somesuch.

3- following on from part 1 using raw php - how would you say, include a basic mysqli object database connection.php + output SQL query, within a twig templated sitepage? EDIT --> still learning but I'm sure I'll get there!

I could be way off here, but I think that in a modern MCV website/app you'd want to put your sql connections in your model. Twig seems like more of a front end template system for your view files and suchlike. If what I am saying doesn't make sense do the Laravel basics course that Mr Paulk teaches. As an added bonus you'll understand why you'd bother converting a website into a framework like Silex at all.

https://teamtreehouse.com/library/laravel-4-basics

(caveat silex isn't technically a MVC framework, but a lot of the logic will carry over such as separation of your sql from your other files)