Archive for category Snippets
Access GET Params via Javascript
function $_GET(q,s) {
s = (s) ? s : window.location.search;
var re = new RegExp('&'+q+'=([^&]*)','i');
return (s=s.replace(/^\?/,'&').match(re)) ? s=s[1] : s='';
}
// Grab the GET param
var someparam = $_GET('someparam');
IE6 – Modal Window stuck behind background
So with some modal windows (like with jQuery Modal or dialog) they can appear “greyed out” and unclickable. This is because the modal window gets placed behind the overlay. The reason for this is related to the containers that the modal is in. To get around it, move the modal outside of all containers except the body. You can do it physically or, if you don’t have access to all of the code, via javascript.
Here is a jQuery based js snippet to do just this with a jqmodal window:
$('body').append($('#someModal'));
This will grab the item with the id of someModal and append it to the body, moving it outside the flow of other containers on the page.
Redirect Subversion to Standard Output for Troubleshooting
$cmd = '/usr/bin/svn list https://someserver.com:8443/svn/proj/trunk/ --username "admin" --password "fobdg$f_Aa" 2>&1';
exec($cmd, $output);
$output = implode("\n", $output) . "\n";
echo $output;
?>
Open a file in the Browser, from within Vim
An example of opening a file you are working on in Vim right into your default browser:
:!open http://localhost:8888/foo/bar.js
I believe in Windows you would need to do:
:!start http://localhost:8888/foo/bar.js
RegEx to Strip HTML Comments
The title is longer than the code!
<!--(.|\s)*?-->
That is imperfect but works with the comments I need to strip. Best thing about it is that it handles line endings so multiline comments don’t confuse it.
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 jquery, Snippets, Zend Framework 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");





