Page 2 of 3

Posted: Thu Sep 01, 2005 3:58 pm
by McGruff
Yes.

I rattled out that example without really thinking about it too much - there may well be better ways to tag the text. Basically what you want is the simplest way to mark up sections of an html file so that, later on, a php script can find & replace with php code. Any kind of formatting surrounding the replaced sections - including tags with or without css attributes - will be preserved. It's just a question of using the right regex pattern.

Posted: Thu Sep 01, 2005 4:01 pm
by Ree
Thanks for explaining, things are getting more clear to me now. :)

Posted: Thu Sep 01, 2005 7:14 pm
by blacksnday
Figured I would throw myself into this :)

I am making a script now that looks to grow into a CMS type thing.
This has caused me to struggle with ways to create a
Template Type of Thing.......

Since I have used many stuff in the past that have template engines, at first that idea
was in my mind for my code, but as I thought about it more and more, I decided to stray
away from that route, at least for now.

Template engines are very nice, but then you have to start making your code around
the template engine, and lose the original aspect.
They also cause many limitations, at least have for me in the past.

The way I decided to go about having a template'able code, is simple.
It still requires some direct code formatting from sql queries, but I plan to fix that later.

Basically, I create a var that will allow to easily change a template set.

Code: Select all

$template   = 'default';
Then in all the main files, calls for something like the below

Code: Select all

include("./templates/$template/header.php");  
include("./templates/$template/headermenu.php"); 
include("./templates/$template/body.php"); 
include("./templates/$template/left.php"); 
include("./templates/$template/footer.php");
Then as you guessed it, the actual content of the site would be store in the
proper ./template folder and file.

Each template file is a php file, so that php can still be used.
The template design is also all css, and the css file to be used is defined in the
templates/yourtemplate/header.php
So once that is set up---- which does take a long time to do if you have a big site already,
it is extremly easy to fully change the whole design of the site just by changing the var

Code: Select all

$template   = 'default';
And you dont have to worry about tags or other stuff.

For some people this may not work because they would rather write around a template engine
and just include tags in their files.
But for me, this has the most desirable effect and allows me to have
as many templates as I want and change them just as easy :)

To see more of how I am doing it below are direct examples.

index.php

Code: Select all

include("./templates/$template/header.php");  
include("./templates/$template/headermenu.php"); 
include("./templates/$template/body.php"); 
include("./templates/$template/left.php"); 
include("./templates/$template/footer.php");
templates/blahblah/header.php

Code: Select all

echo "
<html>
<head>
<link href='style.css' type='text/css' rel='stylesheet'/>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<title> $sitename </title>
</head>
$indextext  
<div id='submiddle'></div>
";
templates/blahblah/body.php

Code: Select all

echo "<div id='mainbody'><h1 class='fivenewbash'>Bash My Ex d0t c0m<br />Newest Bashes</h1>";
echo $newsfunctions->show_last_five_entries();
echo "</div>";
The above returns my site's index with the header/main content showing
As you can see, I am sorta using a template engine 'tag' type of thing, except
my tags are actually variable being defined in other files :P

Posted: Thu Sep 01, 2005 7:59 pm
by Nathaniel
blacksnday, I would look at Alternativate Syntax for Control Structures, as well as consider <?=$your_variable;?> (might require <?PHP echo $your_variable; ?>, depending on your server's setup), instead of echoing everything as you are.

Code: Select all

echo "
<html>
<head>
<link href='style.css' type='text/css' rel='stylesheet'/>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<title> $sitename </title>
</head>
$indextext  
<div id='submiddle'></div>
";
Would be turned into:

Code: Select all

<html>
<head>
<link href="style.css" type="text/css" rel="stylesheet" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title><?=$sitename;?></title>
</head>
<?=$indextext;?>
<div id="submiddle"></div>
As you can see, code can quickly become incredibly readable using this method. :)

Good luck,

- Nathaniel

Posted: Thu Sep 01, 2005 8:07 pm
by blacksnday
I viewed your example but don't really see any benifits.
I just see having to type a few more characters.

Since this is the start of my code and templating for it,
I do believe in the future it change and grow better then now.

so much to learn, not enough time! lol :)

Posted: Thu Sep 01, 2005 8:08 pm
by blacksnday
actually.. i can see a benifit!
scratch my last reply :lol:

But i still dont see how my original post
with echo all and using variables to define site content
would be any different then how a template engine
uses tags without <?= ?> to define site content.

Posted: Thu Sep 01, 2005 9:46 pm
by sweatje
Ree wrote:nielsene, I'm not sure you understood me correctly. What I'm trying to understand is this. Say, for one site you need to generate a table with 3 columns to list some products in the db, and for another, it is required to generate a table with 1 column only to list services. Having in mind we use custom tags, will I have to create different table generating functions for each case (because the tables are different)? If so, that would mean I will not be able to create a universal templating engine to suit my needs for any case, because all cases are different in some way or another. But I can't believe there's no way to create methods that would apply to all table cases, list cases etc.
I wrote the Data:Table component from WACT (http://phpwact.org/) which does just what you are talking about (I believe). It accepts a ResultSet object (basically tabular data) and will automatically transform this into a table. To control specific behavior of the custom tag, you can either alter attributes of the tag

Code: Select all

<data:table from=source tableclass=grid>
or you can add additional custom markup for the tag to interpret:

Code: Select all

<data:table from=source>
  <data:column name="link">
    <data:cell><a href="{$link}">{$link_desc}</a></data:cell>
  </data:column>
</data:table>
HTH

Posted: Fri Sep 02, 2005 3:12 pm
by Ree
McGruff:

I do not understand one thing regarding replacing custom tags with PHP code. As you say, you get this after replacement:

Code: Select all

<h2><?php echo $table_title; ?></h2>
<ul>
    <?php
    while($foo = $foos->fetch()) {
        echo "<li>$foo</li>";
    }
    ?>
</ul>
Say, with PHP you open the .htm template and read it into a string. Since you have the string, after tag replacement the final output (page) should reach the user's browser after echo'ing the whole string. But you cannot put any <?php ?> code in the string, because echo won't work. So I do not understand, how you could replace the tags with PHP code. The only thing I know would be using output buffer to generate HTML (in our case, run the while loop), reading it and only THEN replacing the tags with it. After that you get HTML only string, which you then echo for the user.

Maybe you could explain?

Posted: Fri Sep 02, 2005 4:51 pm
by McGruff
Sorry I didn't say much about that. The template with custom tags isn't used by the program: that's just for the designer. When their done designing, it could be compiled into a second file with php code replacing the tagged text: that second file would be the template used by the php app.

There are all kinds of template systems but this is possibly the best (I first saw it in WACT). There's no eval-ing or regex replacing on each http request just some php code doing the job it was originally designed for (ie no bizarre template languages to learn a la smarty).

Posted: Sat Sep 03, 2005 1:58 am
by Ree
aaaaaah, so what the template engine needs to do is read the .htm template, say, page.htm, replace the custom tags with <?php code; ?> and then write the whole HTML+PHP into a (new) PHP file, page.php. So, that means you don't need to build a page the moment it was requested by user, right? You run the templating engine yourself, and it generates ready to use files for you, which then are served to users when requested. This 'offline' method looks really nice. :)

Posted: Sun Sep 04, 2005 8:08 pm
by Christopher
Ree wrote:aaaaaah, so what the template engine needs to do is read the .htm template, say, page.htm, replace the custom tags with <?php code; ?> and then write the whole HTML+PHP into a (new) PHP file, page.php. So, that means you don't need to build a page the moment it was requested by user, right? You run the templating engine yourself, and it generates ready to use files for you, which then are served to users when requested. This 'offline' method looks really nice. :)
All that implies that the compiled PHP files then run faster than just doing the replace in the first place. For compiling system you also have the overhead of timestamp checks (if building is automatic) and two sets of files, plus a lot more Template code to maintain. Compling system make more sense if you want complex tags. If all you need is replacement and blocks for looping then simpler templates will make more sense.

Posted: Tue Sep 06, 2005 2:04 pm
by Ree
I have another question related to the templating method (custom tags) McGruff explained to me.

*When I write 'HTML' page, I have in mind that it contains custom tags*

McGruff:

As you said, designers want to see the whole HTML page, just like it will be visible for end-users. But almost always site pages contain identical elements - header, footer, menu etc. So here comes the problem - designers want to see full HTML pages (with header, footer etc), but I myself always have each page element stored as a separate HTML file and construct full pages from these parts. Say, two pages - Services and Products: for Services I take header.htm, footer.htm, menu.htm and content element services_content.htm. I combine them and get the whole page. For Products it's the same, but in this case I take products_content.htm as content element. So now the question is: do I need to use full HTML pages, or should I use HTML parts when replacing the tags and generating full .php pages? What is a good way of organizing the .htm templates? That's something I would like to know, as that will influence how I will be writing the tag replacement script.

Would really like to recieve your input on the same, maybe you could also indicate how you do it (if you do).

Posted: Tue Sep 06, 2005 3:06 pm
by McGruff
It's up to you whether you prefer working with whole templates or fragments in separate files. I like the former best because I personally like to work with whole pages when I'm working on the html/css. Also, this simplifies the main body of the php code to simply defining sets of data; otherwise, there would have to be some knowledge of the page structure. If - say - the same tabular data is formatted in different ways on different pages the script would also have to select the correct template fragment.

I prefer to deal with formatting (inc structure) using a formatting tool but, whichever way you go, you've still got the same tags to replace.

Posted: Tue Sep 06, 2005 3:46 pm
by Ree
Now when replacing some tag, how will you put '<?php some_code; ?>' in place of it? I think you cannot declare a string, say $var = '<?php some_code; ?>' and use it in preg_replace(). Maybe reading <?php some_code; ?> code pieces from file(s)?

Posted: Tue Sep 06, 2005 4:16 pm
by McGruff
That bit's up to you :)

Basically, regex out tagged sections of text, replace with php code, write to file (there's no problem with a string like '<?php some_code; ?>' in preg_replace).

You could start with a TagIterator which simply spits out tag meta data (tag type & offsets) for any tags it finds in a named list.

Next match up pairs of tags as they arrive from the iterator (if tags can be nested this'll be a composite structure).

With tag type and offsets, you know which part of the string to replace and what to replace it with.