Archive for category Snippets
Change an Ubuntu server’s timezone from the command line
This is pretty straight forward. Log in to the system and type:
dpkg-reconfigure tzdata
Follow along as it prompts you. Bam. Done.
Zend Framework – Conditionally Load Javascript Files
Posted by Stuart in Snippets, Zend Framework, jquery on April 6, 2010
I needed to load javascript files on a per controller basis in a Zend Framework (ZF) based project. In the past I would have done this with some conditional logic that would load things based on a page number or name or some such. In ZF all that is needed is to stick some code in the controller’s init method, a line in the layout header and we are good to go.
Here is the code to stick in your controller:
public function init()
{
$this->view->headScript()->appendFile('/mysite/public/js/somefile.js')
->appendFile('/mysite/public/js/someother.js');
}
Here is the line you stick in the layout.phtml header:
echo $this->headScript();
That’s it! Now you can have controller specific js loaded and keep your sites code overhead under control by keeping uneeded js files out of your layout.phtml.
Generating XML and passing it to jQuery
I am generating some XML and then POSTing it to a jQuery based plugin. Looking at the POSTed XML in Firebug, everything appears normal. However, something is not getting passed to the plugin so it is using a amount (which results in no data being properly displayed). The solution turned out to be quite simple… set the Mime-Type on the posted XML! Doh!
So by adding
header('Content-Type: text/xml');
everything passed smoothly.
Extending Zend_View to Implement a Concrete Function…
Posted by Stuart in Snippets, Zend Framework on April 29, 2009
…without using a view helper*.
In a nutshell:
- Extend Zend_View
- Put your method in this extended class
- Instantiate the class (in your bootstrap for instance)
- Assign it to the ViewRenderer
- Pass that viewrenderer to Zend_Controller_Action_HelperBroker’s addHelper method
- Use it in your view
The details:
In /library/My/Zend/ create View.php:
class My_Zend_View extends Zend_View
{
public function _($string)
{
return Zend_Registry::get('translate')->_($string);
}
}
In your bootstrap:
require_once APPLICATION_PATH.'../library/My/Zend/View.php'; $view = new My_Zend_View(); $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer(); $viewRenderer->setView($view); Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
In your view:
echo $this->_("Hello");
Some extras. I have APPLICATION_PATH defined in index.php as:
define('APPLICATION_PATH', realpath(dirname(__FILE__)).'/application/');
and I have Zend_Translate instantiated in bootstrap.php like:
$translate = new Zend_Translate('csv', APPLICATION_PATH.'../library/Mine/source-en.csv', 'en');
and lastly, don’t forget to register $translate with the registry or that echo in the view file won’t work:
$registry->translate = $translate;
*Why not use a view helper? Well, you should, actually. The only reason for this solution is that the original need was for something that would work with the underscore notation used by Poedit. Apparently implementing a method named “_” doesn’t work in a helper so this solution extends Zend_View… adding an underscore method.
Sooo… if you should use a view helper what does that look like?
In /library/My/Helper/Translate.php
class My_Helper_Translate extends Zend_View_Helper_Abstract
{
public function translate($string)
{
return Zend_Registry::get('translate')->_($string);
}
}
In your bootstrap:
$view->addHelperPath("../library/My/Helper/", "My_Helper");
In the view:
echo $this->translate("Hello");





