| 1 |
= sfPJS plugin = |
|---|
| 2 |
|
|---|
| 3 |
The `sfPJSPlugin` provides a MVC way to include JavaScript generated by PHP code. It also provides a new JavaScript view class and relies on .pjs templates. |
|---|
| 4 |
|
|---|
| 5 |
== Installation == |
|---|
| 6 |
|
|---|
| 7 |
* Install the plugin |
|---|
| 8 |
|
|---|
| 9 |
{{{ |
|---|
| 10 |
symfony plugin-install http://plugins.symfony-project.com/sfPJSPlugin |
|---|
| 11 |
}}} |
|---|
| 12 |
|
|---|
| 13 |
* Enable the `sfPJS` module in your application `settings.yml` |
|---|
| 14 |
|
|---|
| 15 |
{{{ |
|---|
| 16 |
all: |
|---|
| 17 |
.settings: |
|---|
| 18 |
enabled_modules: [default, sfPJS] |
|---|
| 19 |
}}} |
|---|
| 20 |
|
|---|
| 21 |
* Replace the following lines in `web/.htaccess` |
|---|
| 22 |
|
|---|
| 23 |
{{{ |
|---|
| 24 |
# we skip all files with .something |
|---|
| 25 |
RewriteCond %{REQUEST_URI} \..+$ |
|---|
| 26 |
RewriteCond %{REQUEST_URI} !\.html$ |
|---|
| 27 |
RewriteRule .* - [L] |
|---|
| 28 |
}}} |
|---|
| 29 |
|
|---|
| 30 |
by |
|---|
| 31 |
|
|---|
| 32 |
{{{ |
|---|
| 33 |
# we skip all files with .something |
|---|
| 34 |
RewriteCond %{REQUEST_URI} \..+$ |
|---|
| 35 |
RewriteCond %{REQUEST_URI} !\.html$ |
|---|
| 36 |
RewriteCond %{REQUEST_URI} !\.pjs$ |
|---|
| 37 |
RewriteRule .* - [L] |
|---|
| 38 |
}}} |
|---|
| 39 |
|
|---|
| 40 |
* Clear you cache |
|---|
| 41 |
|
|---|
| 42 |
{{{ |
|---|
| 43 |
symfony cc |
|---|
| 44 |
}}} |
|---|
| 45 |
|
|---|
| 46 |
* You're done. |
|---|
| 47 |
|
|---|
| 48 |
== Usage == |
|---|
| 49 |
|
|---|
| 50 |
To link to a dynamic JavaScript file generated by a symfony action, use the following syntax: |
|---|
| 51 |
|
|---|
| 52 |
{{{ |
|---|
| 53 |
<?php use_helper('PJS') ?> |
|---|
| 54 |
|
|---|
| 55 |
<?php use_pjs('targetModule/targetAction?name=fabien') ?> |
|---|
| 56 |
}}} |
|---|
| 57 |
|
|---|
| 58 |
The `targetModule/targetAction` will be executed by symfony, so it can contain any usual action code: |
|---|
| 59 |
|
|---|
| 60 |
{{{ |
|---|
| 61 |
class targetModuleActions extends sfActions |
|---|
| 62 |
{ |
|---|
| 63 |
... |
|---|
| 64 |
|
|---|
| 65 |
public function executeTargetAction() |
|---|
| 66 |
{ |
|---|
| 67 |
// Business logic here, as usual |
|---|
| 68 |
$this->name = $this->getRequestParameter('name'); |
|---|
| 69 |
|
|---|
| 70 |
// Let's store the time |
|---|
| 71 |
$this->time = time(); |
|---|
| 72 |
} |
|---|
| 73 |
} |
|---|
| 74 |
}}} |
|---|
| 75 |
|
|---|
| 76 |
The `targetAction` view has the layout disabled by default and the response content type is automatically changed to `application/x-javascript`. |
|---|
| 77 |
|
|---|
| 78 |
Unlike normal actions, PJS actions look for a template ending with `.pjs` instead of `.php`. For this example, the default rendered template is `targetActionSuccess.pjs`: |
|---|
| 79 |
|
|---|
| 80 |
{{{ |
|---|
| 81 |
alert('Hello <?php echo $name ?>, it is already <?php echo date('d/m/Y', $time) ?>'); |
|---|
| 82 |
}}} |
|---|
| 83 |
|
|---|
| 84 |
== Alternative syntaxes == |
|---|
| 85 |
|
|---|
| 86 |
You can also include the dynamic JavaScript in one of the following ways: |
|---|
| 87 |
|
|---|
| 88 |
{{{ |
|---|
| 89 |
<?php sfPJSHelper::use_pjs('targetModule/targetAction?foo=bar') ?> |
|---|
| 90 |
|
|---|
| 91 |
<script type="text/javascript" src="<?php echo pjs_path('targetModule/targetAction?foo=bar') ?>" /> |
|---|
| 92 |
|
|---|
| 93 |
<script type="text/javascript" src="<?php echo sfPJSHelper::pjs_path('targetModule/targetAction?foo=bar') ?>" /> |
|---|
| 94 |
}}} |
|---|
| 95 |
|
|---|
| 96 |
== Known limitations == |
|---|
| 97 |
|
|---|
| 98 |
* Internet Explorer is known to cache JavaScript files regardless of HTTP headers. This is means that your template changes, the client will never see it. |
|---|
| 99 |
To bypass this limitation, you should add a `version` parameter to each call to the PJS helpers. |
|---|
| 100 |
|
|---|
| 101 |
{{{ |
|---|
| 102 |
<?php use_pjs('targetModule/targetAction?name=fabien&version=1') ?> |
|---|
| 103 |
}}} |
|---|
| 104 |
|
|---|
| 105 |
Change the version each time you change the template. |
|---|
| 106 |
|
|---|
| 107 |
If your template relies on session parameters, you must use a random / incremental version to forbid client side caching completely. |
|---|
| 108 |
|
|---|
| 109 |
{{{ |
|---|
| 110 |
<?php use_pjs('targetModule/targetAction?name=fabien&version='.time()) ?> |
|---|
| 111 |
|
|---|
| 112 |
<?php use_pjs('targetModule/targetAction?name=fabien&version='.uniqid()) ?> |
|---|
| 113 |
|
|---|
| 114 |
<?php use_pjs('targetModule/targetAction?name=fabien&version='.rand(1000, 9999)) ?> |
|---|
| 115 |
}}} |
|---|
| 116 |
|
|---|
| 117 |
* This is an experimental plugin and as such, the API can change. |
|---|
| 118 |
|
|---|
| 119 |
== Changelog == |
|---|
| 120 |
|
|---|
| 121 |
=== Trunk === |
|---|
| 122 |
|
|---|
| 123 |
* gordon: closed #2273 in changeset #6338 |
|---|
| 124 |
* gordon: closed #2086 |
|---|
| 125 |
|
|---|
| 126 |
=== 2007-04-24 | 0.2.0 beta === |
|---|
| 127 |
|
|---|
| 128 |
* fabien: initial release |
|---|