Re: [PHPLIB] layout_html.inc From: Alexander Aulbach (ssilk <email protected>)
Date: 02/25/00

On Thu, 24 Feb 2000, Michael Vanecek wrote:

}Has _anyone_ used the layout_html.inc file? I've searched the archives
}and have only found two other unanswered questions about this and no
}solutions. If anyone has some examples on using this lib, I'd really
}appreciate a post or private email. Perhaps a helloworld.php3 based on
}it. I'm a little new to arrays and pulling information from arguments
}based on arrays, so it's got me a little stumped. Thanks a million...

Hummm... sorry about this lib... I wrote it for an project that users
can configure the layout of a page in a wide spread.

It's not quite easy to understand how to use it. Again, sorry.

The idea is, that the layout of a page is defined by colors, tables etc.
etc. (I call them "layout-elements" or "HTML-fragments") and could be
changed as fast as possible, without reprogramming the hole application or
writing different versions for every layout.

If you take a look at the file layout_html.inc, that's the first part of the
lib: It's a definiton of how a BODY-tag is locking, what kind of CSS is
used, how an IMG-tag looks like...

In most cases the layout of an HTML-Element is defined by two arrays:

        ################################################# TABLE
        var $table = ARRAY(
                'tabletag' => "<TABLE%s>\n",
                'CELLPADDING' => ' CELLPADDING="%s"',
                'CELLSPACING' => ' CELLSPACING="%s"',
                'BORDER' => ' BORDER="%s"',
                'WIDTH' => ' WIDTH="%s"',
                'BGCOLOR' => ' BGCOLOR="%s"',
                'ALIGN' => ' ALIGN="%s"',
                'endtable' => '</TABLE>'
        );
        var $table_def = ARRAY(
                'CELLPADDING' => '2',
                'CELLSPACING' => '0',
                'BORDER' => '0',
                'WIDTH' => '100%',
                'BGCOLOR' => '',
                'ALIGN' => ''
        );

The first Array defines, what values for the TABLE-tag are valid, the second
inserts default values. In this case, the return-value of

echo $lay->beg_table()

will return

<TABLE CELLPADDING="2" CELLSPACING="0" BORDER="0" WIDTH="100%">

A simple change

$lay->table_def[BGCOLOR]="#ABCDEF";

will now create all tables with an default background color #ABCDEF.

I tried to make the adjustments in a way, that it is mainly looking
neutral.

To make other adjustments you have two ways: Use the file local.inc
and insert a part looking like the following:
-----------------------------------------------------------------------
class pconf_Lay extends Layout {
        var $body_def = ARRAY (
            'BGCOLOR' => '#E1E1E1',
            'BACKGROUND' => '',
            'TEXT' => '#000000',
            'LINK' => '#0000C0',
            'VLINK' => '#0000A0',
            'ALINK' => '#8000FF'
        );

        var $bodytag = ARRAY(
                'endhead' => "</HEAD>\n",
                'bodytag' => "<BODY%s>\n<P>
<TABLE BGCOLOR=\"#444444\" ALIGN=right CELLPADDING=1 CELLSPACING=0
BORDER=0><TR><TD>
<TABLE BGCOLOR=\"#999999\" CELLPADDING=5 CELLSPACING=0 BORDER=0>
<TR ALIGN=center><TD><FONT COLOR=\"#BBBBBB\"><B><BIG>Proxy Config<BR>
INSERT FIRM HERE<BR>INSERT DEPARTMENT HERE</TD></TR></TABLE></TD></TR></TABLE>
\n",
                'BGCOLOR' => ' BGCOLOR="%s"',
                'BACKGROUND' => ' BACKGROUND="%s"',
                'TEXT' => ' TEXT="%s"',
                'LINK' => ' LINK="%s"',
                'VLINK' => ' VLINK="%s"',
                'ALINK' => ' ALINK="%s"',
                'onLoad' => ' onLoad="%s"',
                'endbody' => "%s</P>\n</BODY>\n</HTML>\n"
        );
}
------------------------------------------------------------------------

This redefines the default colors and changes the BODY-Tag in a way, that
directly after it it will display a small right-aligned table with some
Info... sorry, this is not very well programmed - a nicer way would be to
define a special var, so that you don't have to redefine the hole array
$bodytag... but to use it, you have to change the function. Be free to make
such changes!

Or you can do the following:

$lay=new Layout;
$lay->body_def = ARRAY (
            'BGCOLOR' => '#E1E1E1',
            'BACKGROUND' => '',
            'TEXT' => '#000000',
            'LINK' => '#0000C0',
            'VLINK' => '#0000A0',
            'ALINK' => '#8000FF'
        );
$lay->bodytag[bodytag]="<BODY%s>\n<P>
<TABLE BGCOLOR=\"#444444\" ALIGN=right CELLPADDING=1 CELLSPACING=0
BORDER=0><TR><TD>
<TABLE BGCOLOR=\"#999999\" CELLPADDING=5 CELLSPACING=0 BORDER=0>
<TR ALIGN=center><TD><FONT COLOR=\"#BBBBBB\"><B><BIG>Proxy Config<BR>
INSERT FIRM HERE<BR>INSERT DEPARTMENT HERE</TD></TR></TABLE></TD></TR></TABLE>
\n";

Perhaps better to understand?

The idea is now, that you define a set of such changes. For example the
"Amber"-Set:

$lay->body_def = ARRAY (
            'BGCOLOR' => '#E1AAAA',
            'BACKGROUND' => '/pics/amber.gif',
            'TEXT' => '#550000',
            'LINK' => '#C00000',
            'VLINK' => '#A00000',
            'ALINK' => '#FF0080'
        );

The same with the "Mint"-set, or "Night"-Set, and so on.

You save this set as includes and just include them at the right places with
the right filename:

$lay=new Layout;
include("layouts/$user_layout.inc");

... for example.

Ok, and now how to use it in your program.

I for myself currently use this class for some reasons: It's just, cause I
think, that it makes the code more readable and you can change layout of
your application at a central place and don't have to think about it in any
way - as long as you are using standard looking pages. So it is more or less
ideal for intranet-applications, that have to be adapted for another firm.

A program using this lib looks like this:
--------------------------------------------------------------------------
...
## initializing stuff
## for this example I'm also using the Menu-class
...

echo $lay->doc_begin(). ## returns the string $docbegin
     $lay->htmlhd($menu->get_title()). ## returns <TITLE> ...</TITLE>
     $lay->beg_body(); ## the rest of the header

?>
<H3>Liste Tabellen</H3>
<BR CLEAR=all>
<?php

echo $lay->beg_table();
echo "<TR ALIGN=left><TH>";
echo "<SMALL>Tabellenname</TH><TH><SMALL>Beschreibung, Kommentar</TH>";
echo "</TR><TR>";

$db2=new pconf_DB ;

$db->query("SELECT * FROM config_tables WHERE ct_mandant='$_c[mymandant]'");

$i=0; while ( $db->next_record() ) {
        if (($i++)%2) $color='#CCCCEE';
        else $color='#EEEEFF';
        $x=$db->Record;

        echo $lay->beg_tabrow(ARRAY(BGCOLOR=>$color)).$lay->beg_tabcell();
?>
<A HREF="list_ce.html?ct_id=<? echo $x[ct_id] ?>"><?
 echo $x[ct_tabname]
?></A><?

        echo $lay->end_tabcell().$lay->beg_tabcell();

        echo ereg_replace("\n.*$","",$x[ct_descript]);

        echo $lay->end_tabcell().$lay->beg_tabcell();

        echo "* <SMALL>".show_listlog('config_tables',$x[ct_id],$db2);

        echo $lay->end_tabcell().$lay->end_tabrow();

}

echo $lay->end_table();

?>
<P><SMALL>Klick auf Tabellenname -> Alle Einträge der Tabelle auflisten<br>
[edit] -> den Eintrag bearbeiten

</P>
<?

$menu->Show();

echo $lay->end_body().$lay->doc_end();

page_close();

?>
---------------------------------------------------------------------------

You see, that I'm using it chiefly for the table-generation. The reason is
simple: Otherwise I always make errors. :-)

You see also, that I mix HTML and Layout-class just as I need it.
This is, in the case it is an special application which will never be
changed to completly different layout. In this case, it could also make
sense to define own layout-functions, for example a table surrounded by a
"shadow-table" or somthing like that.

I translated the listing above into HTML:

--------------------------------------------------------------------------
...
## initializing stuff
## for this example I'm also using the Menu-class
...

?>
<HTML><HEAD>

... here comes a big CSS and a javacript in my example, I will not copy it
here, it's too long, but I defined it only once in my layout-class and
it will do the rest for me ...

<TITLE><? echo $menu->get_title() ?></TITLE>
<BODY BGCOLOR="#E1AAAA" TEXT="#550000" LINK="#C00000" VLINK="#A00000"
ALINK="#FF0080">

<P><TABLE BGCOLOR=\"#444444\" ALIGN=right CELLPADDING=1
CELLSPACING=0BORDER=0><TR><TD><TABLE BGCOLOR=\"#999999\" CELLPADDING=5
CELLSPACING=0 BORDER=0><TR ALIGN=center><TD><FONT
COLOR=\"#BBBBBB\"><B><BIG>Proxy Config<BR>INSERT FIRM HERE<BR>INSERT
DEPARTMENT HERE</TD></TR></TABLE></TD></TR></TABLE>

<H3>Liste Tabellen</H3>
<BR CLEAR=all>
<TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 BORDER=0>
<TR ALIGN=left><TH>
<SMALL>Tabellenname</TH><TH><SMALL>Beschreibung, Kommentar</TH>
</TR>

<?
$db2=new pconf_DB ;

$db->query("SELECT * FROM config_tables WHERE ct_mandant='$_c[mymandant]'");

$i=0; while ( $db->next_record() ) {
        if (($i++)%2) $color='#CCCCEE';
        else $color='#EEEEFF';
        $x=$db->Record;

?>
<TR ALIGN=top BGCOLOR="<? echo $color ?>"><TD>
<A HREF="list_ce.html?ct_id=<? echo $x[ct_id] ?>"><?
 echo $x[ct_tabname]
?></A>
</TD><TD>
<?
        echo ereg_replace("\n.*$","",$x[ct_descript]);
?>
</TD><TD>
<?
        echo "* <SMALL>".show_listlog('config_tables',$x[ct_id],$db2);
?>
</TD></TR>
<?

}

echo "</TABLE>"

?>
<P><SMALL>Klick auf Tabellenname -> Alle Einträge der Tabelle auflisten<br>
[edit] -> den Eintrag bearbeiten

</P>
<?

$menu->Show();

?>
</BODY>
</HTML>
<?

page_close();

?>
---------------------------------------------------------------------------

And in my eyes it looks nicer using Layout-class, although it is a little
bit more writing and more to do.

I also use very often doc_pic, cause this generates a complete IMG-tag with
correct WIDTH and HEIGHT. Seems to be very practical.

At least a summary of the advantages:

- Easy change of layout. Expecially if you write more functions for
specialized layout you can change the layout of an application completly.
- Seems to be ideal for INTRANET-Applications, where the design of the
surface isn't such important but the corporate identity of the application.
- A combination of traditional HTML-programming together with using
Layout-class will result in a cleaner code.
- Some functions of layout seems to be always useful.
- What I havn't tried yet: A combination of Layout-Class together with
Template-class! I think this could be a very nice combination.
- If you prepare the default-layout of all tags in a good way, you have to
write much less than with using pure HTML, especially if you are using
Tables.

Some disadvantages:

- It dosn't support Style-Sheets and Style Classes in a complete manner (I
needn't it for my project). With this, changes could be done more easy. But
I think, this isn't difficult to implement.
- The current CSS is horrible... .)
- All names used in the associative Arrays should use lowercased chars.
  Currently it is a mix, so you can easy miswrite it.
- a little bit more overhead to everything.
- slows down application
- Changes in layout or different layouts couldn't be done by an
HTML-Designer. They must be implemented by the programmer, cause the layout
cannot be defined as HTML but as PHP-Code.
- The concepts are not easy to understand.

Be free to add and change things in this class. You could always ask me
directly, if you have questions...

-- 

SSilk - Alexander Aulbach - Herbipolis/Frankonia Minoris - (0931)22032

- PHP3 Base Library Mailing List. Send messages to <phplib <email protected>>. To unsubscribe, send "unsubscribe" to <phplib-request <email protected>> in the body, not the subject, of your message.