how do i write my script to output data where i place something like this: {name}

StackOverflow https://stackoverflow.com/questions/933463

  •  06-09-2019
  •  | 
  •  

Question

i have seen many scripts where you'll see something like this

<meta keyword="{keywords}" ...>

or

<input type="text" name="name" value="{name}">

what is this called and how can i do this myself.

thanks

No correct solution

OTHER TIPS

It is called a template.

You need a template engine or you can write one yourself. A long time ago there was a popular template engine called Smarty. Today, template engines are part of most frameworks.

Here's a list of a few template engines for PHP: http://www.webresourcesdepot.com/19-promising-php-template-engines/

Considering the fact that you have such a question, your very own template engine will more likely have bugs, so I suggest choosing one of the freely available ones.

One more thing. PHP itself is a kind of template engine, so you do not necessarily have to add another level of templating. Use PHP:

<input type="text" name="name" value="<?=$VALUES['name']?>">

can i recommend...

<input type="text" name="name" value="<?= e($name) ?>">

where e is an escaping function

i find template engines unnecessary

more examples...

foreach...

<ul>
<?php foreach( $items as $item ): ?>
    <li><?= e($item) ?><li>
<?php endforeach; ?>
</ul>

if....

<input type="checkbox"<?php if( $checkbox ): ?> selected="selected"<?php endif; ?>>

There are templating engines out there that support this style of variable syntax, for example Blitz templates (http://alexeyrybak.com/blitz/blitz_en.html) comes to mind.

I am not sure what you mean, but let me take a wild guess:

$foo = "Please replace {this} here!";
$bar = str_replace('{this}', 'that', $foo);

Of course there are plenty of templating systems around that do exactly that, only much smarter. So you might wanna look into that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top