0

Possible Duplicate:
Dirt-simple PHP templates… can this work without eval?

Let's say I have a text file called template.tpl. The contents of template.tpl is:

<html>
<body>This is a variable: {$variable}</body>
</html>

Is there a way for PHP to render template.tpl as a PHP file and then understand that {$variable} should be processed as <?php echo $variable; ?>?

Community
  • 1
  • 1
John
  • 32,403
  • 80
  • 251
  • 422
  • 1
    Well, with string processing, there is a way, so the answer is yes. However I wonder with such a trivial answer, why you ask the question. Also I can imagine this has been asked before, so I wonder why you haven't found so far a more detailed answer. – hakre Jun 24 '12 at 15:16
  • The question I linked above is basically asking the same question (albeit in a much more long-winded way). The [answer I gave then](http://stackoverflow.com/questions/3930053/dirt-simple-php-templates-can-this-work-without-eval/3958625#3958625) is the same as the one I'd give now. In short: why do you want to do this? – Spudley Jun 24 '12 at 15:19

2 Answers2

1

Pretty much trivial with output buffering.

$variable = 'I am a variable';
$output = '';
ob_start();
require 'template.tpl';
$output = ob_get_clean();
echo $output;
0

If you must do this sort of thing, use the Smarty templating language. That's what it does, and it's popular enough that it must be doing something right.

However, I would strongly suggest not doing this. See my related answer here: Dirt-simple PHP templates... can this work without `eval`?

Community
  • 1
  • 1
Spudley
  • 166,037
  • 39
  • 233
  • 307