How to call $services.wiki.currentWikiDescriptor.prettyName; in a javascript extension?---solved

Dear every one:
my code is like the following

require(['jquery'], function ($) {
$('.container-fluid').append('<p/><p style="color:white">').append($services.wiki.currentWikiDescriptor.prettyName).append('welcome!</p>');

});

but it cannot run. for $services is not defined
i guess i should add some js dependency in require like
require([‘jquery’, ‘xwikixxxx’]
but i cannot find the name about xxx,i cannot find xwiki-services
any suggestion is helpful
thanks in advance!

Hello.

Here, you need 2 things.

First, in your JavaScript extension (JSX), you need to set the option “parse content” (see: How to use Velocity in parsed content in JavaScript Extension).

Then, you need to understand that Velocity will be executed on the server-side, not on the client-side. So any result returned by velocity should be written as valid javascript code.

So here, it means you need to put the wiki pretty name between quotes.

$('.container-fluid').append('<p/><p style="color:white">').append("$services.wiki.currentWikiDescriptor.prettyName")

That’s it!

1 Like

Now if what you need is the current wiki there is a pure client side way to get it which would probably be much safer and faster than enabling velocity in your javascipt. See https://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/FrontendResources/JavaScriptAPI/#HGetsomeinformationaboutthecurrentdocument for the id and maybe the pretty name can be found somewhere in the page too.

thanks alot, this fixed my problem

thanks