The micro-framework that underlies Fcp.Website. Also part of my Code Grimoire.
See below for usage examples. This code may not be current.
<?php
/*
* fcp.website.php A mini-framework for websites.
* Felix Pleșoianu <felixp7@yahoo.com>
*
* If you are asking what license this software is released under,
* you are asking the wrong question.
*/
// Main entry point.
function website($settings) {
if (isset($settings['tpl-vars']) && is_array($settings['tpl-vars'])) {
$T = $settings['tpl-vars'];
} else {
$T = array();
}
$location = parse_location($settings['default_view']);
$T = array_merge($T, $location['view']($location['data'], $T));
if (isset($settings['boxes']) && is_array($settings['boxes'])) {
foreach ($settings['boxes'] as $box_name) {
$box_func = $box_name . '_box';
if (function_exists($box_func)) {
$T[$box_name] =
$box_func($location, $T);
} else {
$T[$box_name] = "(missing content: $box_name)";
}
}
}
print render($settings['template'], $T);
}
function parse_location($default_view) {
$url_split = explode('/', $_SERVER['REQUEST_URI']);
$location = end($url_split);
if ($location == basename($_SERVER['SCRIPT_NAME'])) {
$location_data = array($default_view);
} else if (empty($location)) {
$location_data = array($default_view);
} else {
$location_data =
array_map('urldecode', explode(':', $location));
}
$view_function = $location_data[0] . '_view';
if (function_exists($view_function)) {
return array(
'view' => $view_function,
'data' => array_slice($location_data, 1));
} else {
return array('view' => 'error_page', 'data' => array());
}
}
function to_template_var($str) {
return "%%$str%%";
}
function render($template, $tpl_vars) {
$replaced = str_replace(
array_map('to_template_var', array_keys($tpl_vars)),
array_values($tpl_vars),
$template);
// Clean up leftover template vars.
return preg_replace('/%%[^%]*%%/', '', $replaced);
}
function error_page($tpl_vars) {
if (isset($tpl_vars['error']))
$content = $tpl_vars['error'];
else
$content = 'Page not found';
return array('page_title' => 'Error', 'content' => $content);
}
?>
Basic usage example
<?php
include 'fcp.website.php';
$settings = array(
'template' => '<h1>%%title%%</h1> <p>%%message%%</p>',
'default_view' => 'page');
function page_view($path, $tpl_vars) {
return array('title' => 'Hello', 'message' => 'Hello, world!');
}
website($settings);
?>
Advanced usage example:
<?php
include 'fcp.website.php';
$template = <<<TEMPLATE
<h1>%%page-title%% - %%site-title%%</h1>
%%nav%%
<div>%%content%%</div>
TEMPLATE;
$settings = array(
'tpl-vars' => array('site-title' => 'Hello, World!'),
'template' => $template,
'default_view' => 'page',
'boxes' => array('nav'));
function page_view($path, $tpl_vars) {
if (count($path) == 0) $path[0] = 'Home';
return array(
'page-title' => $path[0],
'content' => 'You are here: ' . $path[0]);
}
function nav_box($location, $tpl_vars) {
$output = "<ul>\n";
foreach (array('Home', 'About', 'Contact') as $page) {
$output .= sprintf('<li><a href="%s/page:%s">%s</a></li>',
$_SERVER['SCRIPT_NAME'], $page, $page);
}
$output .= "</ul>\n";
return $output;
}
website($settings);
?>
Last modified: Tue 04 05 2010, 12:38:47 UTC
