Website.pm -

1 Introduction
2 Usage
 2.1See it in action    
 2.2Loops (Table rows)    
 2.3Using BLOCK command (Sections)    
 2.4Weitere Beispiele / More Samples    
3 Download
4 Installation
5 To Do's
6 Alphabetical Reference

1 Introduction

Quick and easy-to-use Website rendering engine.

Website.pm - Engine to render template-driven applications, designed to run in CGI and mod_perl environments.

The goal of this Perl Module was a straight-forward syntax for handling any Web application with a related HTML template and an alternative to existing Modules where I personally found the usage was too confusing. With Website you may create dynamic and static output for Content Management Systems (CMS) based on a template.

Features

  • Creates dynamic and static rendered output
  • Block handling (Send/create partial blocks on a condition)
  • Loop processing within blocks (Example)
  • Puts Meta values into output to identify the template source (useful for debugging applications) or creates quiet output
  • Can handle your own start and stop sequences for variables so probably you had'nt change your existing templates
  • mod_perl save
  • No dependencies. Stand-alone implementation without the need to install dependent heavyweight Modules.
  • CPAN'd - just type perl -MCPAN -e "install Website" at your shell prompt wherever you need it.







home - feedback - search

2 Usage

2.1   See it in action
2.2   Loops (Table rows)
2.3   Using BLOCK command (Sections)
2.4   Weitere Beispiele / More Samples

Usage

use Website;

$ws = Website->new(
	file => PATH_TO_TEMPLATE
	[, debug     => 0|1  ]
	[, start_seq => '{%' ]
	[, stop_seq  => '%}' ]
	[, save_as   => PATH_TO_TEMPLATE ]
);

$ws->let(KEY, VALUE);
$ws->block(BLOCK_NAME, print => 0|1);

$ws->print( contentType_is_set => 0|1 [ ,quiet => 0|1 ] );

Object Parameters

Zuweisung der Variablen (Platzhalter):

$Website->let(YOUR_VARIABLE_NAME, 'your value');

Examples

use Website;

$Website = Website->new( file => 'website-tpl.htm' );
$Website->let('firstname', 'Reto');
$Website->let('lastname' , 'Hersiczky');
$Website->block('demoBlock', print => $ENV{QUERY_STRING} eq 'block=1' ? 1 : 0);

Variablen von der Struktur {-YOUR_VARIABLE_NAME-} sollen ersetzt werden:

my $Website = Website->new(
  file      => 'website_sample.htm' ,
  start_seq => '{-' ,  
  stop_seq  => '-}' 
);

Rendering auslösen (Seite Senden)

$Website->print( contentType_is_set => 0 );

Falls contentType_is_set auf 0 (false) gesetzt wird, wird der MIME Type von Website ausgegeben. Falls der MIME Type im aufrufenden Script bereits früher ausgeben wurde, muss das Rendering mit folgendem Perl Code ausgelöst werden:

$Website->print( contentType_is_set => 1);

2.1 See it in action

2.2 Loops (Table rows)

/perl/website/ files/website pl.txt
#!/usr/bin/perl -w
$| = 1;

use Website;

my $Website = Website->new( file => 'website-tpl.htm' );

$Website->let('firstname', 'Reto'     );
$Website->let('lastname' , 'Hersiczky');
$Website->block('demoBlock', print => $ENV{QUERY_STRING} eq 'block=1' ? 1 : 0);

# Populate three records
my @table_data = (
	"John	Doe	Washington"	,
	"Foo	Bar	Neverland"	,
	"Tiger	Woods	Florida"	,
);

my $i = 0;
my @rowcolors = ('#e0e0e0', 'white');

foreach (@table_data) {
	my ($firstname, $lastname, $place) = split /\t/;

	$Website->let('bcol', $rowcolors[$i++ % 2], 'tableRow');

	# -- Assign values to data cells (one table row)
	$Website->let('firstname', $firstname, 'tableRow');
	$Website->let('lastname',  $lastname , 'tableRow');
	$Website->let('place'   ,  $place    , 'tableRow');
}

$Website->print( contentType_is_set => 0, quiet => 1 );



HTML Source «website-tpl.htm»


<html>

<p>
Variable <i>firstname</i>: <b>{{firstname}}</b> <br> <br>
Variable <i>lastname </i>: <b>{{lastname}}</b>
</p>

<table cellspacing="0" cellpadding="3" border="1">
  <tr bgcolor="#cccccc">
    <th align="left">Firstname</th> 
    <th align="left">Lastname </th>
    <th align="left">Place    </th>
  </tr>

<!-- {{BLOCK tableRow}} -->
  <tr bgcolor="{{bcol}}"> <td>{{firstname}}</td> <td>{{lastname}}</td> <td>{{place}}</td></tr>
<!-- {{END}} -->

  <tr> <td colspan="3"> End of Table </td> </tr>
</table>

<!-- {{BLOCK demoBlock}} -->
<br>
[ <a href="/cgi-bin/website.pl?block=0">
	<b>Switch *off* the following block</b></a> ]<br>

<p class="rightMargin">
text text text text text text text text text text text text text text
text text text text text text text text text text text text text text
text text text text text text text text text text text text text text
</p>
<!-- {{END}} -->

</html>



See also:
/cgi bin/website.pl?block=1

2.3 Using BLOCK command (Sections)

<!-- {{BLOCK blockName}} -->
Your HTML Block Code HERE!
<!-- {{END}} -->

A BLOCK statement ...

Oben aufgeführter Block des Templates wird durch folgenden Perl Code gerendert:

$Website->block('blockName', print => 1);

Erklärung:
Durch die Zuweisung der Zahl 1 (TRUE) an den Parameter print, wird der entsprechender Block gerendert. Um den Block nicht zu rendern, müsste die Zahl auf 0 (False) gesetzt werden.


Blockzuweisungen in Schleifen

Online Demo mit Source Code:

→ Demo: www.infocopter.com/cgi-bin/website.pl?block=1

2.4 Weitere Beispiele / More Samples

my %hash = ();

$hash{'ABC'} = 'DEF'; # Variable ABC will be replaced with value "DEF"

displayForm \%hash; # Fill in all variables with its values from %hash and display the page

sub displayForm (%) {
        my $args = $_[0];
        use Website;

        my $Website = Website->new( file => 'your-tpl.htm' );
        foreach (sort keys %$args) {
                $Website->let($_, $args->{$_}) if $args->{$_};
        }
        $Website->print( contentType_is_set => 0 );
}

3 Download

Download Website.pm

Current Version

[ Download V1.14.01 (tar.gz) ]

[ or wget "http://www.infocopter.com/perl/Website/Website_pm.txt" ]

Old Versions

[ Version: 1.13.01 ]

[ modperl tested (stable) version ]

4 Installation

Three easy ways to install Website.pm

[1]  The recommended way. At shell prompt (usually as root), enter

perl -MCPAN -e "install Website"

or

[2]  If you have downloaded the tar.gz file:

gzip -cd Website-1.14.01.tar.gz | tar -x
cd Website-1.14.01

perl Makefile.PL
make
make test
make install

or

[3]  If you would like to place just the Perl Module on your system:

cd <a_directory_of_your_@INC_path>

wget "http://www.infocopter.com/perl/Website/Website_pm.txt"

5 To Do's

Was folgt als nächstes?

Zusätzlicher tuning-Parameter bei der Erzeugung eines Objekts

my $Website = Website->new(
        file   => 'yourpage.htm',
        tuning => [qw( output speed pretty cache compress )] ,
        debug  => 1
);

[ To Do's ] - Planned changes beeing requested

6 Alphabetical Reference

 B  -  D  -  E  -  F  -  H  -  I  -  L  -  M  -  R  -  S  -  T  -  U 

B  

D  

E  

F  

H  

I  

L  

M  

R  

S  

T  

U