Jump to content

Template Engine v0.98


Nameless12

Recommended Posts

i'd have liked to see an example file there (without going through the docs to learn it first) so if you can bundle one of them in that'd be good.

 

i had a look through the features. Template engines are kinda funny things in that we all have our own features that we need. I personally like the idea of having a 'master.tpl.php' which contains the "outer" template.

 

i LOVE the idea of supporting short tags even if they're turned off on the server - I use short tags / alternative syntax exclusively in my view files, so that's a good move.

 

as i've built my own template engine around what i kinda expect, i'll give you an idea - aside from the constructor, which doesnt do alot, these are ALL my methods and they have been used to do some pretty big stuff.

 

  • load($filename) - extracts the vars, loads a tpl.php view file, returns the resultant HTML
  • loadtovar($varname, $filename) - same as load, but puts the resultant HTML into a variable using my 'set' method. more a quick, lazy function to be honest, but saves lots of time when it comes to the common task of embedding templates within templates
  • set($varname, $value = '') - simply sets a variable value (i used to have a 'get' method too, but it never got used and in the year or so i've been using it, i've never needed one either)
  • render($return = false) - extracts all the vars, uses the 'load' method to load the 'master.tpl.php' file, and either outputs the result straight off or returns it, depending on the param. Supports Gzip compression

 

as it fits into my framework, everything else really is held in my Config file - default variables (stuff like copyright messages, so I can just tweak my config file rather than messing with my templates), gzip on/off, etc. There are so many things I'd like it to do, but i've purposely kept it small - exactly the reason why I wrote my own instead of using something like Smarty, etc.

Link to comment
Share on other sites

I dont quite get what you mean by load to var?? do you mean buffering the content? or adding the content of one template into a variable of the selected template? I dont quite get it.

 

I have never really used Gzip compression with web design although I have heard about it, I might have to look into it a little.

 

when you say a get method did you mean __get()? i find there is still use for __get even though i am extracting the values from the template data regardless I think its good for completeness. I dont quite get what you mean by the outer template because I just use my this class as a way of loading additional features 

 

The reason I chose to go for completeness is because I want to finally get a "final version" and want this template to come out of beta but one of the main reasons I have chosen to not really care about the performance loss in some areas is because unlike some other template engines i set mine up with the idea of only ever using one template object, i never really thought of using a singleton for a template engine before but it works quite well, no matter how many templates you use you have still created only one object, so i hope that makes up the difference in performance.

 

I put a few basic examples in the readme file but here are some basic examples

 

<?php
#the false value turns off buffering 
template()->assign('name1', 'value1')
              ->assign('name2', 'value2')
              ->display('template.tpl', false);
?>

 

<?php
$t = template();
$t->assign('name1', 'value1');
$t->assign('name2', 'value2');
$t->display('template.tpl');
?>

 

 

<?php
#sets the dir through the factory
template('/some/dir/')->display('template.tpl');
?>

 

 

#loading the template and assigning data
echo template()->assign('name1', 'value1');
                     ->assign('name2', 'value2');
                     ->display('template.tpl');


#inside the template.tpl

<?php $this->debug()?> <?-- the tags are comments and $this->debug() displays all the assigned vars --?>


<?-- displays name1 --?>
<?=$this->name1?>

<?-- also displays name1 --?>
<?=$name1?>

Link to comment
Share on other sites

some people like to do stuff like: include('header.php'), etc. but i tend to use an 'outer' template. If you've had experience with CakePHP or Rails, or possibly even dreamweaver templates, you'll know where i'm coming from - you have an "outer" file which contains all your doctype, css/js includes, and the "common" layout for your site.

 

loadtovar is a bit like 'set', only instead of putting a value into a variable, it puts a parsed template into a variable. it's very rare you'll use a template system on a site and not want to put templates within templates.

 

for example:

 

<?php
$tpl = new Template('/templates/');

$tpl->set('name', 'Cornholio');
$tpl->loadtovar('nav', 'navigation.tpl.php');
$tpl->loadtovar('content', 'index.tpl.php');
$out = $tpl->render();

echo $out;
?>

 

master.tpl.php

...doctype, etc...
<html>
<head>
...js/css includes, etc
</head>

<body>
   <div id="wrapper">
      <div id="header">
         <img src="/images/logo.gif" alt="logo" />
      </div>

      <div id="navigation">
         <?=$nav ?>
      </div>

      <div id="content">
         <?=$content ?>
      </div>

      <div id="footer">
         Copyright 2007 etc etc
      </div>
   </div>
</body>
</html>

 

navigation.tpl.php

<ul>
   <li><a href="/">Home</a></li>
   <li><a href="/about/">About</a></li>
</ul>

 

index.tpl.php

<h1>Home Page</h1>
<p>Hello, I am the home page!</p>
<p>I am <?=$name ?></p>

Link to comment
Share on other sites

ah it makes sense now I always use a controller so i have never really bothered with the other approach as I don't feel it centralizes my code enough for my liking.

 

I don't get why people do it that way, is my bias warranted?? to me it makes no sense unless you are programming for speed.

 

Link to comment
Share on other sites

×
×
  • 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.