Help:Widgets

From Guild Wars 2 Wiki
Jump to: navigation, search

The Widgets extension allows the creation of raw HTML pages that can be embedded (similarly to templates) in normal wiki pages. You do this by creating pages in the Widget namespace. They avoid the security problems of raw HTML in editable wiki pages because the privilege to edit in the Widget namespace is managed.

MediaWiki Extension:Widgets

Widgets on this wiki[edit]

See also: Category:Test widgets, Category:Obsolete widgets

Notes[edit]

General[edit]

  • As mentioned above, only widget editors and sysops can edit widgets to prevent malicious code from being added.
  • Widgets run at the same time as the asyncronous ResourceLoader modules, e.g. jQuery, and will have to wait for the document to be ready before trying to use ResourceLoader dependencies.
  • Parameters can be passed to widgets using SmartySyntax. For example, you could pass a parameter named "clear" to a widget by including the following within the widget code: <!--{$clear|default:both}-->, which would be replaced with the value left for example if used like {{#Widget:widget name|clear=left}}.
  • Use a counter to ensure that scripts and styles defined by the widget will only be included once, even if the widget is used multiple times on a single page.
  • An invalid widget will return a red error string along the lines of "Error in Widget:Widget name." Some smarty parameters will have to be escaped in order to produce a valid widget. Likewise invalid code will produce the same message.

API specific notes[edit]

  • If the language token, i.e. &lang=en, is not specified, it uses the browser's language. All widgets should use a language token to prevent unexpected results.
  • Queries made by wiki widgets also have &wiki=1, which does nothing but theoretically could allow ArenaNet to figure out how much traffic the API receives from wiki pages.
  • Results are not cached, but widget code is cached.

Syntax of a typical widget page[edit]

<noinclude>
== Description ==
This widget performs function X Y Z. The widget is used by [[Template:Widget name ABC]].

=== Parameters ===
; wikiparameter1
: wikiparameter1 description

== Example ==
{{#Widget:Widget name|wikiparameter1=value1}}

[[Category:Widgets]]

</noinclude><includeonly>
<!-- HTML to be inserted by widget -->
<div class="widgetcontainer"><!--{$wikiparameter1}--></div>

<!--{counter assign="WidgetName_counter" name="WidgetName_counter"}--><!--{if $WidgetName_counter eq 1}-->
<style>/* Custom CSS rules for the widget */</style>
<script>/* Custom JavaScript code for the widget */</script>
<!--{/if}--></includeonly>

Commonly used code to wait for the document to be ready[edit]

// defer script execution until the mediawiki loader and jQuery are available
RLQ.push(['jquery', function () {
    console.log('mw.loader and jQuery are available now', $);

    // to load external scripts (here to load User:Example/common.js)
    mw.loader.load('//wiki.guildwars2.com/index.php?action=raw&ctype=text/javascript&title=User:Example/common.js');

    // to depend on other mediawiki modules
    mw.loader.using('mediawiki.api.parse', function () {
        var api = new mw.Api();
        api.parse('[[Sandbox]]').done(function (result) {
            console.log('Parsed HTML', result);
        });
    });
}]);

Notes[edit]