[PHP]Multiple loading instances

Status
Not open for further replies.

Adil

DevBest CEO
May 28, 2011
1,276
714
Basically, I'm working on a CMS, and I need an over-simplified templating system. I've got this so far (from a tutorial):
class.template.php
PHP:
<?php
class template{
function load($filepath) {
 
      $this->template = file_get_contents($filepath);
 
   }
 
   function replace($var, $content) {
 
      $this->template = str_replace("$var", $content, $this->template);
 
   }
 
   function publish() {
 
     eval("?>".$this->template."<?");
 
   }
 
 
}
index.php (calls necessary functions)
PHP:
<?
 
include "Classes/template.class.php";
include "global.php";
 
 
$template = new Template;
 
$template->load("home.html.php");
 
$template->replace("#title#",$CONFIG['site']['title']);
 
$template->replace("#description#",$CONFIG['site']['desc']);
 
$template->publish();
 
?>
What I want to do, is load more than one file, or just use some tools like '#title#' throughout the CMS
Any ideas?
~Adil
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
I'm not quite sure what you mean but I have a small tip for you:

Use <?php instead of <?, I know I used to do it but not all web-servers accept the <? PHP-opener whereas they ALL accept <?php if they have PHP installed on them.
The same applies for when doing something similar to:

PHP:
<body>
<?php
$var = "Mark";
?>
Hello, my name is <?=$var;?>
</body>

You should do:

PHP:
<body>
<?php
$var = "Mark";
?>
Hello, my name is <?php echo $var; ?>
</body>

I know it might seem annoying and takes a little more time, but whatever. I've gone back to using <?php now.
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
It may be worth taking a look at RevCMS, see how hes done it and maybe find a way to implement it into your cms
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
To load multiple files you could do...

PHP:
class template{
 
private $template = array();
 
public function load($filepath) {
 
      $this->template[$filepath] = file_get_contents($filepath);
 
  }
 
  public function replace($var, $content) {
 
      $this->template = str_replace("$var", $content, $this->template);
 
  }
 
  public function publish() {
      //I wouldn't recommend using eval
 
      foreach($this->template as $key => $value)
      {
              print $this->template[$key]; // Or you could just print the value, or do foreacy($this->template as $value) and echo it, either way it works fine.
      }
  }
}

I would use ob BTW, I.E: ob_start(), etc.

This is not the best class, but it works with what you have already.
 

Adil

DevBest CEO
May 28, 2011
1,276
714
I'm not quite sure what you mean but I have a small tip for you:

Use <?php instead of <?, I know I used to do it but not all web-servers accept the <? PHP-opener whereas they ALL accept <?php if they have PHP installed on them.
The same applies for when doing something similar to:

PHP:
<body>
<?php
$var = "Mark";
?>
Hello, my name is <?=$var;?>
</body>

You should do:

PHP:
<body>
<?php
$var = "Mark";
?>
Hello, my name is <?php echo $var; ?>
</body>

I know it might seem annoying and takes a little more time, but whatever. I've gone back to using <?php now.
I usually do that, forgot :p (was using a tutorial at the time)
It may be worth taking a look at RevCMS, see how hes done it and maybe find a way to implement it into your cms
Hmm maybe
Anyway, is there a way to just replace the #title# tag with say my site title?
 

Adil

DevBest CEO
May 28, 2011
1,276
714
Manuel, thanks for your contribution, it works :D
However, I still need to do my #title# toolkit, any ideas on how to do this?
I've figured out that it's $template->replace();, but I can't do it across multiple files
E.g: As I can only load home.html.php, I can only replace #title# in that.
 
Status
Not open for further replies.

Users who are viewing this thread

Top