I have to PHP files, one contains variables, some of which contains PHP code:
/* variables.php */
<?php
$oneVariable = "Hello";
$anotherVariable = "<<<EOF
<h1>Headline</h1>
<p>Text</p>
<?php echo "I whish this would work!" ?>
EOF;
And another one that I access:
/* output.php */
include("variables.php");
echo "$oneVariable World!";
echo "$anotherVariable";
When accessing output.php through a browser I will get:
Hello World!
Headline
Text
<?php echo "I whish this would work!" ?>
But I'd like the php code in the heredoc to be parsed so that I only see "I whish this would work!".
How can I accomplish that?
EDIT: Don't want to ruin the question by editing out my original example, so here's another one:
I'm want to be able to have one template file:
/* index.php */
<?php
include("article.php");
?>
<title><?php echo "$title" ?></title>
<?php echo "$articleContents" ?>
And a bunch of files containing data for articles, for example this one:
/* article.php */
<?php
$title = "About grapes";
$articleContents = <<<EOF
<h1>The wrath</h1>
<p>Even though the title of this article is <?php echo "$title" ?>,
what I'm personally really into is raisins!</p>
EOF;
When I request index.php
I want to get the "formatted" article.
<?php echo "I whish this would work!" ?>
Replace with:
I whish this would work!
This is just a syntax error, you cannot have php tags embeded in a string declaration. Presuming you do need some kind of logic to be used in the creation of the $anotherVariable
string, you just need to construct those parts before the declaration:
$oneVariable = "Hello";
$message = "I wish this would work!";
$anotherVariable = "<<<EOF
<h1>Headline</h1>
<p>Text</p>
$message
EOF;
Another option, if $anotherVariable
is to contain a more complex template, would be to use output buffering:
$oneVariable = "Hello";
//start output buffering
ob_start();
//now exit php mode, and define the template, inlcluding html and php interspersed
?>
<h1>Headline</h1>
<p>Text</p>
<?php echo "I whish this would work!" ?>
<p>Text</p>
<ul>
<?php foreach([1,2,3] as $el):?>
<li>item number <?php echo $el;?> </li>
<?php endforeach;?>
</ul>
<?php
//save output to variable
$anotherVariable = ob_get_clean();
There's no clean simple way to achieve what you want. The simplest thing is to keep the actual template in a separate file and include
it:
/* variables.php */
<?php
$oneVariable = 'Hello';
$template = 'foo.php';
/* output.php */
include 'variables.php';
echo "$oneVariable World!";
include $template;
Try this out:
function render($path, $data = ''){
foreach ($data as $key => &$value) {
//parse the contents of each variable
ob_start();
eval('?>' . $value);
$value = ob_get_clean();
}
//bundle variables with template
ob_start();
extract($data);
require $path;
$content = ob_get_clean();
return $content;
}
$title = "About grapes";
$articleContents = <<<EOF
<h1>The wrath</h1>
<p>Even though the title of this article is <?php echo "$title" ?>,
what I'm personally really into is raisins!</p>
EOF;
$data = compact('title', 'articleContents');
echo render('t.txt', $data);
And the template (t.txt):
<h1><?php echo $title; ?></h1>
<hr>
<article>
<p><?php echo $articleContents; ?></p>
</article>
An output buffer may be preferable to a HEREDOC.
<?php
$var_one = 'I wish you were here.';
ob_start();
?>
<h1>Running over the same old ground</h1>
<p>Something <?php echo some_func('blah'); ?> wicked this way comes.</p>
<?php
$var_two = ob_get_clean();
You could place the html in another file if needed:
ob_start();
include '/path/to/file.php';
$output = ob_get_clean();