Using templates | | |
Anyone feeling like they want to give me a 101 on general template handling in
PHP?
My history
I used to be a Roxen guy, where you could define your own HTML-tags, so that
"<foo />" could be set by "<define tag='foo'>Hello World</define>" so every
time you use the tag <foo /> it would be parse and replaced with "Hello World".
Naturally, within a <define>, any kind of roxen code can be used.
Today
I am using "php_value auto_prepend_file" and "php_value auto_append_file" to
insert a header and foot script to create the layout of a page, and function()
in required files to make small scriptlets such as headline() and stuff like
that.
The problem
With PHP, I can't make containers. Not real ones anyway. In Roxen, I could have
done this:
<define container="headline">
<span class="headline">
<contents />
</span>
</define>
So <headline>Hello!</headline>
Would become <span class="headline">Hello!</span>
You get the idea.
Now, with PHP, the above would be:
function headline($headline){
print "<span class='headline'>$headline</span>";
}
But the obvious problem is when you want to use this function for more than
just a small string - you might want a big block of HTML and PHP code to be
contained inside a HTML table with a border - for example. In Roxen this would
be done by:
<square>
Big old chunk of text, html, mysql, rxml or whatever.
</square>
And the end result would be that big chunk of text surrounded with the code of
a table. This is useful for lots of things, bbut generally for framing things
in different ways - mainly for news-boxes on pages
Currently, Im am doing this in php this way:
<?=square("start") ?>
Big old chunk of text, html, mysql, rxml or whatever.
<?=square("end") ?>
But that's just ugly code and cumbersome.
Now to the point (finally!) - How does templating workm, and could it be used
to accomplish what I am trying to do here? I am aware of 'Smarty' and I have
downloaded it and played with it, but it was just a difficult concept to grasp
for me (yeah,. so call me stupid) and it did caching and had compiled scripts
and whatever. Isn't there a smpler solution?
Nayway, I'm grateful for any help in this area. :)
--
Sandman[.net] | | | | re: Using templates
[color=blue]
> I used to be a Roxen guy, where you could define your own HTML-tags, so that [/color]
[color=blue]
> Now to the point (finally!) - How does templating workm, and could it be used
> to accomplish what I am trying to do here? I am aware of 'Smarty' and I have
> downloaded it and played with it, but it was just a difficult concept to grasp
> for me (yeah,. so call me stupid) and it did caching and had compiled scripts
> and whatever. Isn't there a smpler solution?[/color]
You might try patTemplate or simply use XSLT
____________________________________
Wil Moore III, MCP | Integrations Specialist | Assistant Webmaster | | | | re: Using templates
If you want simple, I guess you can turn on output buffering then pass
everything through strtr(). Example:
$my_own_tags = array(
'<headline>' => '<span class="headline">',
'</headline>' => '</span>',
....);
echo strtr(ob_get_clean(), $my_own_tags);
Uzytkownik "Sandman" <mr@sandman.net> napisal w wiadomosci
news:mr-E1102B.00305519032004@news.fu-berlin.de...[color=blue]
> Anyone feeling like they want to give me a 101 on general template[/color]
handling in[color=blue]
> PHP?
>
> My history
> I used to be a Roxen guy, where you could define your own HTML-tags, so[/color]
that[color=blue]
> "<foo />" could be set by "<define tag='foo'>Hello World</define>" so[/color]
every[color=blue]
> time you use the tag <foo /> it would be parse and replaced with "Hello[/color]
World".[color=blue]
> Naturally, within a <define>, any kind of roxen code can be used.
>
> Today
> I am using "php_value auto_prepend_file" and "php_value auto_append_file"[/color]
to[color=blue]
> insert a header and foot script to create the layout of a page, and[/color]
function()[color=blue]
> in required files to make small scriptlets such as headline() and stuff[/color]
like[color=blue]
> that.
>
> The problem
> With PHP, I can't make containers. Not real ones anyway. In Roxen, I could[/color]
have[color=blue]
> done this:
>
> <define container="headline">
> <span class="headline">
> <contents />
> </span>
> </define>
>
> So <headline>Hello!</headline>
> Would become <span class="headline">Hello!</span>
>
> You get the idea.
>
> Now, with PHP, the above would be:
>
> function headline($headline){
> print "<span class='headline'>$headline</span>";
> }
>
> But the obvious problem is when you want to use this function for more[/color]
than[color=blue]
> just a small string - you might want a big block of HTML and PHP code to[/color]
be[color=blue]
> contained inside a HTML table with a border - for example. In Roxen this[/color]
would[color=blue]
> be done by:
>
> <square>
> Big old chunk of text, html, mysql, rxml or whatever.
> </square>
>
> And the end result would be that big chunk of text surrounded with the[/color]
code of[color=blue]
> a table. This is useful for lots of things, bbut generally for framing[/color]
things[color=blue]
> in different ways - mainly for news-boxes on pages
>
> Currently, Im am doing this in php this way:
>
> <?=square("start") ?>
> Big old chunk of text, html, mysql, rxml or whatever.
> <?=square("end") ?>
>
> But that's just ugly code and cumbersome.
>
> Now to the point (finally!) - How does templating workm, and could it be[/color]
used[color=blue]
> to accomplish what I am trying to do here? I am aware of 'Smarty' and I[/color]
have[color=blue]
> downloaded it and played with it, but it was just a difficult concept to[/color]
grasp[color=blue]
> for me (yeah,. so call me stupid) and it did caching and had compiled[/color]
scripts[color=blue]
> and whatever. Isn't there a smpler solution?
>
> Nayway, I'm grateful for any help in this area. :)
>
> --
> Sandman[.net][/color] | | | | re: Using templates
"Sandman" <mr@sandman.net> wrote:
[color=blue]
> Currently, Im am doing this in php this way:
> <?=square("start") ?>
> Big old chunk of text, html, mysql, rxml or whatever.
> <?=square("end") ?>[/color]
Just want to mention that i usually include the contents, not the "borders":
<div id="submenu">
<? include submenu.php?>
</div>
This gives the benefit of updating easily the contents that are all over the
site. And when those contents are php, they speculate form variables and
change automatically according to them. | | | | re: Using templates
In article <3zv6c.9$_p.0@read3.inet.fi>,
"Perttu Pulkkinen" <perttu.pulkkinen@co.jyu.fi> wrote:
[color=blue]
> "Sandman" <mr@sandman.net> wrote:
>[color=green]
> > Currently, Im am doing this in php this way:
> > <?=square("start") ?>
> > Big old chunk of text, html, mysql, rxml or whatever.
> > <?=square("end") ?>[/color]
>
> Just want to mention that i usually include the contents, not the "borders":
>
> <div id="submenu">
> <? include submenu.php?>
> </div>
>
> This gives the benefit of updating easily the contents that are all over the
> site. And when those contents are php, they speculate form variables and
> change automatically according to them.[/color]
Sorry, that won't help in my situations.
--
Sandman[.net] | | | | re: Using templates
In article <fa6dncg7noGI58fdRVn-uQ@comcast.com>,
"Chung Leong" <chernyshevsky@hotmail.com> wrote:
[color=blue]
> If you want simple, I guess you can turn on output buffering then pass
> everything through strtr(). Example:
>
> $my_own_tags = array(
> '<headline>' => '<span class="headline">',
> '</headline>' => '</span>',
> ...);
>
> echo strtr(ob_get_clean(), $my_own_tags);[/color]
Ok, sounds interesting - exactly how do I do it? Is it called at the very last
in the script or could I call it in the beginning? Could it be used with
external files? Like so:
## headline.txt
<span class='headline'>
&contents; <- To be replaced somehow.
</span>
## init.php (loaded every time - before the current page)
???
## index.php:
<headline>Hello!</headline>
Looking at php.net, it seems ob_get_clean() fetches the current buffer, so
executing it in the beginning of the script won't do much. Hmmm...
--
Sandman[.net] | | | | re: Using templates
Uzytkownik "Sandman" <mr@sandman.net> napisal w wiadomosci
news:mr-23BF2C.12250319032004@news.fu-berlin.de...[color=blue]
> Ok, sounds interesting - exactly how do I do it? Is it called at the very[/color]
last[color=blue]
> in the script or could I call it in the beginning? Could it be used with
> external files? Like so:
>
> ## headline.txt
> <span class='headline'>
> &contents; <- To be replaced somehow.
> </span>
>
> ## init.php (loaded every time - before the current page)
> ???
>
> ## index.php:
> <headline>Hello!</headline>[/color]
You would call ob_start() in init.php and strtr(ob_get_clean(), ...) in the
file that's included at the end, or you can give a callback to ob_start()
which gets called automatically when the page is flushed. Example
init.php:
function add_custom_tag($name, $content) {
global $custom_tags;
$custom_tages["<$name />"] = $content;
}
function add_custom_container($name, $start, $end) {
global $custom_tags;
$custom_tages["<$name>"] = $start;
$custom_tages["</$name>"] = $end;
}
function replace_custom_tags($buffer) {
global $custom_tags;
return strtr($buffer, $custom_tags);
}
load_tag_definitions('custom_tags.txt');
ob_start('replace_custom_tags');
For the tag definitions, you can use Roxen's syntax with something like
this:
function load_tag_definitions($filename) {
$def = file_get_contents($filename);
if(preg_match_all('!<define tag="(.*?)">(*.?)</define>!s', $def,
$matches)) {
$tags = $matches[1];
$content = $matches[2];
foreach($tags as $index => $tag) {
add_custom_tag($tag, $content[$index]);
}
}
if(preg_match_all('!<define container="(.*?)">(*.?)<content
/>(*.?)</define>!s', $def, $matches)) {
$tags = $matches[1];
$start = $matches[2];
$end = $matches[3];
foreach($tags as $index => $tag) {
add_custom_container($tag, $start[$index], $end[$index]);
}
}
} | | | | re: Using templates
"Sandman" <mr@sandman.net> wrote in message
news:mr-E1102B.00305519032004@news.fu-berlin.de...[color=blue]
> The problem
> With PHP, I can't make containers. Not real ones anyway. In Roxen, I could[/color]
have[color=blue]
> done this:
>
> <define container="headline">
> <span class="headline">
> <contents />
> </span>
> </define>[/color]
Maybe this could help: http://www.templatetamer.org/index.p...mplatesExample
rush
-- http://www.templatetamer.com/ |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|