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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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