Xujiajun Blog

About Blog Email GitHub

25 May 2014
Creating and Using Templates [第7天]

As you know, the controller is responsible for handling each request that comes into a Symfony2 application. In reality, the controller delegates most of the heavy work to other places so that code can be tested and reused. When a controller needs to generate HTML, CSS or any other content, it hands the work off to the templating engine. In this chapter, you’ll learn how to write powerful templates that can be used to return content to the user, populate email bodies, and more. You’ll learn shortcuts, clever ways to extend templates and how to reuse template code.


How to render templates is covered in the controller page of the book.


A template is simply a text file that can generate any text-based format (HTML, XML, CSV, LaTeX …). The most familiar type of template is a PHP template - a text file parsed by PHP that contains a mix of text and PHP code:

<!DOCTYPE html>
        <title>Welcome to Symfony!</title>
        <h1><?php echo $page_title ?></h1>

        <ul id="navigation">
            <?php foreach ($navigation as $item): ?>
                    <a href="<?php echo $item->getHref() ?>">
                        <?php echo $item->getCaption() ?>
            <?php endforeach; ?>

Til next time,
Xujiajun at 07:39


About Blog Email GitHub