Iterative substitution allows silliness. It's not a specific problem because
the range of inputs is so narrow here, but it's good cleanup the code-smell.
* Evaluated template.
*/
public static function evaluate(string $tplPath, array $params): string {
- $str = file_get_contents($tplPath);
- // FIXME: Use a single call to 'strtr($str, $vars)'
+ $template = file_get_contents($tplPath);
+ $vars = [];
foreach ($params as $key => $value) {
- $str = str_replace('%%' . $key . '%%', $value, $str);
+ $vars['%%' . $key . '%%'] = $value;
}
- return trim($str) . "\n";
+ return trim(strtr($template, $vars)) . "\n";
}
}