Jump to content

Templating question. Using PHP to manage templates.


kratsg

Recommended Posts

We all understand the concept of accessibility and usability based on numerous different sites to have a certain script. That said, we also know Smarty is a mean, extreme template engine that makes life easy in some cases. If I wanted something simple such as just replacing variables within a template (even if it was just an html table), what would be the best option?

 

For example: I have one where based on the group, different group panels display (some groups see other groups as well as their own).

 

If I had a template that looked like this:

 

grouplinks.tpl.php

<table border='1' width='75%' cellpadding='0' cellspacing='0' style='text-align:center;' align='center'>
<tr><td width='100%'><b>{$header}</b></td></tr>
//the rows after are built based on array values using a loop
<tr><td width='100%'>{$data}</td></tr>
//the rows are built based on array values using a loop
</table>

 

How do I manage something like this? Where {$header} gets replaced with the corresponding header and you automatically loop placing the data in?

 

What I want this to be applied to is a flat-file based ACP (using .htaccess, .htgroup, .htpasswd files) for user-login and management, group management, and template management (template of the ACP itself). I don't know how to NOT use Smarty for templating, as you need to install Smarty on the site that the ACP is on in order for templates to work...

While I don't believe in them, a simple templating engine is easy to create. Given the following template....

 

foo.html

<html>
  <head>
    <title>{title}</title>
  </head>
  <body>
    <p>Hello {hello}</p>
  </body>
</html>

 

You could simply parse it using something like...

 

<?php

  $title = 'foo';
  $hello = 'bob!';
  $file = file_get_contents('foo.html');
  $file = str_replace(array('{title}','{hello}'),array($title,$hello), $file);
  echo $file;

?>

 

As soon as you need to start implimenting loops and conditionals though it gets far more complex. Personally, I use php as my templating engine. Its bar far the quickest.

As soon as you need to start implimenting loops and conditionals though it gets far more complex. Personally, I use php as my templating engine. Its bar far the quickest.

 

How so? What I was trying to accomplish with the ACP, is if someone who doesn't know PHP uses it, they shouldn't be able to affect the php file itself, but rather, an html template file with {variables} that php will recognize and replace.

While I don't believe in them, a simple templating engine is easy to create. Given the following template....

 

foo.html

<html>
  <head>
    <title>{title}</title>
  </head>
  <body>
    <p>Hello {hello}</p>
  </body>
</html>

 

You could simply parse it using something like...

 

<?php

  $title = 'foo';
  $hello = 'bob!';
  $file = file_get_contents('foo.html');
  $file = str_replace(array('{title}','{hello}'),array($title,$hello), $file);
  echo $file;

?>

 

As soon as you need to start implimenting loops and conditionals though it gets far more complex. Personally, I use php as my templating engine. Its bar far the quickest.

I or rather we use almost the same as thorpe's trying to say

why use smarty etc... if you can make your own...

 

The point is that in order for smarty to work, a server has to have it installed... And I need to make any php script compatible with any server that has at least PHP, so I can't be like "SMARTY REQUIRED", so I need my own templating system.

Then, my example is a good place to start. However, as soon as you need to start using loops and conditionals within a template, the actual template system becomes far more complex to write than my simple example.

 

If you don't need loops or conditionals something like my example will suffice. Otherwise, start writting. I'm not sure what you want from us.

It is that simple .. I would add a folder called "smarty" under your application folder.  Then you will need to set the appropriate settings in your script to tell it where smarty is.  Something like require_once('smarty/Smarty.class.php');

 

The only slightly messy part may be setting permissions on the folders smarty uses for caching and so on.  Those will need to exist be writeable.

 

And the other issue is that your bundled smarty will eventually become out of date.  But at least you don't have to worry about future releases breaking your script :)  PHP itself has quite a few third party libraries bundled with it, which is how it is able to compile with so many features without installing anything extra.

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.