3

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

rkrathor
  • 77
  • 7
  • There are existing [templating systems](https://twig.symfony.com/) out there, it might be best to switch to one of those. Otherwise, you are really just going to have to build the logic. You should probably have an opening and a closing syntax for complex structures. Right now you are using `{{transaction}}` to represent both the start and the end of a block. This is valid but also means you can't next an inner structure with the same name. Also, for each `key/value`, you can _try_ to inspect the type to guess some logic for scalar vs other. `this` is also something you'll need to figure out. – Chris Haas Nov 04 '20 at 15:02
  • 1
    I spent about 3 months building a basic templating system, it's the gift that just keeps giving as it were. You'll spend a long time uncovering problem after problem, error after error and having to bolt on something you haven't thought of. I'm not saying don't do this, it can be an interesting learning experience. But really, this wheel has been invented and streamlined that you'll probably spend more time on it than you really care about, and end up wishing youd done something more useful/interesting – imposterSyndrome Nov 04 '20 at 16:29
  • @ChrisHaas yes you are right , After first print, it don't print the second one because the placeholder no more exist. The templates you are talking about are in Twig and I am using simple core PHP than how can I use them? – rkrathor Nov 04 '20 at 17:38
  • 1
    @rkrathor, Yes, that specific solution was in Twig. If you want to use a battle-tested, well thought-out templating system that is very, very feature-rich without being too heavy, you'll just have to implement it, see their install instructions. If you have a lot of code using your existing templating system, introduce Twig as an alternative, either through a config or a different file extension or whatever. Like jameson2012 basically said, building a simple system is easy, adding complexity and debugging edge cases is where you'll spend most of your time. – Chris Haas Nov 04 '20 at 17:46
  • @ChrisHaas, The Idea here is just to create and html by parsing some tag, and this generated HTML to be sent to use in another application which is in other language. – rkrathor Nov 04 '20 at 17:59
  • I understand. Your options are to drop the feature, build the logic or use something that exists. As @jameson2012 said, many of us have built these already but they are usually specific to our projects/system so they probably wouldn't directly help you. I wish you luck on your code! – Chris Haas Nov 04 '20 at 18:03
  • @ChrisHaas thanks for your time and wish. – rkrathor Nov 04 '20 at 18:40

0 Answers0