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 model from outside the program can be achieved with a template engine.
Perks
Don’t reinvent the wheel
It’s better to reuse the hard work of other developers than to implement something like this "from scratch". You avoid spending your own time and avoid many mistakes and mishaps.
More resources (formatting, listing, variables)
At first it may be necessary to just replace one or two fields. But what to do when the entry contains, say a list of items? And the formatting of dates?
The advantage of using a template engine generic is that it already comes "free" with these resources, and other.
Disadvantages
Memory and storage
Use a template engine will likely aggregate more files and classes than a simpler solution of its own.
Performance
Depending on the necessary resources, a template engine can be less efficient as it has many unused resources. However, depending on the amount of replacement operations performed, this the framework can 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();
Produces:
"hello Dude "
Template:
Hello {==$name=}!
The template is converted to PHP:
<?php echo 'Hello ', $name, '!'; ?>
The execution can be done so:
require_once 'Tenjin.php';
$engine = new Tenjin_Engine();
$context = array('name'=>'World');
$output = $engine->render('ex.phtml', $context);
echo($output);
Exit:
Hello World!
Code for implementation:
// 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}
Exit:
1, 2
Template:
Hello {$name}
Code for implementation:
require 'lib/Dwoo/Autoloader.php';
\Dwoo\Autoloader::register();
$dwoo = new \Dwoo\Core();
$data = array('name'=>'World');
$dwoo->output('caminho/template.tpl', $data);
I do not understand what is the final outcome you want to reach. You can explain more in the question?
– Sergio
If I declare a variable age with value of 17, and in the file there is 'I am %{age} years old' the output would be 'I am 17 years old' understood? (I can’t edit the file because it’s off my server)
– Júnior
What exactly can you have in value ? It seems to me that you will have to create some kind of minilanguage to handle strings/numbers. A
eval
wouldn’t be nice since the file isn’t on your server..– Guilherme Bernal
you are mounting a template system?
– rray
Not for nothing, but regex is the last thing to use in a case like this.
– Bacco
Yeah, it’s more or less a template system. @Bacco I was thinking of using regex to return the variable name and its value, and use to replace them in the files, but if you have a better method feel free to share it.
– Júnior
@Junior posted a very didactic solution so that it is easy to take advantage and adapt as used. Regex would kill pigeon with cannon shot.
– Bacco