string - Define variable instead of php echo to implement template tags feature -


i writing project template adding feature. , want have simple variables in template thats why want define variables or thing instead of using :

<?php echo $variable; ?>

i want have thing :

{$varaible} 

how can ? how can create simple template engine ? thanks.

you can using existing template engines or following code set.

create template file shown below ( tweak html , add more template variables or ). save file , call mytemplate.txt

<html> <head> <title>{title}</title> </head> <body> <h1>{header}</h1> {text} </body> </html> 

create php file , , call home.php ( or name depending on use case ) . add following code.

<?php  $tags['title']="replaces title tag"; $tags['header']="replaces header tag"; $tags['text']="replaces text tag";   //lets open template file , replace tags print preg_replace("/\{([^\{]{1,100}?)\}/e","$tags[$1]",file_get_contents("mytemplate.txt")); ?> 

make sure mytemplate.txt, , home.php in same directory on server.

note :- gives basic template engine, using preg_replace function of php.

here references

  1. https://www.webmasterworld.com/php/3444822.htm
  2. http://php.net/manual/en/function.preg-replace.php

Comments