Depending on the requirements (since it was mentioned that the question is a simplified example), the goal of replacing variables and generating content based on a template from outside the program can be achieved with a template engine.
Advantages
Do not reinvent the wheel
It is better to reap the hard work of other developers than to implement something like this "from scratch". You avoid spending time on your own and avoid many mistakes and mishaps.
More resources (formatting, listing, variables)
At first it may be necessary to only replace one or two fields. But what to do when the entry contains, say a list of items? What about formatting dates?
The advantage of using a generic template engine is that it already comes "to toast" with these features,
Disadvantages
Memory and Storage
Using a template engine will probably add more files and classes than a simpler own solution.
Performance
Depending on the features needed, a template engine might be less efficient, as it has many unused features. However, depending on the amount of replacement operations performed, this may change.
Examples of template engines
Sample code:
$View = new Blitz();
$View->load('hello {{ BEGIN block }} {{ $name }} {{ END block }}');
$View->block('/block', array('name' => 'Dude'));
$View->display();
Produce:
"hello Dude"
Template:
Hello {==$name=}!
The template is converted to PHP:
<?php echo 'Hello ', $name, '!'; ?>
The execution can be done like this:
require_once 'Tenjin.php';
$engine = new Tenjin_Engine();
$context = array('name'=>'World');
$output = $engine->render('ex.phtml', $context);
echo($output);
Output:
Hello World!
Code to run:
// Autoload classes ezcomponent
function __autoload( $className ) {
ezcBase::autoload( $className );
}
//cria engine com configuração padrão
$t = new ezcTemplate();
//passar variável para o template
$t->send->a = 1;
// compila o template e imprime a saída
echo $t->process( "hello_world.ezt" );
Template:
{use $a, $b = 2}
{$a}, {$b}
Output:
1, 2
Template:
Hello {$name}
Code to run:
require 'lib/Dwoo/Autoloader.php';
\Dwoo\Autoloader::register();
$dwoo = new \Dwoo\Core();
$data = array('name'=>'World');
$dwoo->output('caminho/template.tpl', $data);