微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用数组中的数据解析html模板?

如何解决如何使用数组中的数据解析html模板?


我有一个html模板,我想附加一些具有数组的动态数据。 我可以使用简单的占位符解析数据,但是在占位符中添加数组时,我找不到任何解决方案,该怎么做。我发现了很多这样的问题,但它们只是为了简单的解析而没有在其中包含数组。
<?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();

我得到了这个结果。

<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>

我想要这样的结果

<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>

你们能帮我实现这一目标吗?

谢谢 拉杰什

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。