Jump to content

Template?


Recommended Posts

I want to use a template for my website. A guy on another forum told me I had to use a template parser like Templatepower or Smarty.

 

Isn't there an easier way, just to implent a code or something?

 

I only want one template file and connect that one to a PHP-file with content. Then be able to change the content to content from another PHP-file when I click on a link on my site. No more functions needed.

 

 

Link to comment
https://forums.phpfreaks.com/topic/54050-template/
Share on other sites

I tried this but it doesn't seems to work:

 

 

index.php:

 

 

<?php

$title = "The title";

$content = "blablabla";

 

include ('template/main.php');

?>

 

 

 

template/main.php:

 

 

<html>

<head>

<title><?php echo $title; ?></title>

 

</head>

<body>

 

<?php echo $content; ?>

 

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/54050-template/#findComment-267424
Share on other sites

How should your template looks like?

My templates are built like that:

<html>
<head>
<title>{{title}}</title>
</head>

<body>
{{content}}
</body>
</html>

Let's say that content is in the file template.tpl

Now index.php:

<?php
$file = '';
if (file_exists('template.tpl') === true && ($size = filesize('template.tpl')) > 0) {
	$fp = fopen('template.tpl', 'r');
	$file = fread($fp, $size);
	fclose($fp);
}

$var_titles[0] = '{{title}}';
$var_titles[1] = '{{content}}';
$var_replace[0] = 'Test';
$var_replace[1] = 'The content of this file is near';

$file = str_replace($var_titles, $var_replace, $file);
echo $file;
?>

Link to comment
https://forums.phpfreaks.com/topic/54050-template/#findComment-267425
Share on other sites

I tried this but it doesn't seems to work:

 

 

index.php:

 

<?php

$title = "The title";

$content = "blablabla";

 

include ('template/main.php');

?>

 

template/main.php:

 

<html>

<head>

<title><?php echo $title; ?></title>

 

</head>

<body>

 

<?php echo $content; ?>

 

</body>

</html>

 

You had this right... you just forgot your quotation marks.

 

So your code should look like this:

 

index.php:

<?php
$title = "The title";
$content = "blablabla";

include ('template/main.php');
?>

template/main.php:

<html>
<head>
<title><?php echo "$title"; ?></title>

</head>
<body>

<?php echo "$content"; ?>

</body>
</html>

 

Should work fine now!

Link to comment
https://forums.phpfreaks.com/topic/54050-template/#findComment-267492
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.