How to write templates for sfTemperPlugin
First we are going to learn how to parse a template file into a php file.
Assuming that we have two variables containing the path of the source (user template) and target (php version) files, named as $tpl and $php, if we know that paths are accessible, we can do it with one line only:
Temper::factory($tpl)->parse($php);
Template management system will be field for one more tutorial. Let's see the way to write templates with an example and it's compiled version.
At the moment there are defined three template tags:
- invoice
- item
- tax
And this is the example (template):
<html>
<head>
<title>Test</title>
</head>
<body>
<p>Lorem ipsum dolor sit amet.<br/><small>This is plain HTML from my .tpl template.</small></p>
<t:invoice get="number" />
<t:invoice get="issue_date" />
<t:invoice get="due_date" />
<t:invoice get="customer" />
<t:invoice get="customer_identification" />
<t:invoice get="customer_email" />
<t:invoice get="contact_person" />
<t:invoice get="invoicing_address" />
<t:invoice get="shipping_address" />
<t:invoice get="base" />
<t:invoice get="discount" />
<t:invoice get="net" />
<t:invoice get="taxes" />
<t:invoice get="gross" />
<t:invoice get="paid" />
<t:invoice get="due" />
<t:invoice get="terms" />
<t:invoice get="notes" />
<t:invoice get="items">
<t:item get="description" />
<t:item get="unitary_cost" />
<t:item get="quantity" />
<t:item get="discount" />
<t:item get="base_amount" />
<t:item get="discount_amount" />
<t:item get="net_amount" />
<t:item get="tax_amount" />
<t:item get="gross_amount" />
<t:item get="taxes">
<t:tax get="name" /> (<t:tax get="value" />)
</t:item>
</t:invoice>
<?php // This should be removed by Temper ?>
</body>
</html>
And this would be the compiled result:
<html>
<head>
<title>Test</title>
</head>
<body>
<p>Lorem ipsum dolor sit amet.<br/><small>This is plain HTML from my .tpl template.</small></p>
<?php echo $object ?>
<?php echo $object->getIssueDate(); ?>
<?php echo $object->getDueDate(); ?>
<?php echo $object->getCustomer(); ?>
<?php echo $object->getCustomerIdentification(); ?>
<?php echo $object->getCustomerEmail(); ?>
<?php echo $object->getContactPerson(); ?>
<?php echo $object->getInvoicingAddress(); ?>
<?php echo $object->getShippingAddress(); ?>
<?php echo $object->getBase(); ?>
<?php echo $object->getDiscount(); ?>
<?php echo $object->getNet(); ?>
<?php echo $object->getTaxes(); ?>
<?php echo $object->getGross(); ?>
<?php echo $object->getPaid(); ?>
<?php echo $object->getDue(); ?>
<?php echo $object->getTerms(); ?>
<?php echo $object->getNotes(); ?>
<?php foreach($object->getInvoiceItems() as $item): ?>
<?php echo $item->getDescription(); ?>
<?php echo $item->getUnitaryCost(); ?>
<?php echo $item->getQuantity(); ?>
<?php echo $item->getDiscount(); ?>
<?php echo $item->getBaseAmount(); ?>
<?php echo $item->getDiscountAmount(); ?>
<?php echo $item->getNetAmount(); ?>
<?php echo $item->getTaxAmount(); ?>
<?php echo $item->getGrossAmount(); ?>
<?php foreach($item->getInvoiceItemTaxs() as $tax): ?>
<?php echo $tax->getName(); ?> (<?php echo $tax->getValue(); ?>)
<?php endforeach; ?>
<?php endforeach; ?>
<!-- REMOVED -->
</body>
</html>
This was not a very pretty-looking example, I know; the rest of the process would involve customizing the template with HTML code, and compiling again. The idea for the template management system is to let the user upload her template in a .tpl file containing all the HTML and CSS code, compile the template and save it into an internal location not accessible from the outside.
You can render your template file this way from the action:
return $this->renderPartial('sfTemperPlugin/load', array('partial' => $php, 'object' => $object));
where partial key in the second argument is the path of the compiled template file, and the rest of keys are variables passed to the template, as we do with normal partials.

