Reputation: 67
I have a html template and I want to append some dynamic data having array.
I am able to parse data with simple placeholder but When it comes to add array in placeholder, I am not able to find any solution , how to do that. I found lots of questions asked like this but they were for simple parsing doesnot include array in that.
<?php
class TemplateParser {
protected $_openingTag = '{{';
protected $_closingTag = '}}';
protected $_emailValues;
protected $_templateHtml = <<<HTML
<html>
<body>
<h1>Account Details</h1>
<p>Thank you for registering on our site, your account details are as follows:<br>
First Name: {{fname}}<br>
Last Name: {{lname}} </p>
<p> Address: {{address}}</p>
<ol>
{{transaction}}
<li> {{sn2}} - {{desc}} - {{amount}}</li>
{{transaction}}
</ol>
</body>
</html>
HTML;
public function __construct($emailValues) {
$this->_emailValues = $emailValues;
}
public function output() {
$html = $this->_templateHtml;
foreach ($this->_emailValues as $key => $value) {
$html = str_replace($this->_openingTag . $key . $this->_closingTag, $value, $html);
return $html;
}
}
$templateValues = array(
'fname' => 'Rajesh',
'lname' => 'Rathor',
'address' => ' G-8/55',
'transaction'=>array(
0=>array(
"sn2"=> 1,
"desc"=> "row 1",
"amount"=> "RS.1234"
),
1=>array(
"sn2"=> 2,
"desc"=> "row 2",
"amount"=> "RS.12345"
)
)
);
$templateHtml = new TemplateParser($templateValues);
echo $templateHtml->output();
I am getting this result.
<html>
<body>
<h1>Account Details</h1>
<p>Thank you for registering on our site, your account details are as follows:<br>
First Name: Rajesh<br>
Last Name: Rathor </p>
<p> Address: G-8/55</p>
<ol>
Array
<li> {{this.sn2}} - {{this.desc}} - {{this.amount}}</li>
Array
</ol>
</body>
</html>
I want result like this
<html>
<body>
<h1>Account Details</h1>
<p>Thank you for registering on our site, your account details are as follows:<br>
First Name: Rajesh<br>
Last Name: Rathor </p>
<p> Address: G-8/55</p>
<ol>
<li> 1 - row 1 - RS.1234</li>
<li> 2 - row 2 - RS.123</li>
</ol>
</body>
</html>
Can you guys help me here to achieve this?
Thanks Rajesh
Upvotes: 3
Views: 385