Derek Perkins
Derek Perkins

Reputation: 657

Using a Helper in a Vendor - CakePHP

In my CakePHP app, I am defining a Wizard vendor that outputs the HTML for a multistep Wizard type plugin, along with its relevant Javascript code. I'm wanting to use the JsHelper so that I can buffer my code to the bottom of the page.

Everything else is working, including my Javascript code if I just output it directly with the HTML. I just can't quite figure out how to use the JsHelper. Do I use a App:Uses or App:Import statement? When using it in a View, I can just define it on the controller level, but that doesn't work here.

$this->Js->buffer("

$('.mws-wizard').wizard({
    buttonContainerClass: 'mws-button-row', 
    orientation: '$orientation',
    forwardOnly: $forwardOnly
});

");

Upvotes: 0

Views: 597

Answers (2)

thaJeztah
thaJeztah

Reputation: 29007

If you are developing this 'vendor' package yourself, you should not develop it as a 'vendor', but as a plugin.

The vendor folders are meant for including third-party libraries that are not developed with CakePHP in mind (for example to use parts of the Zend Framework in your application).

From the manual:

Note: Loading vendors usually means you are loading packages that do not follow conventions. For most vendor packages using App::import() is recommended.

Create a plugin not a vendor

To develop re-usable code that can be used with different projects/applications, develop your code as a Plugin. Plugins are basically 'mini CakePHP applications'. Classes from a plugin can be used inside your application and vice-versa; a plugin can use CakePHP helpers the same way as you use them in your application.

See Creating Your Own Plugins

Regarding the JsHelper

Contrary to the comment placed by Sam Delaney, your usage of the JsHelper looks fine to me. Adding some script to the Js buffer to output it in the layout seems useful. Just don't try to use it for extended blocks of JavaScript; that should be put in external .js files.

I do recommend to write the JavaScript code yourself and not have the JsHelper generate the code for you (e.g. Don't use $this->Js->get('#foo')->event('click', $eventCode);). This may be personal, but IMO this makes it harder to track/debug your JavaScript and isn't any more readable than just $('#foo').click('event code');

Upvotes: 1

Sam Delaney
Sam Delaney

Reputation: 1305

I've personally never found any use for the JavaScript helper in CakePHP as if you're not careful, you end with getting <script> tags littering your markup, which sometimes makes it quite difficult to debug. From what you describe, you have the JavaScript aggregated and appended at the bottom of your HTML so it isn't as bad as the situation I highlight previously.

Is it not possible to relocate all your JavaScript to .js files to encapsulate all the function for your wizard plugin/vendor? If you could do this, it would be in keeping with MVC principles where you could logically separate the view markup and presentation logic.

Upvotes: 0

Related Questions