Jump to content

User:X!/PHPtemp

From Wikipedia, the free encyclopedia

Documentation for the PHPtemp template.

Syntax

[edit]

At the moment, PHPtemp can do 5 simple tasks.

  • {#foo#} is replaced with the foo line from a PHP configuration file. (see sample .conf file)
  • {$foo$} is replaced with bar when this code is called: $phptemp->assign( "foo", "bar" );
  • {&isset: foo &}bar{&endisset&}</nowiki> will render as bar if foo is set with the $phptemp->assign() function.
  • {&math: 1+1 &} returns 2.

The last function is hard to describe, so I'll do the best that I can. It is designed to replicate the PHP built in foreach() function.

{&foreach: arr={{{$testarr$}}} key={^k^} var={^v^}&}
With the key {^k^}, the value is {^v^}.
{&endforeach&}

When called with $phptemp->assign( "testarr", serialize( array( 'foo' => 'bar', 'fu' => 'baz' ) ) );, here's what the output would be:

With the key foo, the value is bar.
With the key fu, the value is baz.

How to use it

[edit]

Single-instance method

[edit]
  1. Create a file called "en.conf", with the following contents:
[main]
title = "Test PHPtemp page"
  1. Create a file called main.tpl with the following contents:
<html>
<head><title>{#title#}</title><head>
<body>
<!-- isset header = "<h2>{$header$}</h2>" -->
{$content$}
<ul>
{&foreach arr={{{$listarr$}}} key={^number^} val={^v^}&}
<li>Line #{^number^}: {^v^}</li>
{&endforeach&}
</ul>
</body>
</html>
  1. Create your main.php file with the following contents:
<?php

require_once( '/absolute/path/to/phptemp/class/phptemp.class.php' );
require_once( '/absolute/path/to/lang/class/language.class.php' );

$phptemp = new PHPtemp( '/absolute/path/to/template/main.tpl' );
$language = new Language( array( 'en' ) );
$lang = $language->getLang();

$phptemp->load_config( '/absolute/path/to/conf/file/'.$lang.'.conf', 'main' );


$phptemp->assign( "header", "Testing PHPtemp" );
$phptemp->assign( "content", "Here's an unordered list of a certain array:" );
$phptemp->assign( "listarr", serialize( array( 'foo', 'bar' ) ) );

$phptemp->display(); 

?>

Result

[edit]
<html>
<head><title>Test PHPtemp page</title><head>
<body>
<h2>Testing PHPtemp</h2>
Here's an unordered list of a certain array:
<ul>
<li>Line #0: foo</li>
<li>Line #1: bar</li>
</ul>
</body>
</html>

Multi-instance method

[edit]

This method is useful for a web site that has multiple pages. What's better about this method is that if you make a change to the <head> tag, or another section of the common template, you don't have to update every single page.

Hierarchy:

>dir
>>configs
>>templates
>>app
>>>configs
>>>templates
  1. Create a file called "en.conf" in the dir/configs directory, with the following contents:
[main]
title = "Test PHPtemp page"
  1. Create a file called "main.tpl" in the dir/templates directory, with the following contents:
<html>
<head><title>{#title#}</title><head>
<body>
{$content$}
</body>
</html>
  1. Create a file called "en.conf" in the dir/app/configs directory, with the following contents:
[app]
header = "Testing PHPtemp"
  1. Create a file called "app.tpl" in the dir/app/templates directory, with the following contents:
<h2>{#header#}</h2>
{$content$}
<ul>
{&foreach arr={{{$listarr$}}} key={^number^} val={^v^}&}
<li>Line #{^number^}: {^v^}</li>
{&endforeach&}
</ul>
  1. Create your dir/app/app.php file with the following contents:
<?php

require_once( '/absolute/path/to/phptemp/class/phptemp.class.php' );
require_once( '/absolute/path/to/lang/class/language.class.php' );

$phptemp = new PHPtemp( '/absolute/path/to/dir/templates/main.tpl' );
$content = new PHPtemp( '/absolute/path/to/dir/app/templates/app.tpl' );

$language = new Language( array( 'en' ) );
$lang = $language->getLang();

$phptemp->load_config( '/absolute/path/to/dir/configs/'.$lang.'.conf', 'main' );
$content->load_config( '/absolute/path/to/dir/app/configs/'.$lang.'.conf', 'app' );

$content->assign( "content", "Here's an unordered list of a certain array:" );
$content->assign( "listarr", serialize( array( 'foo', 'bar' ) ) );

$phptemp->assign( "content", $content->display( true ) );
$phptemp->display(); 

?>

Result

[edit]
<html>
<head><title>Test PHPtemp page</title><head>
<body>
<h2>Testing PHPtemp</h2>
Here's an unordered list of a certain array:
<ul>
<li>Line #0: foo</li>
<li>Line #1: bar</li>
</ul>
</body>
</html>