0

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.

PetaspeedBeaver
  • 433
  • 6
  • 17
  • This is a basic syntax error - you cannot embed php tags within a string declaration. Without understanding what you are trying to achieve, the answer by @ShaileshKatarmal is the best you are going to get – Steve Dec 14 '15 at 12:52
  • remove from anotherVaraible – balaraman Dec 14 '15 at 12:52
  • 1
    @PetaspeedBeaver it is still not realy clear what do you try to achieve. You can't mix HEREDOC with sources. If you want to put result of your code into a varialble - try to use `ob_*` - output buffers functions. – Gennadiy Litvinyuk Dec 14 '15 at 13:00
  • I have written an answer to elaborate. If your requirements are more complex than the example you provide, then there are other options, but i cant guess exactly what you need to do – Steve Dec 14 '15 at 13:00
  • You cannot have a string whose contents are evaluated as PHP just like that. This would require `eval`, which you should never use. There's always a better way to do whatever you're trying to do. But since we don't know what that is exactly, we can't really help you. – deceze Dec 14 '15 at 13:07
  • If my answer did not provide a solution, please explain why - im sure i can help, but not with the limited info you are providing. You edits really dont add anything. – Steve Dec 14 '15 at 13:15
  • @GennadiyLitvinyuk Yea, I've been experimenting a bit with output buffering while editing my question, and I think that might be a much better solution. – PetaspeedBeaver Dec 14 '15 at 13:32
  • @PetaspeedBeaver OK, well see the second example in my answer. It is the same thing Gennadiy mentions – Steve Dec 14 '15 at 13:37
  • Although you could just replace `` with `$title` as per my 1st example – Steve Dec 14 '15 at 13:38
  • Have a look at this http://platesphp.com/ or even this other question http://stackoverflow.com/questions/4977529/using-php-as-a-template-engine – Calimero Dec 14 '15 at 13:41

5 Answers5

1

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();
Steve
  • 20,703
  • 5
  • 41
  • 67
1

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>
321zeno
  • 1,264
  • 1
  • 12
  • 24
0
<?php echo "I whish this would work!" ?>

Replace with:

I whish this would work!
Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15
0

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();
Progrock
  • 7,373
  • 1
  • 19
  • 25
  • I wrote a tiny class that makes simpler to accumulate buffers. Called it Phonograph https://gist.github.com/fcamp/c4cb828cfb22bbe845cb – Francesco Dec 18 '15 at 11:22
-1

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;
deceze
  • 510,633
  • 85
  • 743
  • 889