Reputation: 13
I dont like to use a template engine in PHP. But i do like to assign {variable} to a PHP variable. How can i convert these variables to a php variable?
I tried to search on stackoverflow etc, nothing is what i mean.
Probally you will suggest use a TPL engine but that is not my question. What should be the right term for this 'function'. I just wanna learn for example with a good tutorial.
{username}
links to $user->data()->username
Upvotes: 0
Views: 195
Reputation: 869
If you are looking to roll your own basic templating you can use str_replace
$string = 'Hello {username} Today is {fulldate}';
$search = ['{username}', '{fulldate}'];
$replace = [$user->data()->username, date(DATE_RFC850)];
$result = str_replace($search, $replace, $string);
It's basic and crude and can be full of gotchcas if you're not careful. Which is why using a template engine is highly recommended. They are more powerful and generally safer then rolling your own.
Upvotes: 2