MediaWiki fundraising/REL1_35
MediaWikiTwig.php
Go to the documentation of this file.
1<?php
34 protected $mTwig;
36 protected $mCallbacks;
37
43 public function __construct( $templatePath, IContextSource $context ) {
44 global $wgTwigCachePath;
45
46 $loader = new MediaWikiTwigLoader( $templatePath, $context );
47 $this->mTwig = new Twig_Environment( $loader, [
48 'cache' => $wgTwigCachePath . '/' . md5( $templatePath ),
49 'auto_reload' => true,
50 'autoescape' => false,
51 ] );
52 $this->mTwig->addExtension( new MediaWikiTwigCallbacks( $context ) );
53 }
54
63 public function render( $template, $params = [] ) {
64 return $this->mTwig->render( $template, $params );
65 }
66}
67
71class MediaWikiTwigCallbacks extends Twig_Extension {
72 protected $mContext;
73
74 public function __construct( IContextSource $context ) {
75 $this->mContext = $context;
76 }
77
78 public function getName() {
79 return 'MediaWiki Twig Handler';
80 }
81
82 public function getFunctions() {
83 return [
84 'wfMessage' => new Twig_Function_Method( $this, 'twig_wfMessage' ),
85 'wfText' => new Twig_Function_Method( $this, 'twig_wfText' ),
86 'wfWikiText' => new Twig_Function_Method( $this, 'twig_wfWikiText' ),
87 ];
88 }
89
98 public function twig_wfMessage( $message, $params = [] ) {
99 return wfMessage( $message, $params )->parse();
100 }
101
110 public function twig_wfText( $message, $params = [] ) {
111 return wfMessage( $message, $params )->text();
112 }
113
122 public function twig_wfWikiText( $wikiText ) {
123 return $this->mContext->getOutput()->parseInlineAsInterface( $wikiText );
124 }
125}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
All exposed MW functions to Twig Templates.
__construct(IContextSource $context)
twig_wfText( $message, $params=[])
Retrieves non-wiki-parsed MW i18n message.
twig_wfWikiText( $wikiText)
Fully parses arbitrary wiki text; use for things like template inclusion.
twig_wfMessage( $message, $params=[])
Fully parses a MW i18n message into HTML.
Provides methods for Twig templates to integrate with Mediawiki.
Wrapper around the Twig templating engine that allows MW code to easily integrate HTML templates.
Twig_Environment $mTwig
render( $template, $params=[])
Renders a Twig template and returns the processed HTML data.
__construct( $templatePath, IContextSource $context)
Interface for objects which can provide a MediaWiki context on request.