File: documentation-4.html

package info (click to toggle)
phplib 1%3A7.3dev-3.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,752 kB
  • ctags: 247
  • sloc: php: 6,659; perl: 323; pascal: 157; makefile: 102; sh: 7
file content (788 lines) | stat: -rw-r--r-- 29,621 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
 <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
 <TITLE>PHP Base Library Documentation, Release phplib_7_2: Extended functionality</TITLE>
 <LINK HREF="documentation-5.html" REL=next>
 <LINK HREF="documentation-3.html" REL=previous>
 <LINK HREF="documentation.html#toc4" REL=contents>
</HEAD>
<BODY>
<A HREF="documentation-5.html">Next</A>
<A HREF="documentation-3.html">Previous</A>
<A HREF="documentation.html#toc4">Contents</A>
<HR>
<H2><A NAME="s4">4. Extended functionality</A></H2>

<P>
<P>The section on extended functionality covers non-GUI classes
that provide often needed application functions without a user
interface. Some extended classes depend on core functionality,
some contain independent classes.
<P>Extended classes are treated differently from core classes in
that their code is not automatically included by
<CODE>prepend.php3</CODE>. You have to include the class definition
manually where needed or you modify <CODE>prepend.php3</CODE>.
<P>
<H2><A NAME="ss4.1">4.1 Cart</A>
</H2>

<P>
<P>The Cart class is programmatically independent, but makes sense
only if its instances are made persistent in some way. The Cart
class automatically registers itself as a session variable in
its <CODE>start()</CODE> function.
<P>Cart implements a shopping cart. At the moment, items within the
shopping cart are independent of each other; the cart can only
hold simple things. Support for compound articles that require
other articles to function and provide a base for dependent
articles is to be added at a future time.
<P>An example of a simple article is any article with no options,
for example an apple or a book. Common examples for compound
articles are a pizza (which requires a foundation in either
American or Italian style, a selection of toppings, and cheese,
to function correctly) and a computer system (which requires a
housing, a motherboard, RAM, a video card, etc to function
correctly).
<P><EM>Note:</EM> <CODE>Cart</CODE> was a core class up to <EM>release-5</EM>. If
your applications uses the <CODE>Cart</CODE> class, you <EM>must</EM>
manually add the statement <CODE>include("cart.inc")</CODE> to your
<CODE>prepend.php3</CODE> file where indicated in that file.
<P><EM>Note:</EM> The page management functions do no longer support
the feature <CODE>cart</CODE> to set up and start the cart class. It is
recommended that you use <CODE>Session</CODE>'s <CODE>auto_init</CODE> feature
instead to start your cart automatically or that you manually
set up your cart.
<P>
<H3>Instance variables</H3>

<P>
<P>
<CENTER><TABLE BORDER><TR><TD>
<BR>
classname</TD><TD>Serialization helper: The name of this class.</TD></TR><TR><TD>
persistent_slots</TD><TD>Serialization helper: The names of all persistent slots.</TD></TR><TR><TD>
item</TD><TD>Multidimensional array of items in the cart.</TD></TR><TR><TD>
currentItem</TD><TD>A counter for item positions.</TD></TR><TR><TD>

<CAPTION>Accessible instance variables.</CAPTION>
</TD></TR></TABLE></CENTER>
<P>
<H3>Instance methods</H3>

<P>
<P>
<H3>Accessible instance methods</H3>

<P>
<P>
<DL>
<P>
<DT><B>check($art)</B><DD><P> 
Checks that an item with the given article number <CODE>$art</CODE> is
in the cart. Returns an array of a boolean value and an integer
number. If the boolean is true, there are number many articles
of that article number in the cart.
<P>
<DT><B>reset()</B><DD><P> 
Deletes all items in current cart, resetting $this->currentItem to
1. Always returns true.
<P>
<DT><B>num_items()</B><DD><P>Returns the number of articles in the current shopping cart, or
false if cart is empty. For compatibility reasons, this function
is available as <CODE>tot_arts</CODE> as well (but will print
a warning if used by this name).
<P>
<DT><B>add_item($art, $num)</B><DD><P>Add <CODE>$num</CODE> many articles of article number <CODE>$art</CODE> to the current
shopping cart. Returns the position number of <CODE>$art</CODE> in the
shopping cart.
<P>
<DT><B>remove_item</B><DD><P>Remove <CODE>$num</CODE> many articles of article number <CODE>$art</CODE> from the
shopping cart, if there are at least that many articles in the
cart. Returns the position number of <CODE>$art</CODE> in the shopping cart
or false, if there weren't enough <CODE>$art</CODE> to remove them from the
cart. If the function does return false, the cart has not been
modified.
<P>
<DT><B>set_item</B><DD><P>Set the quantity of article number <CODE>$art</CODE> in the shopping cart to 
exactly <CODE>$num</CODE>. If <CODE>$num</CODE> is set to zero, article is removed from
cart. Returns the position number of <CODE>$art</CODE> in the shopping cart.
<P>
<DT><B>show_all()</B><DD><P>If the shopping cart is empty, it will call
<CODE>show_empty_cart()</CODE> once and then return.
<P>Calls <CODE>show_item_open()</CODE> once at the beginning of a shopping
cart listing. Then calls <CODE>show_item()</CODE> once for each item in
the shopping cart. Calls <CODE>show_item_close()</CODE> once at the end
of a shopping cart listing.
<P>
<DT><B>show_item($art, $num)</B><DD><P>This function should be provided by the user. It renders the
HTML to display a single item from the cart. <CODE>$art</CODE> is the
article number of the item and there are <CODE>$num</CODE> of these in
the cart.
<P>
<DT><B>show_cart_open()</B><DD><P>This function should be provided by the user. It renders the
prologue HTML to display a shopping cart listing.
<P>
<DT><B>show_cart_close()</B><DD><P>This function should be provided by the user. It renders the
epilogue HTML to display a shopping cart listing.
<P>
<DT><B>show_empty_cart</B><DD><P>This function should be provided by the user. It should render
an appropriate message to symolize an empty cart.
</DL>
<P>
<H3>Example</H3>

<P>
<P>Use a subclass of <CODE>Cart</CODE> to provide an implementation of
<CODE>show_item()</CODE>.
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
class My_Cart extends Cart {
  var $classname = "My_Cart";

  // Look up article numbers...
  var $database_class = "DB_Article";
  var $database_table = "articles";
  var $db;
  
  var $sum = 0;

  function show_cart_open() {
    printf("&lt;table class=cart_table&gt;\n");
    $this-&gt;sum = 0;
  }
  
  function show_cart_close() {
    printf("&lt;/table&gt;\n");
    printf("That's a total of %s.\n", $this-&gt;sum);
  }

  function show_item($art, $num) {
    if (!is_object($this-&gt;db)) {
      $class    = $this-&gt;database_class;
      $this-&gt;db = new $class;
    }
    
    $query = sprintf("select * from %s where artid = '%s'",
      $this-&gt;database_table,
      $art);
    $this-&gt;db-&gt;query($query);

    while($this-&gt;db-&gt;next_record()) {
      printf(" &lt;tr class=cart_row&gt;\n  &lt;td class=cart_cell&gt;%s&lt;/td&gt;\n",
        $this-&gt;db-&gt;Record["name"]);
      printf("  &lt;td class=cart_cell&gt;%s&lt;/td&gt;\n", 
        $this-&gt;db-&gt;Record["price"]);
      printf("  &lt;td class=cart_cell&gt;%s&lt;/td&gt;\n",
        $num);
      $rowsum = $num * $this-&gt;db-&gt;Record["price"];
      $this-&gt;sum += $rowsum;
      printf("  &lt;td class=cart_cell&gt;%s&lt;/td&gt;\n",
        $rowsum);
      printf(" &lt;/tr&gt;\n");
    }
  }
}
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>To use a cart, create an instance of your <CODE>Cart</CODE> subclass and
call <CODE>start()</CODE>. This will automatically register <CODE>cart</CODE>.
<P>It is recommended that you set in your <CODE>Session</CODE> subclass
in <CODE>local.inc</CODE> the slot <CODE>$auto_init</CODE> to the value
<CODE>setup.inc</CODE> and create an include file of that name which
contains the following code:
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
  global $cart;               ## $cart is a global variable.
  $cart = new My_Cart; ## Make a My_Cart instance named $cart
  $cart-&gt;start();          ## and have it register itself.
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>Use <CODE>add_item()</CODE> and <CODE>remove_item</CODE> to work with your Cart:
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
  $cart-&gt;add_item("101", 2);    ## Add two pieces of "101"
  $cart-&gt;remove_item("101", 1); ## Drop one piece of "101"
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>Use <CODE>show_all()</CODE> to display the contents of your cart.
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
  $cart-&gt;show_all();    ## What's in a cart, anyway?
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>
<H3>On using Cart</H3>

<P>To make use of the Cart class, you need to define a new table
in your database that lists all articles you shop should sell.
With PHPLIB and MySQL we recommend that you create a new
instance of PHPLIB for each virtual web server and a new database
for each customer. This database should hold the
active_sessions and auth_user tables as well as all
application specific tables like for example the article
list. In other words, with MySQL we strongly discourage
that you use PHPLIB and the MySQL directive <CODE>use</CODE>
<EM>database_name</EM> together. There is no support
if you do (there is no support if you do as we say, too,
because PHPLIB is an open source product you are using on your
own risk, but ...).
<P>So let us assume you define a very simple new table <CODE>articles</CODE>
with a structure like this:
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
#
# Table structure for table 'articles'
#
CREATE TABLE articles (
  name text,
  price float(8,2),
  artid int(11) DEFAULT '0' NOT NULL auto_increment,
  PRIMARY KEY (artid)
);
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>This table has an article number called <CODE>artid</CODE>, and for each
<CODE>artid</CODE>  there is an article description <CODE>name</CODE> and a <CODE>price</CODE>. You
may extend this minimal definition for your purposes by adding
article groups, BLOBs with article images and more, but this will
suffice for our example purposes.
<P>Populate this table with some products that suit your taste.
<P>The next step is to teach PHPLIB about the cart class. Three
steps are necessary to do so:
<P>
<UL>
<LI>the <CODE>Cart</CODE> class has to be included on every page. Even on
that pages that do not make use of the <CODE>Cart</CODE> class.

On that pages that use <CODE>Cart</CODE>, a cart subclass is instantiated and
saved. On all subsequent pages, that <CODE>Cart</CODE> object is recreated and
to be able to recreate the <CODE>Cart</CODE> object, PHP must know what a <CODE>Cart</CODE>
object is. Since you cannot know which pages a user loads after
he has put the first item into the <CODE>Cart</CODE>, we need to have a
definition for <CODE>Cart</CODE> on <EM>all</EM> pages.

The proper place to include the <CODE>Cart</CODE> definition from <CODE>cart.inc</CODE> is
consequently <CODE>prepend.php3</CODE>. Edit <CODE>prepend.php3</CODE> and
<CODE>require("cart.inc")</CODE> as indicated by the comments in that file.
</LI>
<LI>a subclass of <CODE>Cart</CODE> has to be created to suit your tastes.

Your subclass of <CODE>Cart</CODE> will be called <CODE>Example_Cart</CODE> in this example.
You may actually name it as you like, but you have to be
consistent.

The definition of <CODE>Example_Cart</CODE> goes into <CODE>local.inc</CODE> anywhere below
your definition for <CODE>Example_Session</CODE>. It looks like this

<BLOCKQUOTE><CODE>
<HR>
<PRE>
class Example_Cart extends Cart {
  var $classname = "Example_Cart";

}
</PRE>
<HR>
</CODE></BLOCKQUOTE>


and we will add additional code later in this example. That
additional code will teach your shopping cart about the database
table that holds your articles and so on.
</LI>
<LI>finally, you need to create an instance of your shopping cart
class so that you have an object that actually holds the articles
selected by the user.

We will use a very nifty feature of PHPLIB to create that object
instance: If you set up PHPLIB properly, it is able to load and
execute an include file every time a session is being created. We
call this feature <CODE>auto_init</CODE>, after the instance variable of
Session that controls it.

Go into <CODE>local.inc</CODE> and edit your subclass of <CODE>Session</CODE>. You will
have some code like

<BLOCKQUOTE><CODE>
<HR>
<PRE>
class Example_Session extends Session {
  var $classname = "Example_Session";

...
}
</PRE>
<HR>
</CODE></BLOCKQUOTE>


in your <CODE>local.inc</CODE>. Add
a line like

<BLOCKQUOTE><CODE>
<HR>
<PRE>
  var $auto_init = "setup.inc",
</PRE>
<HR>
</CODE></BLOCKQUOTE>


to your definition of <CODE>Example_Session</CODE> and create a file
<CODE>setup.inc</CODE> in the same directory that holds your local.inc.
Whatever code is in this file will be executed every time we
create a new session. The code is being executed after your
<CODE>$sess</CODE>, <CODE>$auth</CODE> and <CODE>$perm</CODE> objects are loaded and
initialized, but does run from within a function context. You
have to <CODE>global</CODE> everything you define to export it from that
function context.

In <CODE>setup.inc</CODE>, create a global instance of
<CODE>Example_Cart</CODE> named <CODE>$cart</CODE> and register that variable
with PHPLIB:

<BLOCKQUOTE><CODE>
<HR>
<PRE>
&lt;?php
  global $cart;
  $cart = new Example_Cart;

  // $sess is already global
  $sess->register("cart");
 ?&gt;
</PRE>
<HR>
</CODE></BLOCKQUOTE>
</LI>
</UL>
<P>Now you have a <CODE>$cart</CODE> object available by default on every
page that uses PHPLIB. That object is created automatically at
session startup, is carried from page to page by PHPLIBs session
management and is destroyed by the garbage collection that reaps
session records. You do not have to worry anymore about that
cart, but simply use it anytime between <CODE>page_open()</CODE>
and <CODE>page_close()</CODE>. PHPLIB does the rest for you.
<P>The <CODE>Cart</CODE> class is actually dead stupid. It maintains an array
<CODE>$cart-&gt;item[]</CODE> that holds records about what the user bought. Each
<CODE>$cart-&gt;item[$x]</CODE> consists of a
<CODE>$cart-&gt;item[$x]["art"]</CODE>, which is
the article number of an item the user wants to buy and of a
<CODE>$cart-&gt;item[$x]["num"]</CODE>, which is the # of items with that article
number that are wanted. <CODE>$cart-&gt;currentItem</CODE> is the next $x to use
for articles added to <CODE>$cart-&gt;item[]</CODE>.
<P>You add articles to the shopping cart with
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
$x = $cart->add_item($art, $num)
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>This will add <CODE>$num</CODE> items with the article number <CODE>$art</CODE>
to your cart contents. If you already have an item with that
article number in your cart, the count for that article is
increased by <CODE>$num</CODE>. Otherwise a new article entry is being
created and set to <CODE>$num</CODE>. The function does return the
<CODE>$x</CODE> index into the <CODE>$cart-&gt;item[]</CODE> array for that article.
<P>To remove an item from the shopping cart, code
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
$x = $cart->remove_item($art, $num)
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>This will remove <CODE>$num</CODE> items with the article number
<CODE>$art</CODE> from your cart, if there are that many items in your
shopping cart. If you do not have the <CODE>$art</CODE> in your cart or
there are not <CODE>$num</CODE> many <CODE>$art</CODE> in your cart, the
function will return false and not remove anything from the
cart. Otherwise, <CODE>$num</CODE> articles with article number
<CODE>$art</CODE> are taken out of the cart and if the count for that
article drops to zero while doing this, we even unset the array
element.
<P>You may check how many articles with a given article number are
in the cart:
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
list($have, $num) = $cart->check($art)
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>The check function does return a two-element array. The first
element <CODE>$have</CODE> is true, if we have the wanted article in the cart.
If <CODE>$have</CODE> is true, <CODE>$num</CODE> holds the number of articles with that
number in the cart, otherwise <CODE>$num</CODE> is undefined (actually, it is
0, but you must not rely on that).
<P>Finally, we have a function
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
$cart->show_all()
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>which you may call to walk your shopping cart and have
Example_Cart to generate a list of articles in your cart. That
function will first call <CODE>$cart-&gt;show_cart_open()</CODE>, for which you
may provide code in your subclass. It will then call
<CODE>$cart-&gt;show_item($art, $num)</CODE> for each item in the cart. We have a
stupid default implementation for that function in Cart, but you
may provide more sophisticated code in <CODE>Example_Cart</CODE> for that, too.
Finally, at the end of your cart listing,
<CODE>$cart-&gt;show_cart_close()</CODE> is being called, which again may be code
of yours.
<P>The example in the previous section shows a more
sophisticated implementation of a Cart subclass. That
implementation uses show_cart_open() to create an opening 
table tag (formatted with a CSS class) and sets a counter
<CODE>$cart-&gt;sum</CODE> to
zero.
<P>In show_cart_close(), the table is being closed and the
<CODE>$cart-&gt;sum</CODE> counter is printed.
<P>As you might have guessed, <CODE>show_item($art, $num)</CODE> queries the
database for each article number, retrieves the article
description and prices and finally sums up all prices, taking the
number of articles per article into consideration. It also
generates table rows, printing a nice receipt for the customer.
<H2><A NAME="ss4.2">4.2 Template</A>
</H2>

<P><EM>Note:</EM> If you think that this is like FastTemplates, read
carefully. It isn't.
<P>
<P> 
The template class allows you to keep your HTML code in some
external files which are completely free of PHP code, but
contain replacement fields. The class provides you with
functions which can fill in the replacement fields with
arbitrary strings. These strings can become very large, e.g.
entire tables.
<P>
<P>
<H3>Instance variables</H3>

<P>
<P>
<CENTER><TABLE BORDER><TR><TD>
<BR>
classname</TD><TD>String. Serialization helper: The name of this class.</TD></TR><TR><TD>
debug</TD><TD>Integer, Bitmask: set 1 bit to see all variable assignments,set 2 bit to see all variable accesses,set 4 bit to see internal function references.</TD></TR><TR><TD>
unknowns</TD><TD>One of "keep", "comment", "remove" (Default).Determines how to handle unresolved variable names intemplates upon output. If set to "keep", those are leftuntouched. If set to "comment", unresolved variable names aretransformed into HTML comments reporting the error. If set to"remove", unresolved variable names are silently removed (thedefault).</TD></TR><TR><TD>
halt_on_error = "yes"</TD><TD>One of "yes"(Default), "report", "no". Determines how Template handleserror conditions. If set to "yes" (the Default), the error isreported, then execution is halted. If set to "report", theerror is reported, then execution continues by returning"false". If set to "no", errors are silently ignored, andexecution resumes reporting "false".</TD></TR><TR><TD>
last_error = ""</TD><TD>The last error message iskept in this variable.</TD></TR><TR><TD>

<CAPTION>Accessible instance variables.</CAPTION>
</TD></TR></TABLE></CENTER>
<P>
<CENTER><TABLE BORDER><TR><TD>
<BR>
file</TD><TD>Hash of strings. A translation table whichtranslates variable names into filenames.</TD></TR><TR><TD>
root</TD><TD>Array of pathnames. A list of base directories fromwhich template files are being loaded.</TD></TR><TR><TD>
varkeys</TD><TD>Hash of strings. A translation table whichtranslates variable names into regular expressions forthemselves.</TD></TR><TR><TD>
varvals</TD><TD>Hash of strings. A translation table whichtranslates variable names into replacement values for theirrespective varkeys.</TD></TR><TR><TD>

<CAPTION>Internal instance variables.</CAPTION>
</TD></TR></TABLE></CENTER>
<P>
<H3>Instance methods</H3>

<P>
<P>
<H3>Accessible instance methods</H3>

<P>
<P>
<DL>
<DT><B>Template($root = ".", $unknowns = "remove")</B><DD><P>Constructor. May be called with two optional parameters. The
first parameter sets the template directories (see
<CODE>set_root()</CODE>, the second parameter sets the policy
regarding handling of unknown variables.
<P>
<DT><B>set_root($root)</B><DD><P>The function checks that $root is an array of valid directory
and sets these directories as the base directories where
templates are being loaded from.
<P>
<DT><B>set_unknowns($unknowns = "remove")</B><DD><P>The function sets the policy for dealing with unresolved
variable names. Must be either "remove", "comment" or "keep". If
set to "keep", those are left untouched. If set to "comment",
unresolved variable names are transformed into HTML comments
reporting the error. If set to "remove", unresolved variable
names are silently removed (the default).
<P>
<DT><B>set_file($varname, $filename = "")</B><DD><P>The function defines a filename for the initial value of a
variable. It may be called with either a $varname/$filename pair
or with a hash of $varname/$filename pairs. The files are not
loaded until they are needed.
<P>
<DT><B>set_block($parent, $varname, $name = "")</B><DD><P>A variable $parent may contain a variable block named by
$varname. The function removes that block from $parent and
replaces it with a variable reference named $name. If $name is
omitted, it is assumed to be the same as $varname.
<P>
<DT><B>set_var($varname, $value = "")</B><DD><P>The functions sets the inital value of a variable. It may be
called with either a $varname/$value pair or with a hash of
$varname/$value pairs.
<P>
<DT><B>subst($varname)</B><DD><P>The function returns the value of the variable named $varname,
with all defined variable values filled in. The resulting string
is not "finished", that is, the unresolved variable name policy
has not been applied yet.
<P>
<DT><B>psubst($varname)</B><DD><P>This is a shorthand for <CODE>print $this->subst($varname)</CODE>.
<P>
<DT><B>parse($target, $varname, $append = false)</B><DD><P>The function substitutes the values of all defined variables in
the variable named $varname and stores or appends the result in
the variable named $target.
<P>If $varname is an array of variable names, $append is ignored.
The variables named by $varname are being sequentially
substituted and the result of each substitution step is stored
in $target. The resulting substitution is available in the
variable named by $target, as is each intermediate step for the
next $varname in sequence.
<P>
<DT><B>pparse($target, $varname, $append = false)</B><DD><P>A shorthand for <CODE>print $this->parse(...)</CODE>.
<P>
<DT><B>get_vars()</B><DD><P>Returns a hash of all defined values, keyed by their names.
<P>
<DT><B>get_var($varname)</B><DD><P>Returns the value of the variable named by $varname. If $varname
references a file and that file has not been loaded, yet, the
variable will be reported as empty.
<P>When called with an array of variable names, an hash of values,
keyed by their names, will be returned.
<P>
<DT><B>get_undefined($varname)</B><DD><P>The function will return a hash of unresolved variable names in
$varname, keyed by their names (that is, the hash has the form
$a[$name] = $name).
<P>
<DT><B>finish($str)</B><DD><P>The function will returned the finished version of $str, that
is, the policy regarding unresolved variable names will be
applied to $str.
<P>
<DT><B>p($varname)</B><DD><P>The function will print the finished version of the value of the
variable named by $varname.
<P>
<DT><B>get($varname)</B><DD><P>The function will return the finished version of the value of
the variable named by $varname.
<P>
<DT><B>haltmsg($msg)</B><DD><P>This function can be overridden by your subclass of Template. It
will be called with an error message to print.
</DL>
<P>
<H3>Internal instance methods</H3>

<P>
<DL>
<DT><B>filename($filename)</B><DD><P>When called with a relative pathname, this function will return
the pathname with the appropriate directory from $this->root
prepended. Absolute pathnames are taken unchanged.
<P>The resulting filename must exist in any base directory, or an
error is generated.
<P>
<DT><B>varname($varname)</B><DD><P>The function will construct a variable name regexp for a given
variable name.
<P>
<DT><B>loadfile($varname)</B><DD><P>If a variable is undefined or empty and is backed by a filename,
the backing file will be loaded and the files contents will be
assigned as the variables value.
<P>
<DT><B>halt($msg)</B><DD><P>This function is called whenever an error occurs and will handle
the error according to the policy defined in
$this->halt_on_error.
</DL>
<P>
<H3>Example</H3>

<P>The class manages a set of variables which are text strings.
These strings may contain references to other variables in the
form of "{variable}". When parsed or substituted, a variable
reference is being replaced by the value of that variable. For
example, if you
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
&lt;?php
  $t = new Template;

  $t-&gt;set_var("a", "defined as hugo");
  $t-&gt;set_var("b", "the value of a is {a}");

  print $t-&gt;subst("b")
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>will print <CODE>the value of a is defined as hugo</CODE>.
<P>A variable value may be defined manually by calling
<CODE>set_var("name", "value");</CODE> or it may be defined from
a file by calling <CODE>set_file("name",
"filename.ihtml");</CODE>. In the latter case, the contents of the
file are being loaded when needed (as late as possible) and set
as the value of that variable.
<P>A third way to define a variable value is to call
<CODE>set_block("parent", "block", "name");</CODE>. In this case,
the variable named <CODE>parent</CODE> is being searched for a block
that starts with <CODE>&lt;!-- BEGIN block --&gt;</CODE> and ends
with <CODE>&lt;!-- END block --&gt;</CODE>. This string is removed
from the variable <CODE>parent</CODE> and assigned to the variable named
<CODE>block</CODE>. In <CODE>parent</CODE>, a variable reference to <CODE>name</CODE> is
placed instead. If the optional parameter <CODE>"name"</CODE> is left
out, <CODE>"block"</CODE> is being used instead.
<P>For example, if you
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
&lt;?php
  $t = new Template;

  $t-&gt;set_var("a", "front matter

  &lt;!-- BEGIN b --&gt;
  this is block b
  &lt;!-- END b --&gt;

  end matter");
  $t-&gt;set_block("a", "b", "bb");
?&gt;
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>this will define the variable <CODE>a</CODE> as <CODE>front matter {bb}
end matter</CODE> and the variable <CODE>b</CODE> as <CODE>this is block b</CODE>. All
of this will become more clear when you set the instance
variable <CODE>debug</CODE> to 7 in the following example and trace the
variable accesses.
<P>Use <CODE>Template</CODE> direcly or define a subclass of <CODE>Template</CODE>
as needed.
<P>Define a template file named page.ihtml as follows:
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
&lt;html&gt;
 &lt;head&gt;&lt;title&gt;{PAGETITLE}&lt;/title&gt;&lt;/head&gt;
 &lt;body bgcolor="#ffffff"&gt;
 &lt;table border=1 cellpadding=4 cellspacing=0 bgcolor="#eeeeee"&gt;
  &lt;tr&gt;
   &lt;td colspan=2&gt;&lt;h1&gt;{PAGETITLE}&lt;/h1&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;{OUT}&lt;/td&gt;
   &lt;td&gt;Content&lt;br&gt;{UNDEFINED_VARIABLE}&lt;/td&gt;
  &lt;/tr&gt;
 &lt;/table&gt;
 &lt;/body&gt;
&lt;/html&gt;
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>This file contains a reference to the variable <CODE>PAGETITLE</CODE>
and a reference to the variable named <CODE>OUT</CODE>. Another
reference to the variable named <CODE>UNDEFINED_VARIABLE</CODE> is never
resolved. Another template file, named box.ihtml, contains a
block named <CODE>row</CODE> with three variable references {TITLE},
{NUM} and {BIGNUM}:
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
&lt;!-- start box.ihtml --&gt;
&lt;table border=1 bgcolor="#cccccc" cellpadding=4 cellspacing=0&gt;
 &lt;tr&gt;
  &lt;td colspan=2&gt;&lt;b&gt;{TITLE}&lt;/b&gt;&lt;/td&gt;
 &lt;/tr&gt;
  &lt;!-- BEGIN row --&gt;
  &lt;tr&gt;
   &lt;td&gt;{NUM}&lt;/td&gt;
   &lt;td&gt;{BIGNUM}
  &lt;/tr&gt;
  &lt;!-- END row --&gt;
&lt;/table&gt;
&lt;!-- end box.ihtml --&gt;
</PRE>
<HR>
</CODE></BLOCKQUOTE>
<P>The following php3 file demonstrates how to use these templates:
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
&lt;?php
  /* include the Template class */
  include("template.inc");

  /* create an instance of Template with desired parameters */
  $t = new Template("/home/kris/www/test.koehntopp.de/pages/template", "keep");
  /* $t-&gt;debug = 7; */ /* activate for full debugging */

  /* define two variables from files */
  $t-&gt;set_file(array(
     "page" =&gt; "page.ihtml",
     "box"  =&gt; "box.ihtml"));

  /* define a variable contained in another */
  $t-&gt;set_block("box", "row", "rows");

  /* define two variables manually */
  $t-&gt;set_var(array("TITLE"     =&gt; "Testseite",
                    "PAGETITLE" =&gt; "hugo"));

  for ($i=1; $i&lt;=3; $i++) {
    $n  = $i;
    $nn = $i*10;

    /* define values for NUM and BIGNUM */
    $t-&gt;set_var(array("NUM" =&gt; $n, "BIGNUM" =&gt; $nn));

    /* replace NUM and BIGNUM in row and
     * append the result to rows */
    $t-&gt;parse("rows", "row", true);
  }

  /* replace all variables in box, result in OUT,
   * then replace all variables in page, result in OUT */
  $t-&gt;parse("OUT", array("box", "page"));

  /* print OUT */
  $t-&gt;p("OUT");
?&gt;
&lt;hr&gt;
Undefined variables in OUT:
&lt;?php
  print @implode(", ", $t-&gt;get_undefined("OUT"));
 ?&gt;
</PRE>
<HR>
</CODE></BLOCKQUOTE>

<P>
<P>
<HR>
<A HREF="documentation-5.html">Next</A>
<A HREF="documentation-3.html">Previous</A>
<A HREF="documentation.html#toc4">Contents</A>
</BODY>
</HTML>