#native_company# #native_desc#

Working with the Symfony Components PHP Libraries Page 2

By Octavia Andreea Anghel
on May 11, 2010


The Symfony Templating component provides all the tools needed to build any kind of template system. This component is a thin and flexible layer on top of PHP that provides simple but powerful features, covering most common templating needs.
It is centered on a main class, sfTemplateEngine, which provides the features that are common to all templating systems (inclusions, template inheritance, slots, helpers, and so on), and three main sub-components: template loaders, template renderers, and template helpers.
The template loader, implemented by the sfTemplateLoaderFilesystem class, is used to load templates from the filesystem. The template renderer, implemented by the sfTemplateRendererPhp class, renders plain PHP templates.
The application example below renders a template named index.php, which should exist in the current directory and list the HTML form.
Listing template.php:

require_once 'sfTemplateAutoloader.php';
$loader = new sfTemplateLoaderFilesystem('%name%.php');
$engine = new sfTemplateEngine($loader);
echo $engine->render('index', array('name' => 'Fabien'));

The index.php listing:

<title>HTML Form for uploading image to server</title>
<form action="" method="post">
<input type="file" name="pictures[]" >
<input type="submit" value="Send" >

The output of the template.php listing is:
The next application example renders the index2.php template (which is in the same directory with the template2.php PHP script) listed below:
The template2.php listing:


require_once 'sfTemplateAutoloader.php';

//$engine = new sfTemplateEngine($loader, array('php' => $renderer));
$loader = new sfTemplateLoaderFilesystem('%name%.php');
$engine = new sfTemplateEngine($loader, array('php' => new sfTemplateRendererPhp()));

echo $engine->render('index2', array('name' => 'Nume'));

The index2.php template is:


<TD><B>Phone Number</B></TD>


The output of the template2.php is:

Name   Surname Phone Number
Ionescu Bogdan    123456 
Stancu   George     567890


You can use helpers, the PHP functions used to return HTML code, in templates. Helpers save you from complex coding and make the process of writing templates very easy. Below is a list of several helpers that are available by default in every template; they don’t need declaration. These helpers are of the following helper groups:
  • Helper: Required for helper inclusion (The use_helper() function is, in fact, a helper itself.)
  • Tag: Basic tag helper, used by almost every helper
  • Url: Links and URL management helpers
  • Asset: Helpers populating the HTML <head> section, and providing easy links to external assets (images, JavaScript, and stylesheet files)
  • Partial: Helpers allowing for inclusion of template fragments
  • Text: Basic text helper
  • Form: Form input helpers
The line below turns all URLs and email addresses into clickable links using the TextHelper helper:

<?php echo auto_link_text('Please visit the website www.PHPBuilder.com to read interesting articles.') ?>

Please visit the website <a href=" www.PHPBuilder.com'"> www.PHPBuilder.com'</a> to read interesting articles.

The following application example presents a brief list of frequently used default helpers and the HTML code returned:
Listing using_helpers.php:

// Using a Helper named HN- using the HelperHelper helper
<?php use_helper('HN') ?>
<?php use_helper('HN1', 'HN2') ?>
// Creating a Tag group ?? using the TagHelper helper
<?php echo tag('input', array('name' => 'HN', 'type' => 'text')) ?>

// Alternative options syntax to list a tag
<?php echo tag('input', 'name=HN type=text') ?>  

<?php echo content_tag('textarea', 'helper name', 'name=HN') ?>
// Using an Url group ?? using the UrlHelper helper
<?php echo link_to('click', '/php/') ?>
<?php echo auto_link_text auto_link_text('Please visit website www.PHPBuilder.com to read interesting articles.') ?>

// Using the AssetHelper helper group
//Returns an <img> image tag for the asset given as argument
<?php echo image_tag('php_image', 'alt=mypicture size=640x400') ?>

//Returns a <script> include tag per source given as argument
<?php echo javascript_include_tag('my_js_script') ?>

//Returns a CSS <link> tag per source given as argument
<?php echo stylesheet_tag('style') ?> 

The output of the above helpers is:

=> <input name="HN" type="text" />
=> <textarea name="HN">helper name</textarea>
=> <a href="/php/">click </a> 
=> Please visit website <a href=" www.PHPBuilder.com'"> www.PHPBuilder.com'</a> to read interesting articles.
=> <img src="/images/php_image.jpg" alt="mypicture" width="640" height="400"/>
=> <script language="JavaScript" type="text/javascript" src="/js/my_js_script.js"></script>
=> <link href="/stylesheets/style.css" media="screen" rel="stylesheet"type="text/css" />

What Have You Learned?

In this article, you have learned the basics of working with the Symfony Components library: the YAML, Templating, and Event Dispatcher PHP libraries.
About the Author
Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book “XML Technologies — XML in Java” (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she’s interested in software architecture, web services, UML, and high-performance unit tests. to e-mail her.