Forms are a good way to get information from the user. Writing form and form elements in HTML can sometimes be cumbersome, especially when you want to be XHTML-compliant. You could include form elements in symfony templates the usual way, as shown in Listing 4-8, but symfony provides helpers that make this task easier.
Listing 4-8 - Templates Can Include Usual HTML Code
<p>Hello, world!</p> if ($hour >= 18): <p>Or should I say good evening? It is already echo $hour .</p> endif; <form method="post" action="/myapp_dev.php/mymodule/anotherAction"> <label for="name">What is your name?</label> <input type="text" name="name" id="name" value="" /> <input type="submit" value="Ok" /> </form>
A helper is a PHP function defined by symfony that is meant to be used within templates. It outputs some HTML code and is faster to use than writing the actual HTML code by yourself. Using symfony helpers, you can have the same result as in Listing 4-8 with the code shown in Listing 4-9.
Listing 4-9 - It Is Faster and Easier to Use Helpers Than to Use HTML Tags
<p>Hello, world!</p> if ($hour >= 18): <p>Or should I say good evening? It is already echo $hour .</p> endif; echo form_tag('mymodule/anotherAction') echo label_for('name', 'What is your name?') echo input_tag('name') echo submit_tag('Ok') </form>
Note that the use of the short opening tags (
<?=, equivalent to
<?php echo) is not recommended for professional web applications, since your production web server may be able to understand more than one scripting language and consequently get confused. Besides, the short opening tags do not work with the default PHP configuration and need server tweaking to be activated. Ultimately, when you have to deal with XML and validation, it falls short because
<? has a special meaning in XML.
Form manipulation deserves a whole chapter of its own, since symfony provides many tools, mostly helpers, to make it easier. You will learn more about these helpers in Chapter 10.