php - Print array to template without template engine

883

I have a simple associative array in PHP containing data I like to print out in a format specified by a HTML template. The template would look something like this:

<html><head>Some header</head>
<body>
<b>{data1}</b>: {data2}

<b>{meta1}</b>: {meta2}

<b>{post1}</b>: {post2}

<i>Note</i>: {data1}, {meta1}, {post1} Some text

{data2}, {meta2} Some text
etc.
</body></html>

I wish to avoid using a template engine as much as possible since this is a simple program and I like to keep things simple.

569

Answer

Solution:

After some searching, I found the answer to my own question. In short, it is, use PHP's:

HEREDOC

and if I need to run any function within HEREDOC, it's possible too!

Thanks to the answers to these questions:

320

Answer

Solution:

I would recommend something like

 $tmpl_output = str_replace(['{{POST1}}','{{META1}}'],[$post1, $meta1],$template);

No template engine, very fast mechanism, and you have nice clean tokenization and markup.

People are also looking for solutions to the problem: php - Local Proxy not always working

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.