Fill text templates using arrays in php

Text templates are often used to generate content like notification emails, invoices filled with the details of a customer or user. The best example would be a bulk email program that sends out emails to multiple users filling the details of the particular user in every individual mail.

Text templates have place holders for fields which can be filled using an array or object anything similar.
Here is an example of a template.

$template = "
			Hello {{customer_name}}
			
			Thank you for making the purchase of ${{amount}}. 
			Your order shall be processed shortly.
			
			Regards
			SuperOnline Store
			";

That kind of an email might go out from an ecommerce app once the order of any customer is punched into the system. It might be an sms as well.

Now the values wrapped in {{ }} are the values that are dynamic and come from a database. They have to be filled every time a message is to be generated for a new customer or new order.

The biggest advantage of using such templates is that they are configurable from the application's admin area and do not require any coding skills for the administrator to edit. Applications also provide users with features to create their own templates and use them with database entities like users etc.

Filling text templates

So to fill them up with array here is a simple and short php function that uses preg_replace

function bind_to_template($replacements, $template) 
{
	return preg_replace_callback('/{{(.+?)}}/', function($matches) use ($replacements) 
	{
		return $replacements[$matches[1]];
	}, $template);
}

I got it here.

usage

$template = "
			Hello {{customer_name}}
			
			Thank you for making the purchase of $ {{amount}}. 
			Your order shall be processed shortly.
			
			Regards
			SuperOnline Store
			";

# Your template tags + replacements
$row = array(
	'customer_name' => 'Mike',
	'amount' => 400,
);

function bind_to_template($replacements, $template) 
{
	return preg_replace_callback('/{{(.+?)}}/', function($matches) use ($replacements) 
	{
		return $replacements[$matches[1]];
	}, $template);
}

echo bind_to_template($row, $template); 

So in the above example the template is filled with values from the variable $row. This $row could have been fetched from the database for example.

Here is an alternative function that is smaller and has a simpler syntax.







<?php

$template = "
			Hello {{customer_name}}
			
			Thank you for making the purchase of $ {{amount}}. 
			Your order shall be processed shortly.
			
			Regards
			SuperOnline Store
			";

# Your template tags + replacements
$row = array(
	'customer_name' => 'Mike',
	'amount' => 400,
);

function fill_template($template, $replacements)
{
	$final = preg_replace('#{{([^{}]+)}}#sie' , "$replacements['${1}']" , $template);
	
	return $final;
}

echo fill_template($template, $row);

This new function achieves the text replacement in just 1 line of code, and uses preg_replace instead of preg_replace_callback as in the earlier example.

There is a library called Mustache that can do such template filling. It supports many more features like using objects etc.

Last Updated On : 9th June 2013

Subscribe to get updates delivered to your inbox

Leave a comment