-1

Let me rephrase this question. I'm trying to store the contents of an html file into a string. I would then like to have the php code in the html string convert the php to whatever the values I feed it later. I think the string interpolation might work. I might have over complicated it. But I think it would be interesting to still be able to use php tags for certain situations.

I would like to do something like this:

$str = 'some words';
$php = '<p><?= $str; ?></p>';
echo $php;

which would output to the DOM (source):

<p>some words</p>

or simply just on your browser screen

some words

What I get:

<p><!-- <?= $str; ?> --></p>

is this even possible?

I know the code above looks simple but this is just a simple case of the problem I am trying to solve.

<?php

// View
class View {

    private static $paths = [];

    private static function getTemplate($templatePath) {

        if (!array_key_exists($templatePath, self::$paths)) {

            // Filename must exist
            if (!file_exists($templatePath)) {
                die('File Doesn\'t Exist');
            }

            self::$paths[$templatePath] = file_get_contents($templatePath);

        }

        return self::$paths[$templatePath];

    }

    // Fill Template
    public static function fill($vars, $templatePath) {

        // Turn Vars into unique variables
        extract($vars);

        $input = self::getTemplate($templatePath);

        // Get View Output
        ob_start();
        echo $input;
        $output = ob_get_contents();
        ob_end_clean();

        return $output;

    }

    public static function fillWith($templatePath) {

    }

}
Jon49
  • 4,444
  • 4
  • 36
  • 73
  • Have you tried substituting < and > for < and > ? I'm not sure I understand your question. – coopersita Oct 30 '14 at 21:37
  • 3
    Am I missing something? This is pretty much the second step of every introductory PHP tutorial, right after "open your text editor". – ceejayoz Oct 30 '14 at 21:38
  • I fixed the question so you know where I'm coming from. And basic string interpolation doesn't work. Tried it. – Jon49 Oct 30 '14 at 21:44
  • @Jon49 What isn't working now? And where do you want to `output` a `String` in `p` tags? Because my answer and also Alex Shesterov answer should still work for you! Also do you use any `framework`? Because if your `echo` your `$php` variable the "normal" output should be: `

    = $str; ?>

    `
    – Rizier123 Oct 30 '14 at 21:55
  • @Rizier123, When echoing php code, php puts the code in comments per my updated question. – Jon49 Oct 30 '14 at 21:57
  • 1
    @Jon49 that isn't normal! Do you use a Framework? And where do you output now your $php variable in the posted Code?? – Rizier123 Oct 30 '14 at 21:58
  • @Jon49 See the link below the output of my answer machtes your requirements: http://ideone.com/VsRWw1! Your output what you get isn't normal, also i don't see where in your code your echo $php variable – Rizier123 Oct 30 '14 at 22:00
  • I don't currently use a framework. I'm an advanced student learning php and was just playing around with different ideas. I thought it would be cool to have my html in its own file then be able to reuse the template without calling includes every time (since they can get expensive apparently). – Jon49 Oct 30 '14 at 22:01
  • @Jon49 use my code snippet in my answer this should work! – Rizier123 Oct 30 '14 at 22:03
  • @Rizier123,it won't work because I have the php code in a string variable. – Jon49 Oct 30 '14 at 22:04

2 Answers2

2

Use string interpolation:

echo "<p>$str</p>";

Note the double-quoted syntax ("..."). Variables are not substituted in PHP if the string is single-quoted ('...').


As for your updated question

If you obtain the template string from external source, then PHP's string interpolation won't work (it would be a huge security risk if it would).

You may use regular expressions to replace occurrences of specific patterns.

Or use a template engine like Twig. It has more power than you need currently and requires some learning, but it's future-proof in case that you need more complex features sometime.

You may also make your template file a PHP script and include it (instead of file_get_contents). + Define the variables before the inclusion. Then PHP's usual string interpolation would work. But I do not recommend you to do it this way. It's not readable and introduces a potential security risk.

See also this question.

Community
  • 1
  • 1
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • I updated the question so you can understand where I am coming from. Simple string interpolation won't work in my case. – Jon49 Oct 30 '14 at 21:45
  • So, it's not really possible then. Thanks. It was more academic for me. Since I'm learning the limits of php. I'll go ahead and pick up twig. Thanks. – Jon49 Oct 30 '14 at 22:14
1

Thats so simple just use this:

<?php

    $str = "some words";
    echo "<p>$str</p>";
?>

Also some extra information about single and double quotes you finde here: What is the difference between single-quoted and double-quoted strings in PHP?

Community
  • 1
  • 1
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • I updated my question so you can understand where I am coming from. It is more complicated than what I put above. Sorry, I should have explained better. – Jon49 Oct 30 '14 at 21:46