quote author=nutballs link=topic=519.msg3347#msg3347 date=1190271961
I do a bunch of
regex
replacements for tokens that are more complicated than just a single replacement. such as #PASTARTICLES-5# which would display 5 articles. I just don't have one of those implemented yet. Those are straightforward, however, the question comes into play for the Other tokens. Specifically my CONTENT token.
I have some suggested code at the bottom of this post... I've been hinting at it, but will make it clear now.
quote author=nutballs link=topic=519.msg3347#msg3347 date=1190271961
In the code page I put a function called content, shown below. That content function is a wrapper for HTML of the content area. That HTML generally might have code snips inserted, such as echo time(); for example. Is there any issue with the way I am mode switching from
PHP
to HTML so much? In ASP
its pretty much a non-issue, if you do it right.
It is a non-issue, however it is not seen as particularly good form. Personally, I prefer a pure form of
PHP
with specific outputs as opposed to starting and stopping the code the way that you have it. Here's two examples of what I mean - the first, a way that many folks do it but I personally do not approve of, and the second, the way that I do it:
<html>
<body>
<H1>Hello - the date is <?
php
echo date('m/d/y', time()); ?></H1>
</HTML>
<?
php
$t = date('m/d/y', time();
echo <<<HTML
<html>
<body>
<H1>Hello - the date is $t</H1>
</HTML>
HTML;
?>
Although the difference is ever-so-slight (and output time would probably be identical) for <me>, it is important to get into a language and stay there. It's easier for me to think of blocks of HTML that I am outputting when I choose to within a script, then it is an HTML page where I am interspersing code. I also believe that this makes my down-the-road readability considerably better.
quote author=nutballs link=topic=519.msg3347#msg3347 date=1190271961
Also, splitting the template on all the # symbols is an easy way to deal with all the left over tokens. But as the template gets to a real one, currently it isn't, does splitting a dozen tokens, with such huge variances in resulting array node sizes an issue? i'm assuming not, but I gotta make sure.
OK, code example. Essentially, you want to put items with parameters into your output text. Please consider the following... NOTE that this is not necessarily the best all-around way to do it, but I want to spin your gears here with a different (and more
PHP
way) of thinking. This example contains 3 separate files: class.nutballs.
php
, which is a content generation class, template.
php
which is your website template file and index.
php
which is the actual file that the surfer requested.
First, the index.
php
file - this is what the surfer called for:
<?
php
$path = '/www/sites/nb';
require_once("$path/class.nutballs.php
");
$nb = new nutballsOutputComponent();
$nb->titleBase = 'The Really Big Site';
$nb->message = file_get_contents("$path/messageFile.txt");
$nb->content = file_get_contents("$path/contentFile.txt");
// Here's the magic:
require_once("$path/output.php
");
?>
Now, the class.nutballs.
php
file:
<?
php
class nutballsOutputComponents()
{
public $titleBase;
public $message;
public $content;
function title()
{
// A function to return the title base with the requested URI...
return "{$this->titleBase}: {$_SERVER['REQUEST_URI']}";
}
function searchResults($theCount)
{
// ... Code to output($theCount) number of search results...
}
function shoppingCart()
{
// ... Code to display the shopping cart...
}
function showMRU($theCount)
{
// ... Code to output($theCount) lines from a most-recently-used list
}
}
Finally, here is the template.
php
file:
<?
php
echo <<<BLOCK
<html>
<head>
<title>{$nb->title()}</title>
</head>
<body>
{$nb->searchResults(10)}
<br><br><br>
{$nb->shoppingCart()}
<br><br><br>
{$nb->showMRU(5)}
{$nb->message}<br>
{$nb->content}<br>
</html>
BLOCK;
?>
I've done much the same as you - separated the content from the processor from the request... but in this way, we're using some of the best features of
PHP
. The most important here is the single-pass processing of the actual content in template.
php
, using object references within a text block - rather than parsing out and recombining the text, the single-pass processing will call the executable code where it finds it in the block. As you can see, there is no parsing in this routine at all. In a way,
PHP
is kind of doing what your code does, but at C speed and in a single pass rather than you having to process it. Additionally, it makes the separation of church and state really clear... the executable stuff is <here> while the content is <there>.
And as you can see, by separating that mess from the actual request, the index.
php
file could very easily grab any template file and use it - it does not care what the output is at all... and the output template does not care at all what the content processor is doing.
Obviously you could WAY increase the capabilities of the nutballsOutputComponent - give it properties for paths, have it understand how to get its own content, you name it - I just made it be simple and readable here.
Some might argue that the template.
php
file should actually fill a variable and then the index.
php
file should print/echo it... that's true, that is much more "one-in-one-out" thinking... I just wanted to show some different notions of ways that things can be spit back out.
I dunno - perhaps I'm just too objecty, but this kind of thinking makes way more sense to me.
/p