Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added BTS2 as trunk. tests need completion.
git-svn-id: file:///Users/bill/svn/mboxsvn/BTS@10 fdeb315d-9e43-0410-bf1e-99c702123453
- Loading branch information
shupp
committed
Mar 14, 2008
1 parent
2a984ec
commit feb5fe7
Showing
22 changed files
with
627 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,253 @@ | ||
<?php | ||
/** | ||
* BTS2 - Bill's Template System for PHP 5 | ||
* | ||
* A very simple Smarty-like template sytem | ||
* | ||
* PHP Version 5 | ||
* | ||
* @category HTML | ||
* @package BTS2 | ||
* @author Bill Shupp <hostmaster@shupp.org> | ||
* @copyright 2006-2008 Bill Shupp | ||
* @license GPL 2.0 {@link http://www.gnu.org/licenses/gpl.txt} | ||
* @link http://shupp.org/bts | ||
*/ | ||
|
||
require_once 'BTS2/Exception.php'; | ||
|
||
/** | ||
* BTS2 | ||
* | ||
* @category HTML | ||
* @package BTS2 | ||
* @author Bill Shupp <hostmaster@shupp.org> | ||
* @copyright 2006-2008 Bill Shupp | ||
* @license GPL 2.0 {@link http://www.gnu.org/licenses/gpl.txt} | ||
* @link http://shupp.org/bts | ||
*/ | ||
class BTS2 | ||
{ | ||
/** | ||
* data | ||
* | ||
* Where all data is stored | ||
* | ||
* @var array | ||
* @access public | ||
*/ | ||
protected $data = array(); | ||
|
||
/** | ||
* templateDir | ||
* | ||
* Location of template directory | ||
* | ||
* @var string | ||
* @access protected | ||
*/ | ||
protected $templateDir = null; | ||
|
||
/** | ||
* __constructor | ||
* | ||
* Setup | ||
* | ||
* @param string $templateDir Teamplates directory | ||
* | ||
* @access public | ||
* @return void | ||
*/ | ||
public function __construct($templateDir = './templates') | ||
{ | ||
$this->templateDir = $templateDir; | ||
$this->data['php_self'] = $_SERVER['PHP_SELF']; | ||
} | ||
|
||
/** | ||
* __set | ||
* | ||
* @param mixed $key data array key | ||
* @param mixed $val data array value | ||
* | ||
* @access public | ||
* @return void | ||
*/ | ||
public function __set($key, $val) | ||
{ | ||
$this->data[$key] = $val; | ||
} | ||
|
||
/** | ||
* __get | ||
* | ||
* @param mixed $key data array key | ||
* | ||
* @access public | ||
* @return void | ||
*/ | ||
public function __get($key) | ||
{ | ||
if (isset($this->data[$key])) { | ||
return $this->data[$key]; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* assign | ||
* | ||
* Assign a variable to $data | ||
* | ||
* @param mixed $name variable name | ||
* @param mixed $value variable value | ||
* | ||
* @access public | ||
* @return void | ||
*/ | ||
public function assign($name, $value) | ||
{ | ||
$this->data[$name] = $value; | ||
} | ||
|
||
/** | ||
* getContents | ||
* | ||
* return the parsed PHP contents of a file | ||
* | ||
* @param string $file filename | ||
* | ||
* @access protected | ||
* @throws BTS2_Exception if $file is not readable | ||
* @return string | ||
*/ | ||
protected function getContents($file) | ||
{ | ||
$path = $this->templateDir . DIRECTORY_SEPARATOR . $file; | ||
if (!is_readable($path)) { | ||
throw new BTS2_Exception("BTS2 Error: Could not retrieve $path"); | ||
} else { | ||
// Get contents and parse PHP | ||
foreach ($this->data as $key => $val) { | ||
$$key = $val; | ||
} | ||
ob_start(); | ||
include $path; | ||
$buffer = ob_get_contents(); | ||
ob_end_clean(); | ||
return $buffer; | ||
} | ||
} | ||
|
||
/** | ||
* parse | ||
* | ||
* Parse template data | ||
* | ||
* @param mixed $data contents to parse | ||
* | ||
* @protected | ||
* @return $data | ||
*/ | ||
protected function parse($data) | ||
{ | ||
// Replace Tags | ||
foreach ($this->data as $key => $value) { | ||
if (is_array($value)) { | ||
$re = '/{[$]*' . trim($key) . '}/i'; | ||
$data = preg_replace($re, 'Array', $data); | ||
foreach ($value as $ar_key => $ar_val) { | ||
$re = '/{[$]*' . trim($key) . '.' . trim($ar_key) . '}/i'; | ||
$data = preg_replace($re, trim($ar_val), $data); | ||
} | ||
} else { | ||
$re = '/{[$]*' . trim($key) .'}/i'; | ||
$data = preg_replace($re, trim($value), $data); | ||
} | ||
} | ||
return $data; | ||
} | ||
|
||
/** | ||
* display | ||
* | ||
* Output data using print() if second argument is 0. | ||
* Otherwise, return the data to the caller | ||
* | ||
* @param mixed $file filename | ||
* @param bool $return whether to return rather than display | ||
* | ||
* @access public | ||
* @throws BTS2_Exception bubbles up from $this->getContents() | ||
* @return mixed string $data or void | ||
*/ | ||
public function display($file, $return = false) | ||
{ | ||
$data = $this->getContents($file); | ||
$data = $this->parse($data); | ||
if ($return == true) { | ||
return $data; | ||
} | ||
print ($data); | ||
} | ||
|
||
/** | ||
* selectOptions | ||
* | ||
* Determinte which option is selected in an array, output | ||
* as an <option> list | ||
* | ||
* @param mixed $opts options array | ||
* @param mixed $selectedOpt selected option | ||
* | ||
* @access public | ||
* @return void | ||
*/ | ||
public function selectOptions($opts, $selectedOpt) | ||
{ | ||
$out = ''; | ||
foreach ($opts as $key => $val) { | ||
$selected = ''; | ||
if ($val == $selectedOpt) { | ||
$selected = ' selected'; | ||
} | ||
$out .= "<option$selected>$val\n"; | ||
} | ||
return $out; | ||
} | ||
|
||
/** | ||
* cycle | ||
* | ||
* Simple function for cycling items like bgcolor in a foreach loop | ||
* Derived from the Smarty Plugin by Monte Ohrt | ||
* | ||
* @param mixed $values bgcolor values | ||
* @param string $name name | ||
* | ||
* @access public | ||
* @return void | ||
*/ | ||
public function cycle($values, $name = 'default') | ||
{ | ||
static $cycleVars; | ||
if (isset($cycleVars[$name]['values']) | ||
&& $cycleVars[$name]['values'] != $values) { | ||
$cycleVars[$name]['index'] = 0; | ||
} | ||
$cycleVars[$name]['values'] = $values; | ||
|
||
$cycleArray = explode(',', $cycleVars[$name]['values']); | ||
if (!isset($cycleVars[$name]['index'])) { | ||
$cycleVars[$name]['index'] = 0; | ||
} | ||
$retval = $cycleArray[$cycleVars[$name]['index']]; | ||
if ($cycleVars[$name]['index'] >= count($cycleArray) -1) { | ||
$cycleVars[$name]['index'] = 0; | ||
} else { | ||
$cycleVars[$name]['index']++; | ||
} | ||
return $retval; | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* BTS2_Exception | ||
* | ||
* Exception class for BTS2 | ||
* | ||
* PHP Version 5 | ||
* | ||
* @category HTML | ||
* @package BTS2 | ||
* @uses Exception | ||
* @author Bill Shupp <hostmaster@shupp.org> | ||
* @copyright 2008 Bill Shupp | ||
* @license GPL 2.0 {@link http://www.gnu.org/licenses/gpl.txt} | ||
* @link http://shupp.org/bts | ||
*/ | ||
|
||
|
||
/** | ||
* BTS2_Exception | ||
* | ||
* Exception class for BTS2 | ||
* | ||
* @category HTML | ||
* @package BTS2 | ||
* @uses Exception | ||
* @author Bill Shupp <hostmaster@shupp.org> | ||
* @copyright 2008 Bill Shupp | ||
* @license GPL 2.0 {@link http://www.gnu.org/licenses/gpl.txt} | ||
* @link http://shupp.org/bts | ||
*/ | ||
class BTS2_Exception extends Exception | ||
{ | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
require_once 'BTS2.php'; | ||
$tpl = new BTS2('../examples/templates');; | ||
|
||
$tpl->assign('title', 'Welcome to my web site'); | ||
$tpl->display('tpl1.tpl'); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
$name_array = array('first' => 'bill', 'last' => 'shupp'); | ||
|
||
require_once 'BTS2.php'; | ||
$tpl = new BTS2; | ||
|
||
$tpl->assign('title', 'Welcome to my web site'); | ||
$tpl->assign('name', $name_array); | ||
$tpl->display('tpl2.tpl'); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
$names_array[0] = array('first' => 'John', 'last' => 'Parker'); | ||
$names_array[1] = array('first' => 'Matt', 'last' => 'Timmons'); | ||
$names_array[2] = array('first' => 'Joe', 'last' => 'Shmoe'); | ||
$names_array[3] = array('first' => 'Fred', 'last' => 'Hamilton'); | ||
|
||
require_once 'BTS2.php'; | ||
$tpl = new BTS2; | ||
|
||
$tpl->assign('title', "Here's a List of names, with cycling tr bgcolors:"); | ||
$tpl->assign('names', $names_array); | ||
$out = $tpl->display('tpl3.tpl', 1); | ||
echo $out; | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
$names_array[0] = 'bill'; | ||
$names_array[1] = 'david'; | ||
$names_array[2] = 'bob'; | ||
$names_array[3] = 'steve'; | ||
$names_array[4] = 'mike'; | ||
|
||
require_once 'BTS2.php'; | ||
$tpl = new BTS2; | ||
|
||
$tpl->assign('title', "Here's a List of names, with cycling tr bgcolors:"); | ||
$tpl->assign('names', $names_array); | ||
$tpl->assign('name_selected', 'steve'); | ||
$out = $tpl->display('tpl4.tpl', 1); | ||
echo $out; | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
{title} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<html> | ||
<body> | ||
{title} | ||
|
||
My name is {name.first} {name.last} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<html> | ||
<body> | ||
{title} | ||
|
||
<table> | ||
<tr> | ||
<td>First Name</td> | ||
<td>Last Name</td> | ||
</tr> | ||
<? foreach($names as $n): ?> | ||
<tr bgcolor="<?= $this->cycle("#cccccc,#fefefe,#666666")?>"> | ||
<td><?= htmlentities($n['first'])?></td> | ||
<td><?= $n['last']?></td> | ||
</tr> | ||
<? endforeach?> | ||
</table> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<html> | ||
<body> | ||
{title} | ||
|
||
<table> | ||
<tr> | ||
<td bgcolor="<?= $this->cycle("#eeeeee,#dddddd") ?>">Select Name</td> | ||
<td><select name=names><?= $this->selectOptions($names, $name_selected)?></select></td> | ||
<td bgcolor="<?= $this->cycle("#eeeeee,#dddddd") ?>">Select Name</td> | ||
<td><select name=names><?= $this->selectOptions($names, $name_selected)?></select></td> | ||
</tr> | ||
</table> | ||
</body> | ||
</html> |
Oops, something went wrong.