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

Nick Davies
Nick Davies
7,972 Points

PHP Development

I did the PHP development track and built a php website following "building websites with PHP" and then looked at connecting databases via the course "PHP & Databases with PDO".

I am now trying to collate all I have learnt from teamtreehouse.

Problem is I have created twig templates and I am now trying to create a 'gallery page' where the database stores all the links and information to the images and I do a foreach loop to create links which will then go to a dynamic page such as 'film.php' to display the results. Whilst trying to do this the php isn't being read correctly and when viewing the source code you are able to view the PHP code.

I tried this in a .php file and it worked but in my .twig files it does not execute correctly.

Please can you help me with this or can Randy Hoyt and Hampton Paulk explain why they choose to use each method instead of using the other persons method?

Basically I'm creating a website with everything on it that I am learning so I am able to see it in a live environment. I am now left with a variety of twig templates which seem pointless. As "Building a simple php application" shows this can be accomplished using .php files.

Sorry I could not help you however, ill tag Hampton Paulk and Randy Hoyt maybe they can help :)

Can you provide a sample of the code in question? PHP not parsing can sometimes be an issue with the syntax itself.

Also, if you try clearing out your PHP file and have a simple echo('TEST'); inside of it, does that still show in the html?

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

You can't put regular php into a twig file, as it gets parsed differently. If you want to use custom php inside a twig view, you need to follow the steps to do so in the Extending Twig docs found here: http://twig.sensiolabs.org/doc/advanced.html

Twig, is kinda like sass for css, or haml for html. It's goal is to allow you to write cleaner, more easily read and understood views.

Simple applications can be harder to see the benefit. When you get into bigger sites and more complex views, you'll appreciate how verbose twig is, and it separates view logic from view code. It's too much for a beginner to talk on at first though. Which is why Randy did not use it. Those course were aimed more for entry level.