Templating Engine: avioding bottle neck.
Author |
Message |
Craige
|
Posted: Sat Dec 23, 2006 4:45 pm Post subject: Templating Engine: avioding bottle neck. |
|
|
Technically, this thread could go in the PHP help forum, but I am not asking for help with the language specifically, but rather a code design. Thus, I have posted it here.
So my problem is in a potential design for a website templating engine I'm working on for a new site. I was wanting to do a recursive variable replacement function to the class, however my problem is with how to design it. I wanted to do a recursive function, but I'm not sure how much this is going to bottle neck the script on me.
The template file which includes a recursive replace might look like so:
code: |
<!--Block: LIST-->
<div style="whatever"><!-- < Var > --></div>
<div style="whatever"><!-- < Var2 > --></div>
<div style="whatever"><!-- < Var3 > --></div>
<!--End Block: LIST-->
|
And what the templating engine would do is take a copy of the entire block, and use it as the template to replace variables in, x number of times. Each time, appending the result block to a static temp variable.
The recursive function would work something like so:
code: |
if first iteration
get count of block variables: store in static var
get copy of block from temalte and save in static var
set static var to represent the iteration function level
if ! first iteration, > last iteration
replace vars in copy of block by calling self with loop num set to 1 for each var
append new block to temp static var
call self with loop num set to num - 1
if last iteration
I havn't worked it out yet, but it would finalize the iterations, and return true.
Add new set of blocks to template
|
I fear this will leave me with a huge bottle neck, which I want to avoid. I haven't worked out how all the code will work yet, but I think you can see what I want to do.
Ideas? Opinions? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Sat Dec 23, 2006 6:11 pm Post subject: (No subject) |
|
|
I'm curious as to whether this is an academic exercise. Templating engines have been done so many times, and there are so many good ones out there that I fail to see why you wouldn't just learn one of those.
Of course, there's nothing wrong with it if it is an academic exercise. I'm just curious. |
|
|
|
|
|
Craige
|
Posted: Sat Dec 23, 2006 6:31 pm Post subject: (No subject) |
|
|
Nah. I've never even taken an academic course on programming. This is for personal use.
And the reason I don't use a prebuilt templating engine is simply for two reasons:
1) I enjoy learning new thins like this. Every experience helps you out later in life.
2) I generally don't use any prebuilt libraries for my personal, closed source sites. I don't have any problem using them in Open Source things I may do, but if nobody else is going to see and/or modify it (outside of a small team of coders), I prefer the code to be all original. It just means I know it in and out, and can customize it easily. |
|
|
|
|
|
wtd
|
Posted: Sat Dec 23, 2006 7:41 pm Post subject: (No subject) |
|
|
For simplicity, I would suggest having your templating code look absolutely nothing like the HTML you plan to embed it in.
Consider Perl's Template Toolkit for instance, that uses brackets instead of less-than and greater-than symbols. |
|
|
|
|
|
Craige
|
Posted: Sat Dec 23, 2006 8:10 pm Post subject: (No subject) |
|
|
wtd wrote: For simplicity, I would suggest having your templating code look absolutely nothing like the HTML you plan to embed it in.
Consider Perl's Template Toolkit for instance, that uses brackets instead of less-than and greater-than symbols.
Actually, I'm using those due to the fact they are HTML comments. If I don't use a variable for some reason, it gets output as a comment. No big deal. Though in the programming scope of it, I should do something with every variable regardless of whether I want to display something or not. |
|
|
|
|
|
|
|