OOUI/PHP examples

If you are using OOUI:


 * in a MediaWiki extension, see.


 * in a standalone PHP project, run.

A more extensive set of most PHP widget examples is available in the source code repository. When you clone the repository and/or install it using composer, take a look at.

Setup the library
Before you display any OOUI widgets, you need to set up the library by calling the following functions:

You also need to load the CSS:

Creating a button
To create a button, use the class. The first and only argument to a button's constructor is its configuration.

Before the button is converted to HTML and emitted, the label, target and href can be changed using the and  methods.

The button is now created, and has a label which reads " Still click me! ", but is not yet visible to the user, as it must first converted to HTML and echoed into the document:

Adding JavaScript behaviors
Setting the property will ensure that clicking the button loads a new PHP page. But if you want to use the technique of progressive enhancement to add client-side JavaScript behaviors to the button, you can "infuse" the object on the client side with the JavaScript [ https://doc.wikimedia.org/oojs-ui/master/js/#!/api/OO.ui-method-infuse OO.ui.infuse] method. You must set the property when the widget is configured:

Then you'd load some client-side JavaScript to enhance this widget:

Playing it safe
For extra type safety, you can use the method of the specific type of widget you expect. That is:

This will throw an exception at runtime if PHP and JavaScript disagree about the widget type of. (As of 2015-05, this is not implemented yet, but will be in the future.)