Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Symfony2 Essentials

You're reading from   Symfony2 Essentials

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781784398767
Length 158 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Templates – the View layer

Each MVC framework offers the View layer, and Symfony2 is no different. In Symfony1, we were using plain PHP code. Symfony has introduced a new templating engine named Twig. The Symfony syntax is more elegant — it's shorter and designed to be cleaner than regular PHP code in templates.

Note

We can still use it now. However, it's not the recommended approach, as in most cases, you will not find examples of a working bundle code in pure PHP.

Let's take a look at the template code within our bundle, src/AppBundle/Resources/views/Default/index.html.twig:

Hello {{ name }}!

Yes, this is it. As you can figure out, {{ name }} outputs the variable name. It is an equivalent of <?php echo $name; ?>. The code actually doesn't do much, and it is not a valid HTML code (it just outputs some text). Let's modify it a little:

{% extends '::base.html.twig' %}

{% block body %}
Hello {{ name }}!
{% endblock body %}

Now try to fire http...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image