Fill text templates using arrays in php
Fill text templates using arrays in php
Text templates are often used in various applications when generating 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. So how does a template look like. Here is a simple example ...
$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 the ecommerce platform of a store 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 admin area and do not require any coding skills for the administrator to edit.
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);
}
Source
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 pregreplace instead of pregreplace_callback as in the earlier example.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.