Manual:HTML templates/es

A partir de la, MediaWiki puede generar contenido en HTML a partir de plantillas de Mustache al servidor y el cliente. Server-side template parsing is implemented in PHP via the TemplateParser class, which acts as a wrapper around the lightncandy library. Client-side templates are supported via  ResourceLoader modules and the   library.

Crear plantillas
Para usar plantillas HTML en tu código, primero crea una plantilla de Mustache con la extensión, por ejemplo,. Las plantillas tienen que contener la menor programación lógica posible, haciéndola fácil de leer y proporcinoando separación de intereses. Si tu plantilla forma parte del núcleo del software MediaWiki, colócala en el directorio. Si este es parte de una extensión, debes crear otro directorio  dentro de la carpeta de dicha extensión para alojarlo. Templates should follow the Mustache-5 specification.

TemplateParser (server-side)
This class finds template files, reads them, compiles them into PHP code, and expands tags in the template using data you provide to produce the output HTML. MediaWiki compiles templates as needed, and uses caching to store the compiled templates if available (see #Caching below). This avoids developers having to compile templates into PHP files during development or as a build step, or the server writing them to the file system during operation.

To use, first create a new instance of the class:

If your Mustache templates do not reside in core's, you need to pass the path to where they reside as the first parameter to the constructor (either relative to your MediaWiki root or relative to the calling file's directory with __DIR__):

You then parse templates into HTML by invoking the  function. The first parameter to this is the name of your template (the part of the filename before ). The second parameter is an array providing the values needed by the Mustache tags in your template. For example,

This replaces any instances of  and   tags in the "MyWidget" mustache template with the values you provided, and returns the resulting HTML. (The  simply prints the generated HTML in the current output context.)

As an example of HTML templating, see as used by.

Caching
TemplateParser attempts to cache the compiled PHP template. It prefers to use  (See ), but falls back to   (a general object cache like Memcached or Redis, see ).

Partials
The cache is keyed on a hash of the template file's contents, so if you change a template file, the compiled template will update (you may need to clear your wiki's front-end cache using ). However, this does not notice changes to "partial" templates that you include with  (bug T113095). So if you change a partial, you need to make cosmetic changes to the parent templates that include it, or restart your cache.

mw.template (client-side)
To use a Mustache template on the client-side, add it to your module definition first:

The template definition above consists of two pieces, a file path and an optional alias. The alias must be suffixed with the name of the templating language (e.g. ' ') so that it knows which compiler to use. ResourceLoader automatically serves the supporting  JavaScript modules, so you don't need to mention anything in. Once you have added the template to your module, you can retrieve it in JavaScript using :

To render the template and data into HTML output, call the compiled template's  function:

Partials
Partials are also supported on the client-side. See https://mustache.github.io/mustache.5.html#Partials for more information.