0

I'm trying to make use of html templates in my project and it's working so far but to make the templates easier to use I would like to use {} to denote the field areas. Like this:

//tplCall.html
<div id="{CustID}">
    <div class="callname">{Name}</div>
    <div class="calladdress">{Address}</div>
</div>

The following works fine:

$tmpCall = file_get_contents("templates/tplCall.html");
$tmpdata = get_object_vars($thiscall);
$htmlout = str_replace(array_keys($tmpdata),array_values($tmpdata),$tmpCall);
echo $htmlout;

But obviously leaves the {} intact. I would like to do something like the following but I get an array to string error. How can I add the {} to the key portion before it gets sent to str_replace?

$tmpCall = file_get_contents("templates/tplCall.html");
$tmpdata = get_object_vars($thiscall);
$htmlout = str_replace("{" . array_keys($tmpdata) . "}",array_values($tmpdata),$tmpCall);
echo $htmlout;

1 Answer 1

1

Modify each element of array_keys($tmpdata) explicitly:

$keys = array_map(function($v) { return '{' . $v . '}'; }, array_keys($tmpdata));
// also there's no need to apply `array_values` 
// as `str_replace` does not care about keys.
$htmlout = str_replace($keys, $tmpdata, $tmpCall);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.