Closed Thread Icon

Topic awaiting preservation: Templating and Parsing PHP in the Template (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=25949" title="Pages that link to Topic awaiting preservation: Templating and Parsing PHP in the Template (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Templating and Parsing PHP in the Template <span class="small">(Page 1 of 1)</span>\

 
Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 06-03-2005 19:50

So, having long reached the limits of what I could accomplish with SSI and being so proud of myself for writing a working PHP script, I decided to move into PHP templating. After some research, I decided Smarty was too robust for these smaller sites I'm working on (though I may use it for my personal sites in the future) and instead settled on a simple script I came across.

FYI, here's basically what I've got so far, with a little customization. I don't totally understand every line, but I was able to fake the new bits from the original bits. (Feel free to tell me, "Hey, that bit there will bite you in the ass, moron.")

I'm finding, however, that I'd like to include some PHP within the template file itself. Problem being, of course, that it doesn't get parsed for PHP. So, I did some more research while writing this post and came across eval(). Before I try to use it, though, I wanted to run my thinking by you guys.

Here's how I'm thinking of doing things; tell me if this works or if there's a better way. Within my templating class, before/after/while parsing the template file for template tags, I look for PHP open/close tags (<?PHP and ?>). When found, I eval() whatever's between them and simply return the result at the end. Correct?



(Edited by Wes on 06-03-2005 19:51)

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 06-03-2005 21:30

Woah!

I would slow down there. That seems like a whole lot of overkill there. If you go to the GN there are a couple of tutorials that I have written that will go into some simple template systems. That might be a start they are a bit old. If you want to do some basic templating in PHP I would do it with just PHP files, and no need for interpretation. The EVAL is normally a terrible idea, it is a huge gaping security hole.

What I do for most of my sites now is to create a structure as such

code:
/
- templates
-- head.php
-- foot.php
-- menu.php
- index.php



And then my index.php would be like this

code:
<?php include($_SERVER['DOCUMENT_ROOT'] . "/templates/head.php"); ?>

<!-- The HTML or PHP Content Goes in Here -->

<?php include($_SERVER['DOCUMENT_ROOT'] . "/templates/foot.php"); ?>



Your header or footer would include your menu.php file (I always put my menu into my header.php), you would update this file whenever you create a new content page.

Your header.php and footer.php would then contain all of your top and bottom template information.

I would recommend keeping it really simple to start with, the big class structure you have going on there just seems like it is way to much.

The above is most likely very similar to what you have done with SSI but allows you to add to it with some more advanced functionality.

Dan @ Code Town

Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 06-04-2005 03:27

Well, I've done that with PHP includes, but like you said, that's pretty much like what I was doing with SSI, with PHP functionality. But then I end up with includes within includes and often have to track down where the different bits are to change something.

With a templating class, each content page contains nothing more than:

code:
<?php
require_once($_SERVER['DOCUMENT_ROOT']."/templates/template.class");
$page = new Page("template.html");

$page->replace_tags(array(
	"title"		=>	"The title for this specific page.",
	"feature-item"	=>	"This is the feature item.",
	"body"		=>	"Body stuff here.",
	));

$page->output();
?>



And the single template file looks like:

code:
<html>
<head>
	<title>{title}</title>
</head>

<body>

<Layout>
<Layout>
<Layout>

{feature-item}

<Layout>
<Layout>
<Layout>

{body}

<Layout>
<Layout>
<Layout>

</body>
</html>



It separates the style and the content completely. To me, that's far more organized and simpler to maintain. Now I just want some of the PHP functionality back in the template.



(Edited by Wes on 06-04-2005 03:32)

DmS
Maniac (V) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 06-05-2005 14:10
quote:
Now I just want some of the PHP functionality back in the template.

and that's pretty much what smarty does.

Smarty has a set of built in functions that you can call from within the smarty tags (curly braces). The functions are built in PHP and are called through a set smarty-tag syntax.

I've worked quite a lot with smarty lately and I'm split, I think you tend to put too much logic into the templates in smarty. Personally I prefer to call a predefined function that produces a result and replaces the tag that represents what you want to get.

Looking at you example there is actually nothing that stops you from defining a set of tags that represents the defined PHP functionality you want. that would keep the logic out of the template and into the engine, where it IMHO belongs.
/Dan

{cell 260} {Blog}
-{ ?There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence. - Jeremy S. Anderson" }-

jiblet
Paranoid (IV) Inmate

From: Minneapolis
Insane since: May 2000

posted posted 06-06-2005 16:15

Wes, you should really check out my earlier post about Templation to be released under the GPL in September. Even though the system is written in PHP, it doesn't conflict with your code, because it creates the final page using readfile() instead of include(), so your page is built in a sandbox and only executed after Templation has completed it's processing.

The reason I think you might like it is because it doesn't require a reference to the template within each page. Instead, the functionality begins from a global driver script that you include at the top of every page. Which template to use can be specified in a META tag either in the file or any of its parent directories. So in other words, if you specify the template to use in the root directory, all pages will use that template unless a new template is specified at a lower level. All data is treated this way, even include files are searched for recursively, so your template can specify something like <%%inc menu.php%%>, but it will recursively search for menu.php so you can have a different menu for different directories.

Anyway, I was kind of trolling for beta testers around here, but so far not even a single response...

-jiblet

Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 06-07-2005 02:55

DmS, I think I'm going to end up doing what you've suggested and define tags that trigger functions in the script, at least for this site. It's pretty small, so there shouldn't be that much to do.

Jiblet, I saw your post before and thought possibly of looking at it for another project, but this one has to be done pretty quick here. Feel free to e-mail me and let me know more, though. I might take a look at it for my personal site I'm hoping to get started on in the near future.

It's probably apparent I'm no guru at any of this stuff, so I may not be the best beta tester. Then again, that may make me a better beta tester, I dunno.

jiblet
Paranoid (IV) Inmate

From: Minneapolis
Insane since: May 2000

posted posted 06-07-2005 17:08

The code is pretty solid, so I'm looking for beta testers mostly to make the setup easier and suggest what kind of tools I should develop this summer before the public release. Once it's installed properly (which of course I can help with) you never even really see PHP code, which is one of the strong points. I'm currently simplifying the install process so you should be able to try it out mostly by dropping a few files in place. When I'm done I'll shoot you an email...

-jiblet

« BackwardsOnwards »

Show Forum Drop Down Menu