Python has a feature called template strings.
>>> from string import Template >>> s = Template('$who likes $what') >>> s.substitute(who='tim', what='kung pao') 'tim likes kung pao'
I know that PHP allows you to write:
"Hello $person"
and have $person
substituted, but the templates can be reused in various sections of the code?
A PHP template engine is a way of outputting PHP in your HTML without using PHP syntax or PHP tags. It's supposed to be used by having a PHP class that will send your HTML the variables you want to display, and the HTML simply displays this data.
Template string is another method used to format strings in Python. In comparison with %operator, . format() and f-strings, it has a (arguably) simpler syntax and functionality.
You could also use strtr
:
$template = '$who likes $what'; $vars = array( '$who' => 'tim', '$what' => 'kung pao', ); echo strtr($template, $vars);
Outputs:
tim likes kung pao
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With