dat/shit.php -
index
<?php
/* Simple HTML In Templates - simple web template engine that thinks it can */
define('SHIT_EOL',"\n"); // make '' to not add EOLs
/* shit_html - pass the HTML and form it to be assigned and displayed later */
// usage:
// $foo = "foobar"
// eval(shit_html('<h1>$foo</h1>'));
// print $HTML; // print "<h1>foobar</h1>"
function shit_html($html, $name = 'HTML') {
if ($name === '')
$name = 'HTML=""; $HTML';
$html = addslashes($html);
$html = str_replace('\\\'','\'',$html); // *
return "\$$name .= \"$html\";";
}
// * a faster was is to do just this:
// $html = addcslashes($html,'"');
// and that would allow PHP's \n\v\r conversions
// but then again, this is all rather stupid stuff
/* shit_show - pass the HTML and form it to be displayed with "translations" */
// usage:
// $HTML = "variable text";
// $html = '<h1>$HTML</h1>';
// eval(shit_show($html)); // print "<h1>variable text</h1>";
function shit_show($html) {
$html = _convertchars($html);
$html = addslashes($html);
$html = str_replace('\\\'','\'',$html);
return "print \"$html\";";
}
/* shit_show_no - like shit_show but no translations */
function shit_show_no($html) {
$html = addslashes($html);
$html = str_replace('\\\'','\'',$html);
return "print \"$html\";";
}
/* shit_file - read file, create HTML to be displayed */
// usage:
// $HTML = "variable text";
// $file = 'template.html'; // file = '<h1>$HTML</h1>';
// eval(shit_file($template)); // print "<h1>variable text</h1>";
function shit_file($file) {
if (!is_file($file))
return "print \"shit_file: '$file' not found\";";
$html = file_get_contents($file);
return shit_show($html);
}
/* shit_html_file - read file, create HTML to be assigned */
// usage:
// $foo = "foobar";
// $file = 'template.html'; // file = '<h1>$foo</h1>';
// eval(shit_file($template)); // $HTML = "<h1>foobar</h1>";
// print $HTML;
function shit_html_file($file) {
if (!is_file($file))
return shit_html("shit_html_file: '$file' not found",'');
$html = file_get_contents($file);
return shit_html($html,'');
}
/* markup creation functions */
/* shit_elements - do all kinds of crazy shit */
/*
// usage:
$a = array("abc","xyz");
echo shit_elements($a,'ID $1, VAL $2<br>');
$a = array('A'=>"abc",'B'=>"xyz");
echo shit_elements($a,'ID $1, VAL $2<br>');
$b[] = array('A'=>"abc",'B'=>"def");
$b[] = array('A'=>"uvw",'B'=>"xyz");
echo shit_elements($b,'ID $1, VAL $2<br>');
$c[] = array('name'=>"Jones",'action'=>"Exploring");
$c[] = array('name'=>"Ralph",'action'=>"Wrecking");
echo shit_elements($c,'<b>$name</b> goes $action<br>',0);
$d['a'] = array('name'=>"Jones",'action'=>"Exploring");
$d['b'] = array('name'=>"Ralph",'action'=>"Wrecking");
echo shit_elements($d,'$key <b>$name</b> goes $action<br>',0);
echo shit_elements($d,'$selected <b>$1</b> goes $2<br>',1,'b');
echo shit_elements($d,'<b>$name</b> goes $action',0,'b','!');echo "<br>";
exit;
*/
function shit_elements($data, $html, $numbers = 1, $sel = '', $after = '') {
$htm = '';
if (isset($data[0]) && !is_array($data[0])) { // regular array
$i = 1;
foreach ($data as $dat)
$html = str_replace('$'.$i++,$dat,$html);
return $html;
}
if (!is_array(current($data))) { // single array
foreach ($data as $key => $value) {
$str = str_replace('$1',$key,$html); // note '$html'
$str = str_replace('$2',$value,$str); // note '$str'
$htm .= $str.SHIT_EOL;
}
return $htm;
}
$i = 0;
$n = '';
$c = count($data);
foreach ($data as $key => $value) {
$names = array_keys($value);
$num = 1;
if (++$i == $c) $after = '';
$str = str_replace('$key',$key,$html);
$selected = ($key == $sel) ? 'selected' : '';
$str = str_replace('$selected',$selected,$str);
foreach ($names as $n) {
$id = '$'.$num++;
if (!$numbers) $id = '$'.$n;
$str = str_replace($id,$value[$n],$str);
// no EOL
}
$htm .= $str.$after;
}
return $htm;
}
// don't need any more complex markup processing... so there
/* private functions */
/* convert "special characters/strings" */
function _convertchars($str) {
$from = array('$$','\$'); // more stuff later perhaps
$to = array('$','$');
return str_replace($from,$to,$str);
}
?>