Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently decided to see if I could make a functioning templating system. It works, but I feel like it is not all that great. What I'm looking for here is just general critique or ideas to improve. I'm new to this stackexchange, so if this is not the right place to do this, please let me know.

As requested below, I'll outline why I feel this templating system is not up to par. I drew inspiration from the Wordpress templating system. However, without having all the WP backend, its does not have nearly the same functionality (though it should be noted, I'm not trying to recreate WP, I just used it templating system as my inspiration). I supposed the most glaring piece missing is the lack of GET compatibility. You know how you can take www.site.com/?user=37 and make it www.site.com/user/37. Basically stripping out the GET requests and making it look nice. That's something that is missing. However, I feel like already my code in ungainly and a mess, and adding in exceptions for words or numbers that might be GET requests or might be filenames will only exacerbate the problem.

A problem I've run into is that, for some reason, sometimes it likes to grab my GET calls for the CSS and JS files and pipe it through the templating system, which obviously breaks the CSS and JS. Near as I can tell, it grabs the path/to/file.js and the templating system looks for templates/pages/path.to.file.js.php. I'm not 100% sure why it does this to the link and script includes of CSS and JS, but it is annoying. And one of the reasons that this system is sub-par.

To protect your sanity, I'll only include the important files. The Template class (shown) the request chained through public/index.php->public/bootstrap.php->utilities/main.php-> utilities/Template.php (thispage/requests->this/one)

/public
  index.php
  bootstrap.php
/templates
  /layouts
    header.php
  home.php
  404.php
  page.php
  multilevel.page.php
/utilities
  main.php
  Template.php

public/index.php

<?php require_once 'bootstrap.php'; ?>
<!-- if page is called with AJAX -->
<?php if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') : ?>
  <?= Template::find(); ?>
<?php endif; ?>
<!-- otherwise, display -->
<!DOCTYPE html>
<html>
  <head>
    <?= Template::show('components.head'); ?>
  </head>
  <body class="<?= Template::classes(); ?>">
    <header>
      <?= Template::show('layouts.header'); ?>
    </header>
    <main>
      <div class="container">
        <?= Alert::display() ?>
      </div>
      <?= Template::find(); ?> <!-- main content -->
    </main>
    <footer>
      <?= Template::show('layouts.footer'); ?>
    </footer>
    <?= Template::show('components.scripts'); ?>
  </body>
</html>

utiltiies/Template.php

<?php

class Template
{
  public static function show($file, $page = false)
  {
    if (!$page) {
      $filepath = str_replace('.', '/', $file);
    } else {
      $filepath = 'pages/'.$file;
    }
    $template = ROOTPATH.'/templates/'.$filepath.'.php';
    require_once $template;
  }

  public static function find()
  {
    $fileName = Template::getFileName();
    if (Template::pageExists()) {
      Template::show($fileName, true);
    } else {
      Template::show('404', true);
    }
  }

  public static function getTitle()
  {
    if (Template::pageExists()) {
      $fileName = Template::getFileName();
      $file = ROOTPATH.'/templates/pages/'.$fileName.'.php';
      $docComments = token_get_all(file_get_contents($file))[0][1];
      if (strpos($docComments, "Title")) {
        $woFront = str_replace('<!-- Title: ', '', $docComments);
        $title = explode(' -->', $woFront)[0];
      } else {
        $title = ucwords(str_replace('.', ' ', $fileName));
      }
    } else {
        $title = "Page not found";
    }
    return $title;
  }

  public static function classes($custom = false)
  {
    if ($custom) {
      $custom = ' '.$custom;
    }
    $classes = Template::pageClass() . $custom;
    return $classes;
  }

  private static function getFileName()
  {
    $URI = $_SERVER['REQUEST_URI'];
    if ($URI === '/') {
      $fileName = 'home';
    } else {
      $fileName = str_replace('/', '.', trim($URI, '/'));
    }
    return $fileName;
  }

  private static function pageClass()
  {
    $URI = $_SERVER['REQUEST_URI'];
    if ($URI === '/') {
      return 'home';
    } elseif (Template::pageExists()) {
      $className = str_replace('/', '-', trim($URI, '/'));
      return $className;
    } else {
      return '404';
    }

    $fileName = Template::getFileName();
    if (Template::pageExists()) {
      $className = str_replace('/', '-', trim($fileName, '/'));
      return $className;
    } else {
      return '404';
    }
  }
  private static function pageExists($page = false)
  {
    if (!$page) {
      $fileName = Template::getFileName();
    } else {
      $fileName = $page;
    }
    $file = ROOTPATH.'/templates/pages/'.$fileName.'.php';
    if (file_exists($file)) {
      return true;
    } else {
      return false;
    }
  }
}
share|improve this question
1  
can you add more info like any reasons why you feel it maybe limited in effect or versatility, any known or predicted limitations, any required functionality you predict being a possibility in the future from this? Also, a nod towards any direct inspiration sources would be nice, if you have them, as it might help us answer. – Daniel Brose Mar 17 at 4:26
    
@DanielBrose I have added the information requested above as the second paragraph down. Thanks – amflare Mar 18 at 15:55
    
I added an edit with more information pertaining to why I am not satisfied with my system. – amflare Mar 23 at 15:57
    
Clean URLs can be achieved with url-routing, done using PHP or .htaccess (if using Apache HTTPD). Those mechanisms should be independent of the templating system. – 200_success Mar 23 at 16:09
    
@200_success That would require a fundamental change to several of my Template functions, yes? Or are you saying there is a way to merge that in? Not that I am opposed to the fundamental change, I just want to be sure I'm not going about it the hard way. – amflare Mar 23 at 16:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.