NinjaCipher:-*-:ro60

Archive for November, 2007

smarty ternary modifier plug-in

Saturday, November 24th, 2007

Here is a modifier plug-in that I wrote for smarty that does a simple ternary operation. I would have thought that they would have this built in but from my searches it doesn’t seem to be the case.

function smarty_modifier_ternary($value,$option1,$option2)
{
    return ($value)?$option1:$option2;
}

You can use it like so…

{$post.is_draft|ternary:'draft':'published'}

If you don’t know how to use smarty plug-ins then you can read up here.

View helpers with Zend Framework

Wednesday, November 14th, 2007

Just wanted to quickly share some zend view helpers that I wrote that someone out there may find useful.

1: NF_View_Helper_EllipsisString

This helper takes a string an returns a shortened version with … tacked on. Pretty standard fair but helpful. I wrote it before I switched to Smarty for my template engine which has a similar function built in.

class NF_View_Helper_EllipsisString
{
    //defaults to 50 chars and ...
    public function ellipsisString($string, $max = 50, $rep = '...') {
	    $leave = $max - strlen ($rep);
	    return substr_replace($string, $rep, $leave);
	}
}

2: NF_View_Helper_FormatDate

This helper takes a date and gives you back a formatted version. It allowes you to pass in a format specifier string or choose from one of the predefined formats.

require_once 'Zend/Date.php';

class NF_View_Helper_FormatDate
{
    private $date_formats = array("mdy"=>"MMMM/dd/yy");

    public function formatDate($date, $formatName, $formatStr = '') {
	    if (!Zend_Date::isDate($date)) {
		    return $date;
		}else{
			$date = new Zend_Date($date);
			if(($formatName != null)&&(in_array($this->date_formats,$formatName))){
				return $date->toString($this->date_formats[$formatName]);
			}else if($formatStr != null){
				return $date->toString($formatStr);
			}
		}
	}
}

Feel free to use and no you can’t blame me if it blows up and kills your family dog… What do you want for nothing?

Oh whats that you say? You have never used a view helper? erm well ok then… You can get the info from their docs but I’ll break it down for you quickly as well.

If you have never used a custom view helper its really easy (as long as you follow the conventions of course). Just put them in the application/views/helpers dir (if you followed the standard MVC layout from the docs). The php files that hold the classes should be named after the function in CamelCase. So for instance the NF_View_Helper_EllipsisString plugin lives in a file called EllipsisString and contains a class called NF_View_Helper_EllipsisString with a function called ellipsisString. Even though the method name starts with a lower case e it needs to have a CamelCase in the file name and the class name. NF_View_Helper_ is a namespace which helps with organization.

Once you have that together inside of your controller init you call

$this->view->setHelperPath('../views/helpers', 'NF_View_Helper');

Now your all set to use these in calls in your views like so

$this->ellipsisString("my really really really long string or whatever",10,"...");