Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Kyle Barney
Kyle Barney
9,081 Points

PHP Code Showing Up As An HTML Comment Rather Than Being Executed

I'm working with the Twig templating language in Workspaces. Ever since I started using Twig the server has stopped executing the other PHP code included in my document. Instead it shows up as an HTML comment (viewable in the dev tools). Other than that, the Slim routing and Twig templating features appear to be working fine. I tried remaking the files to no avail. Any ideas on why this is happening and how to fix it?

1 Answer

Kevin Korte
Kevin Korte
28,111 Points

I had a suspicion that you can't use regular php in a twig template, because it's getting parsed by twig now, not php. My guess is when twig encounters php or unknown parts, it must put them in html comments so it doesn't error out the page or view.

A quick search confirmed I think I'm right, see if this helps you: http://twig.sensiolabs.org/doc/advanced.html

Kyle Barney
Kyle Barney
9,081 Points

Good call - that did the trick. Looks like once you start using Twig you have to use their way of expressing things rather than the standard PHP way - no mixing. So:

{{ text }}

rather than

<?php echo "text" ?>)
Kevin Korte
Kevin Korte
28,111 Points

You got it! Their docs have a examples for a few different things you might want to do. Happy coding!