Ahmad Ameri
Ahmad Ameri

Reputation: 400

Including template file in PHP and replacing variables

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 :-)

Upvotes: 4

Views: 15568

Answers (5)

Mihai Iorga
Mihai Iorga

Reputation: 39724

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()

Upvotes: 14

Asad Ali
Asad Ali

Reputation: 665

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;


 ?>

Upvotes: 2

Gummibeer
Gummibeer

Reputation: 197

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!?

Upvotes: 2

Adsy2010
Adsy2010

Reputation: 545

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.

Upvotes: 1

dadasign
dadasign

Reputation: 438

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.

Upvotes: 1

Related Questions