4

I have a .tpl file which contains the HTML code of my webpage and a .php file that I want to use the HTML code in it and replace some variables. For example imagine this is my file.tpl:

<html>
<head>
<title>{page_title}</title>
</head>
<body>
Welcome to {site_name}!
</body>
</html>

and I want to define {page_title} and {site_name} in my php file and display them.

One way we can do this is to load the page code in a variable and then replace {page_title} and {site_name} and then echo them.

But I don't know that it's the best way or not because I think there will be some problem if the .tpl file is large.

Please help me to find the best way. Thanks :-)

3
  • 1
    I would say: either use plain PHP files for templating, or use one of the gazillion existing templating solutions like Twig, Smarty or Mustache. If you want to invent your own you should know what you're doing and have a good reason to create yet another template system. Commented Dec 21, 2013 at 14:32
  • 1
    I always feel uneasy when I see people implementing a templating solution in/for php when php is actually build to be used inside html. What kind of benefits are you expecting from using templating? In many cases you can achieve the same with a clean structured php/html code. Commented Dec 21, 2013 at 14:47
  • @Layne sometimes it is a requirement to limit the availability of PHP. For example if users are allowed to create templates. You don't want them to run any PHP code. Commented Dec 21, 2013 at 14:50

5 Answers 5

14

One way you could do it:

$replace = array('{page_title}', '{site_name}');
$with = array('Title', 'My Website');

$contents = file_get_contents('my_template.tpl');

echo str_replace($replace, $with, $contents);

Update: removed include, used file_get_contents()

Sign up to request clarification or add additional context in comments.

1 Comment

This produces the same results as a file_get_contents, but is vulnerable to exploitations, since PHP is EVALUATED with include. user3105264, if you don't want to use a templating system, IMHO you should rather use file_get_contents.
2

Cause I searched for this and found this article I will provide my solution:

$replacers = [
    'page_title'=> 'Title',
    'site_name' => 'My Website',
];
echo preg_replace("|{(\w*)}|e", '$replacers["$1"]', $your_template_string);

You have to get your Template to a String. For example with

file_get_contents(), 
ob_start(); 
include('my_template.tpl'); 
$ob = ob_get_clean();

or anything like this.

Hope this will help!?

Comments

2

Here is simple example hope this will work

Your HTML :

<?php

$html= '
<div class="col-md-3">
    <img src="{$img}" alt="">
    <h2>{$title}</h2>
    <p>{$address}</p>
    <h3>{$price}</h3>
    <a href="{$link}">Read More</a>
</div>
';

?>

Your Array with you want to replace

<?php 

$content_replace = array(
    '{$img}'    => 'Image Link',
    '{$title}'  => 'Title',
    '{$address}'=> 'Your address',
    '{$price}'  => 'Price Goes here',
    '{$link}'   => 'Link',
    );

$content = strtr($html, $content_replace );

echo $content;


 ?>

Comments

1

As you mention you can read the file into a string and replace your markers, alternatively you can include the file but in such case rather than use markers insert php fragments to echo the variables like:

<html>
<head>
<title><?php echo $page_title ?></title>
</head>
<body>
Welcome to <?php echo $site_name ?>!
</body>
</html>

In such case you don't need to run str_replace on the whole template. It also alows you to easily insert conditions or loops in your template. This is the way I prefer to handle things.

2 Comments

It's also much faster, than str_replace, but it is vulnerable, any PHP code can be run from the template file.
Using php in template files will make troubles for template designers.
1

I use something similar to the above but I am looking for a better way of doing it as well.

I use this:

$templatefile = 'test.tpl';
$page = file_get_contents($templatefile);

$page = str_replace('{Page_Title}', $pagetitle, $page);
$page = str_replace('{Site_Name}', $sitename, $page);

echo $page;

sorry to bring up an answered thread but I am looking at finding better ways to do this.

I am currently playing with jQuery to do this too so I can have dynamic pages without the full reload. For example:

<div id="site_name"></div>

<script type="text/javascript">
$.ajax({
  type: 'GET',
  url: 'data.php',
  data: {
    info: 'sitename'
  }
  success: function(data){
    $('#site_name').html(data); 
    //the data variable is parsed by whatever is echoed from the php file
  }
});
</script>

sample data.php file:

<?php
  echo "My site";
?>

Hope this might help others too.

Comments

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.