use of org.xwiki.template.Template in project xwiki-platform by xwiki.
the class UntypedEventListener method evaluateVelocity.
private XDOM evaluateVelocity(Event event, Object source, DocumentReference userReference, String templateContent) throws Exception {
ScriptContext currentScriptContext = scriptContextManager.getCurrentScriptContext();
currentScriptContext.setAttribute(EVENT_BINDING_NAME, event, ScriptContext.ENGINE_SCOPE);
currentScriptContext.setAttribute(SOURCE_BINDING_NAME, source, ScriptContext.ENGINE_SCOPE);
try {
Template customTemplate = templateManager.createStringTemplate(templateContent, userReference);
return templateManager.execute(customTemplate);
} finally {
currentScriptContext.removeAttribute(EVENT_BINDING_NAME, ScriptContext.ENGINE_SCOPE);
currentScriptContext.removeAttribute(SOURCE_BINDING_NAME, ScriptContext.ENGINE_SCOPE);
}
}
use of org.xwiki.template.Template in project xwiki-platform by xwiki.
the class TemplateLESSSource method getContent.
@Override
public String getContent() throws FileNotFound, CannotReadFile {
try {
// We execute velocity on the main skin file only (which is included by SSX objects using LESS).
//
// This a limitation we introduce because when we do an HTML export, we must execute velocity to know which
// resources have to be included in the ZIP file, but we avoid executing LESS and we use the cache instead
// (otherwise the export would be too slow).
//
// When we do an HTML export, we execute Velocity on the main skin file, but not on any .less.vm that the
// skin might have. Actually we have no way to know which .less.vm are included, without running LESS.
//
// That is why we do not execute Velocity on any ".less.vm" file but only on the main skin template.
String mainSkinTemplate = "less/" + CachedLESSCompiler.MAIN_SKIN_STYLE_FILENAME;
if (mainSkinTemplate.equals(templateName)) {
return templateManager.renderFromSkin(templateName, skin);
}
// Otherwise, return the raw content
Template template = templateManager.getTemplate(templateName, skin);
TemplateContent templateContent = template.getContent();
return templateContent.getContent();
} catch (Exception e) {
throw new CannotReadFile();
}
}
use of org.xwiki.template.Template in project xwiki-platform by xwiki.
the class DefaultVelocityManager method getVelocityEngineMacrosTemplate.
/**
* @return the key used to cache the Velocity Engines. We have one Velocity Engine per skin which has a macros.vm
* file on the filesystem. Right now we don't support macros.vm defined in custom skins in wiki pages.
*/
private Template getVelocityEngineMacrosTemplate() {
Template template = null;
Map<String, Template> templateCache = null;
Skin currentSkin = this.skinManager.getCurrentSkin(true);
// Generating this key is very expensive so we cache it in the context
ExecutionContext econtext = this.execution.getContext();
if (econtext != null) {
templateCache = (Map<String, Template>) econtext.getProperty(VELOCITYENGINE_CACHEKEY_NAME);
if (templateCache == null) {
templateCache = new HashMap<>();
econtext.setProperty(VELOCITYENGINE_CACHEKEY_NAME, templateCache);
} else {
template = templateCache.get(currentSkin.getId());
}
}
if (template == null) {
template = this.templates.get().getTemplate("macros.vm");
if (templateCache != null) {
templateCache.put(currentSkin.getId(), template);
}
}
return template;
}
use of org.xwiki.template.Template in project xwiki-platform by xwiki.
the class DefaultVelocityManager method getVelocityEngine.
/**
* @return the Velocity Engine corresponding to the current execution context. More specifically returns the
* Velocity Engine for the current skin since each skin has its own Velocity Engine so that each skin can
* have global velocimacros defined
* @throws XWikiVelocityException in case of an error while creating a Velocity Engine
*/
@Override
public VelocityEngine getVelocityEngine() throws XWikiVelocityException {
// Note: For improved performance we cache the Velocity Engines in order not to
// recreate them all the time. The key we use is the location to the skin's macro.vm
// file since caching on the skin would create more Engines than needed (some skins
// don't have a macros.vm file and some skins inherit from others).
// Create a Velocity context using the Velocity Manager associated to the current skin's
// macros.vm
// Get the location of the skin's macros.vm file
XWikiContext xcontext = this.xcontextProvider.get();
final Template template;
if (xcontext != null && xcontext.getWiki() != null) {
template = getVelocityEngineMacrosTemplate();
} else {
template = null;
}
String cacheKey = template != null ? template.getId() : "default";
// Get the Velocity Engine to use
VelocityEngine velocityEngine = this.velocityFactory.getVelocityEngine(cacheKey);
if (velocityEngine == null) {
// created only when a new skin is created and not be on the main execution path.
synchronized (this) {
velocityEngine = this.velocityFactory.getVelocityEngine(cacheKey);
if (velocityEngine == null) {
// Gather the global Velocity macros that we want to have. These are skin dependent.
Properties properties = new Properties();
// Loader
if (!this.velocityConfiguration.getProperties().containsKey(RESOURCE_LOADER)) {
properties.setProperty(RESOURCE_LOADER, "xwiki");
properties.setProperty(RESOURCE_LOADER_CLASS, XWikiWebappResourceLoader.class.getName());
}
if (xcontext != null && xcontext.getWiki() != null) {
// Note: if you don't want any template to be used set the property named
// xwiki.render.velocity.macrolist to an empty string value.
String macroList = xcontext.getWiki().Param("xwiki.render.velocity.macrolist");
if (macroList == null) {
macroList = "/templates/macros.vm";
}
properties.put(RuntimeConstants.VM_LIBRARY, macroList);
}
velocityEngine = this.velocityFactory.createVelocityEngine(cacheKey, properties);
if (template != null) {
// template by default
try {
final VelocityEngine finalVelocityEngine = velocityEngine;
this.authorExecutor.call(() -> {
finalVelocityEngine.evaluate(new VelocityContext(), NullWriter.NULL_WRITER, "", template.getContent().getContent());
return null;
}, template.getContent().getAuthorReference());
} catch (Exception e) {
this.logger.error("Failed to evaluate macros templates [{}]", template.getPath(), e);
}
}
}
}
}
return velocityEngine;
}
use of org.xwiki.template.Template in project xwiki-platform by xwiki.
the class DefaultNotificationFilterDisplayerTest method displayWithCustomTemplate.
@Test
public void displayWithCustomTemplate() throws Exception {
Template fakeTemplate = mock(Template.class);
when(templateManager.getTemplate(any(String.class))).thenReturn(fakeTemplate);
NotificationFilter filter = mock(NotificationFilter.class);
when(filter.getName()).thenReturn("filterName");
mocker.getComponentUnderTest().display(filter, mock(NotificationFilterPreference.class));
verify(templateManager, times(1)).execute(eq(fakeTemplate));
}
Aggregations