I’ve got a simple (but not tiny) template for some HTML, complete with inline variables. I’d like to pull that out as a separate file, and have the ability to switch in other template files. Is there a way to load a file into a string, but have it process inline variables?
Eg:
$thing="complete sentence";
$test=<<<END
This will get parsed as a $thing.
END;
echo $test; // This will get parsed as a complete sentence.
What I want is something like this:
// "test.html"
<html>
<body>
<p>This will get parsed as a $thing.</p>
</body>
// "index.php"
$thing="complete sentence";
$test=file_get_contents("test.html");
echo $test; // This will get parsed as a complete sentence.
How do I achieve this, preferably without a templating library?
You can use
includeto simply load the file as if it were part of the calling code.If you cannot
includefor some reason, you can read the file contents andevalit.UPDATE:
As pointed by NikiC, your file test.html doesn’t have valid PHP. You would have to change it so
includecan work. Your test.html should have this content:And
evalwould not work with this code, as this is not pure PHP code, it is HTML code with PHP inside it. If your included file has just PHP code, it would work fine.