In part 1 we explored why exactly we may want to build our own jQuery plugin, so now in part two, we will cover exactly how to do so.
Note: these examples are through a bash command line, as I am fairly certain that is the most common way one interacts with Symfony. If you're using Windows, you'll get the gist of what to do, however.
Let's start with initializing the plugin:
./symfony generate:plugin majaxJqueryPlugin |
./symfony generate:plugin majaxJqueryPlugin
Now we will switch to our plugin directory:
cd plugins/majaxJqueryPlugin |
cd plugins/majaxJqueryPlugin
We're going to be providing web resources, so we will want to make web, js, and css directories. We will also make a temp directory to download files to:
mkdir web
mkdir web/js
mkdir web/css
mkdir temp |
mkdir web
mkdir web/js
mkdir web/css
mkdir temp
Prepare a place to download what we need:
Here is our download and copy code. I have decided to host it from here, as jQuery UI doesn't seem to provide direct download links.
wget "https://jmather.com/wp-content/plugins/download-monitor/download.php?id=3"
unzip jquery-ui-1.8.2.custom.zip
cp js/jquery-* ../web/js/
cp -a css/smoothness/ ../web/css/ |
wget "https://jmather.com/wp-content/plugins/download-monitor/download.php?id=3"
unzip jquery-ui-1.8.2.custom.zip
cp js/jquery-* ../web/js/
cp -a css/smoothness/ ../web/css/
Let's clean up after ourselves...
Now for the fun part, to make it work. Since I like things to 'just work', here's how I will do it. I will take a page from jQuery Reloaded and use a helper to actually load the files into the response, but instead of making people manually add it where they want it, we will just automatically shove it in if the plugin is enabled!
It's time to make our helper directory:
Here is the contents of lib/helper/MajaxjQueryHelper.php:
1
2
3
4
5
6
7
| <?php
$jq = '/majaxJqueryPlugin/js/jquery-1.4.2.min.js';
$jqui = '/majaxJqueryPlugin/js/jquery-ui-1.8.2.custom.min.js';
$jquicss = '/majaxJqueryPlugin/css/smoothness/jquery-ui-1.8.2.custom.css';
sfContext::getInstance()->getResponse()->addJavascript($jq, 'first');
sfContext::getInstance()->getResponse()->addJavascript($jqui, 'first');
sfContext::getInstance()->getResponse()->addStylesheet($jquicss, 'first'); |
<?php
$jq = '/majaxJqueryPlugin/js/jquery-1.4.2.min.js';
$jqui = '/majaxJqueryPlugin/js/jquery-ui-1.8.2.custom.min.js';
$jquicss = '/majaxJqueryPlugin/css/smoothness/jquery-ui-1.8.2.custom.css';
sfContext::getInstance()->getResponse()->addJavascript($jq, 'first');
sfContext::getInstance()->getResponse()->addJavascript($jqui, 'first');
sfContext::getInstance()->getResponse()->addStylesheet($jquicss, 'first');
For the last little trick before we enable the plugin, open up config/majaxJqueryPluginConfiguration.class.php and add the following code to the initialize() function:
$helpers = sfConfig::get('sf_standard_helpers', array());
$helpers[] = 'MajaxjQuery';
sfConfig::set('sf_standard_helpers', $helpers); |
$helpers = sfConfig::get('sf_standard_helpers', array());
$helpers[] = 'MajaxjQuery';
sfConfig::set('sf_standard_helpers', $helpers);
Your completed majaxJqueryPluginConfiguration.class.php file should look like the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| <?php
/**
* majaxJqueryPlugin configuration.
*
* @package majaxJqueryPlugin
* @subpackage config
* @author Jacob Mather
* @version SVN: $Id: PluginConfiguration.class.php 17207 2009-04-10 15:36:26Z Kris.Wallsmith $
*/
class majaxJqueryPluginConfiguration extends sfPluginConfiguration
{
const VERSION = '1.0.0-DEV';
/**
* @see sfPluginConfiguration
*/
public function initialize()
{
$helpers = sfConfig::get('sf_standard_helpers', array());
$helpers[] = 'MajaxjQuery';
sfConfig::set('sf_standard_helpers', $helpers);
}
} |
<?php
/**
* majaxJqueryPlugin configuration.
*
* @package majaxJqueryPlugin
* @subpackage config
* @author Jacob Mather
* @version SVN: $Id: PluginConfiguration.class.php 17207 2009-04-10 15:36:26Z Kris.Wallsmith $
*/
class majaxJqueryPluginConfiguration extends sfPluginConfiguration
{
const VERSION = '1.0.0-DEV';
/**
* @see sfPluginConfiguration
*/
public function initialize()
{
$helpers = sfConfig::get('sf_standard_helpers', array());
$helpers[] = 'MajaxjQuery';
sfConfig::set('sf_standard_helpers', $helpers);
}
}
You're done! Now you just have to go back to your project root, and edit your project configuration to add the plugin, and publish it's assets.
Here is the line to add to your ProjectConfiguration's setup() function, just in case you need it:
$this->enablePlugins('majaxJqueryPlugin'); |
$this->enablePlugins('majaxJqueryPlugin');
To publish it's assets:
And now you're ready to use jQuery throughout your Symfony project!
To download a copy of the majaxJqueryPlugin I have made (and save yourself some work!), use the link below, and you just have to follow the last step to enable the plugin in your project:
[download id="4"]