The process you're describing is often referred to as "template rendering" or "string interpolation." In Laravel, Blade is the templating engine that handles this kind of text substitution. However, if you want to perform similar text substitution outside of Blade, you can use PHP's str_replace or preg_replace functions, or even a more sophisticated templating library.
Here's a simple example using PHP's str_replace to achieve this:
function substituteText($template, $variables) {
foreach ($variables as $key => $value) {
$template = str_replace('{{ ' . $key . ' }}', $value, $template);
}
return $template;
}
$template = "Hello {{ name }}, how are you today? {{ agent_name }}";
$variables = [
'name' => 'John',
'agent_name' => 'Bob'
];
$result = substituteText($template, $variables);
echo $result; // Outputs: Hello John, how are you today? Bob
In this example, the substituteText function takes a template string and an associative array of variables. It replaces each placeholder in the template with the corresponding value from the array.
If you need more advanced templating features, you might consider using a library like Twig or Plates, which are more powerful and flexible than simple string replacement. However, for basic use cases, the above approach should suffice.