0

I am writing a project with template adding feature. And want to have simple variables in template thats why I want to define some variables or some thing like that instead of using :

<?php echo $variable; ?>

I want to have some thing like :

{$varaible}

how can I do that ? actually how can I create my simple template engine ? thanks.

3
  • You should check some template engines. Commented May 8, 2015 at 8:31
  • First learn how to use existant template engines. Then study how they work internally if you want to reinvent the wheel. Try twig.sensiolabs.org Commented May 8, 2015 at 8:33
  • play with preg_replace_callback it's perfect for the job and can provide more logic than simple lookup-arrays. Commented May 8, 2015 at 8:54

2 Answers 2

2

You can do it using any existing template engines or by the following code set.

Create a template file as shown below ( Tweak the HTML , add more template variables or anything ). Save this to a file and call it mytemplate.txt

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

Create a PHP file , and call it home.php ( Or any name depending on your use case ) . Add the following code.

<?php 
$tags['title']="Replaces title tag";
$tags['header']="Replaces header tag";
$tags['text']="Replaces text tag"; 

//lets us open your template file and replace the tags
print preg_replace("/\{([^\{]{1,100}?)\}/e","$tags[$1]",file_get_contents("mytemplate.txt"));
?>

Make sure that mytemplate.txt, and home.php are in the same directory on your server.

Note :- This gives you a basic template engine, using preg_replace function of PHP.

Here are the references

  1. https://www.webmasterworld.com/php/3444822.htm
  2. http://php.net/manual/en/function.preg-replace.php
Sign up to request clarification or add additional context in comments.

Comments

-1

First of all, you will have to define your variables

Next you will have to use this PHP function to do your replacements str_replace http://php.net/str_replace

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

Apply the same to your template and it works just fine.

2 Comments

Why are you marking me down? Did you ever test before marking down? There are many ways to deal with many solutions so always test before marking down and don't also always expect all answers to be given exactly to you without you doing any further little works. Look at this from PHP.net mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) Do you understand that at all? If you do then you will know that it can be used even for your newsletter templates. READ MORE
@mehdi If you don't want to use preg_replace simply use str_replace and it will do what you want. It can simply take more examples to show you exactly what you want done...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.