so we are working on a system, where admin will set templates with variables name for different types of email like shipping, order placed etc and will be saved to database. we are getting email template from db so we have to render to properly use variables inside email template. can anyone please review my code.
<?php $ user = (array)json_decode($ user); //user basic info $ shipping_code = '567'; //shipping code function render($ setting, $ user, $ shipping_code, $ products) { //match values like {{$ user}} $ output = preg_replace_callback('~\{{(.*?)\}}~', function($ key) use ($ user, $ shipping_code, $ products) { $ products = (array)json_decode($ products); //products info $ multiple_products = ""; foreach($ products as $ product) { $ multiple_products .= $ product->name.'<br/>';//add products in text } $ variable['$ products'] = $ multiple_products; $ variable['$ user'] = $ user['name']; $ variable['$ shipping_code'] = $ shipping_code; return $ variable[$ key[1]]; }, $ setting); return $ output; } echo $ output= render($ setting, $ user, $ shipping_code, $ products); //call function ?>