Back again with more TinyMVC code. As I get more into this framework the more code I’ll be posting up, the functions in the code below are to be used directly from TinyMVC views and not controllers or models.
One of the most useful features below is block($view) allowing you to directly output other views, inside your view (like CakePHP does) although this is technically not following “strict” MVC guidelines it is still a very useful feature.
tinymvc_script_helpers.php
<?php
function esc($string) {
return htmlentities($string);
}
function anchor($url,$text) {
if (isset($url, $text)) {
return "<a href=\"$url\">$text</a>";
}
}
function css($css) {
if (isset($css)) {
if (is_array($css)) {
foreach ($css as $file) {
echo '<link rel="stylesheet" href="'.$file.'" media="screen" />'."\n";
}
} else {
echo '<link rel="stylesheet" href="'.$css.'" media="screen" />'."\n";
}
}
}
function js($js) {
if (isset($js)) {
if (is_array($js)) {
foreach ($js as $file) {
echo '<script type="text/javascript" src="'.$file.'"></script>'."\n";
}
} else {
echo '<script type="text/javascript" src="'.$js.'"></script>'."\n";
}
}
}
function block($bTemplate) {
if (isset($bTemplate)) {
$tmvc = tmvc::instance();
$template = $tmvc->controller->view->fetch($bTemplate);
if ($template) {
echo $template;
unset($template);
}
}
}
function arrp() {
$arrs = func_get_args();
if (isset($arrs) && is_array($arrs)) {
echo '<pre style="padding: 5px; font-size: 11px; line-height: 16px;">';
$o='';
foreach ($arrs as $key=>$val) {
ob_start();
var_dump($val);
$o.=ob_get_contents();
ob_end_clean();
}
echo htmlspecialchars($o, ENT_QUOTES);
echo '</pre>';
}
}
function relativeTime($utimestamp){
if (isset($utimestamp)) {
$types = Array('second', 'minute', 'hour', 'day', 'week', 'month', 'years', 'decade');
$duration = Array(60, 60, 24, 7, 4.35, 12, 10);
$gap = (time() - $utimestamp);
if ($gap > 0) {
$end = "ago";
} else {
$gap =- $gap;
$end = "to go";
}
for($i = 0; $gap >= $duration[$i]; $i++) {
$gap /= $duration[$i];
$gap = round($gap);
}
if($gap != 1) {
$types[$i].= "s";
$reltime = $gap.' '.$types[$i].' '. $end;
}
return $reltime;
}
}
?>
Copy the above code into a file and name it tinymvc_script_helpers.php. Place this file in your TinyMVC applications plugin directory. You will need to register loading this file by using the following in any controller (or are using auto-loading ignore this)
$this->load->script('helpers');
You should now have access to all these functions directly from all of your views. Below is a table of the functions and their descriptions.
The Helpers
| Function | Description |
| block($view) | Used to directly output other views. Useful for including sections inside your main view. Takes one parameter, the view name. |
| css($css) | Outputs HTML to include stylesheets, takes one string parameter or an array for multiple stylesheets. |
| js($js) | Outputs HTML to include javascript files, takes one string parameter or an array for multiple files. |
| esc($str) | Performs a htmlentities on the given string. |
| anchor($url, $text) | Outputs HTML to form an anchor from a URL and text. |
| arrp($arr) | Outputs HTML to format a given array. A nicer version of print_r, takes unlimited array arguments. |
| relativeTime($timestamp) | Translates a UNIX timestamp into relative time e.g. 3 Minutes ago or 1 Day ago. |
The helpers will be expanded upon and updated at some point, I am planning to integrate optional minifying and combining for the javascript and stylesheets sometime soon.
There are also plans to develop some sort of ‘Form builder’ and ‘Array to HTML Table’ generators etc. but this will come at a later time.
Enjoy.

On TinyMVC, the helpers are accessible in view layer? If yes, how access them (simply call functionName(parameters)?
Regards
Please disregard the previous comment. I had not watched the first paragraph. Please delete both comments.