1

OK i have 2 files one is index.php and the other is file.html now i want to display my array content through that html file like calling it and then displaying here is the code

index.php:

$var1 = 'text';

array('var' => $var1);

include ('file.html');


file.html:

<p>html {var} html</p>

now its not displaying text i have seen people use {} and call the key and display its value how do i do that? all i want is display my values through the html file not with echo or using php open close codes in between the html codes.

KJYe.Name
  • 16,969
  • 5
  • 48
  • 63
Keshav Nair
  • 423
  • 1
  • 14
  • 24
  • You could go through some Template Engines for example Smarty. How they are doing and can use Template engines instead. They did it already for you – Shakti Singh Mar 31 '11 at 12:27

10 Answers10

0

you must use template engines like smarty, or PatTemplate. or write your own.

Headshota
  • 21,021
  • 11
  • 61
  • 82
0

PHP doens't do anything with {var} statement. You should work with it manually... or read about template engines http://www.smarty.net/ for e.g.

Emmerman
  • 2,371
  • 15
  • 9
0

What you saw I assume were templates of some template engine or other. When using templates, the (HTML) template files are parsed on runtime and the placeholders ({var}) are replaced with their values. If you google around for PHP template engine a bit I'm sure you'll get the idea of it and/or find an existing solution to use.

Christof
  • 3,777
  • 4
  • 37
  • 49
0

If you want to replace tags {name} to value with $a = array('name' => 'value'); you can use str_replace();

$filehtml = file_get_contents('file.html');
$array = array('var' => 'value');

foreach($array as $tag => $value)
{
  $filehtml = str_replace('{'.$tag.'}', $value, $filehtml);
}

echo $filehtml;

Or you use Smarty (or others).

Robik
  • 6,047
  • 4
  • 31
  • 41
0

This is definitely not the way of templating but here's a rough snippet, so you get the idea.

$var1 = 'text';
$tags = array('var' => $var1);

$tpl = file_get_contents('file.html');

foreach($tags as $label => $data) {
    $tpl = str_replace('{' . $label . '}', $data, $tpl);
}

echo $tpl;
fabrik
  • 14,094
  • 8
  • 55
  • 71
0

You could use extract(), just be mindful of collisions!

index.php

$var1 = 'text';

$variabbles = array('var' => $var1);
extract ($variables);
include ('file.html');

file.html:

<p>html <?php echo $var; ?> html</p>
code_burgar
  • 12,025
  • 4
  • 35
  • 53
  • HTML file can not parse PHP variables – Shakti Singh Mar 31 '11 at 12:32
  • @Shakti - this will work: if you use `include()`, it will be treated as PHP code, even if the file name is .html. That said, I'm not sure that I'd agree with the idea of using `extract()` to achieve this; there are several better ways to do it. – Spudley Mar 31 '11 at 12:35
  • The code above works. Downvoting answers without a real reason is just plain inconsiderate. – code_burgar Mar 31 '11 at 12:35
  • @Spudley: I agree that extract() isn't the best approach, but it is the closest to what the OP wants. – code_burgar Mar 31 '11 at 12:37
  • If I had to do it like this, I would have enclosed the `include()` inside a function, with its own parameters. This would allow you to specify whatever variables you want without worrying about collisions or making unnecessary globals. – Spudley Mar 31 '11 at 12:43
0

It sounds like you want to make it easier to work with HTML without having to have PHP code embedded in it. This would be done using a templating language.

There's a number of templating languages available for PHP; the most well-known one is Smarty.

However, I would caution you that if you have any kind of complexity in your pages, you will need some sort of program code. Smarty and others allow you to do loops and conditions, but when used like this, it can very quickly become a programming language in its own right. Plus they add quite a bit of processing overhead, which is really unnecessary.

The bottom line is that for most uses, it's easier just to stick with standard PHP tags for printing your variables into your HTML code.

See also a previous answer I gave to a similar question: Dirt-simple PHP templates... can this work without `eval`?

Community
  • 1
  • 1
Spudley
  • 166,037
  • 39
  • 233
  • 307
0

If you don't want to use a template engine for this task, I could suggest the following code to add to your index.php:

<?php
$var1 = 'text';

$templateVars = array('var' => $var1);

$content = file_get_contents('file.html');

foreach($templateVars as $tplVar => $tplVarContent) {
    $content = preg_replace('/\{' . $tplVar . '\}/', $tplVarContent, $content);
}

echo $content;

Why using preg_replace()?

It's known that preg_replace() does a better job at replacing in a bigger text content rather than str_replace() which starts to be slower and slower after each replacement (you can find a benchmark on Google, I can't seem to find it right now)

Later edit:

I found am improvement on this one.

<?php
$var1 = 'test';

$templateVars = array('var' => $var1);

$content = file_get_contents('tpl.tpl');

$content = preg_replace('/\{([a-zA-Z0-9_]+)\}/e', "\$templateVars['\\1']", $content);

echo $content;

I have also done a little test against the str_replace() solution. str_replace() is faster only when there are a couple of variables (2-3). With 6 variables preg_replace() is 2 times faster.

Here are my results:

preg_replace() 10000 times: 0.038811922073364seconds
str_replace() 10000 times: 0.085460901260376seconds

This is the code I ran to test the speed:

<?php
$var1 = 'text';

$templateVars = array('var' => $var1, 'var1' => 'blabla', 'var3' => 'variable3', 'var22' => $var1, 'var133' => 'blabla', 'var3444' => 'variable3');

$content = file_get_contents('tpl.tpl');


$start = microtime(true);
for($i = 0; $i <= 9999; $i++) 
{
    $content = preg_replace('/\{([a-zA-Z0-9_]+)\}/e', "\$templateVars['\\1']", $content);
}

$end = microtime(true);

echo 'preg_replace() '. $i . ' times: ' . ($end - $start) . 'seconds<br>';


$start = microtime(true);
for($i = 0; $i <= 9999; $i++) 
{
    foreach($templateVars as $tplVar => $tplVarContent) {
        $content = str_replace('{' . $tplVar . '}', $tplVarContent, $content);
    }
}
$end = microtime(true);

echo 'str_replace() '. $i . ' times: ' . ($end - $start) . 'seconds<br>';
Bogdan Constantinescu
  • 5,296
  • 4
  • 39
  • 50
0

You can use output buffering to solve your problem but be careful on quotes if you only use php to output it.

Efox
  • 651
  • 3
  • 8
  • 19
-2

PHP doesn't care what file types you include - it will execute them as if they were php files. So.....

ndex.php:

$var1 = 'text';

$myArr = array('var' => $var1);

include ('file.html');


file.html:

<p>html <?php print_r($myArr);?> html</p>
Ian Wood
  • 6,515
  • 5
  • 34
  • 73
  • minus scores???? lol - just answered the OP - if templating was asked for then I'd have expanded - you guys are harsh ;) – Ian Wood Mar 31 '11 at 12:37
  • html Array ( [var] => text ) html is the output. IF he just wanted $var1 in there then just echo $var1 or $myArr['var'] - nothing wrong with the actual answer surely???? – Ian Wood Mar 31 '11 at 12:46
  • OP asks for a simple templating system and what you've provided is definitely doesn't do the trick. – fabrik Mar 31 '11 at 12:49