Skip to content

Extensions

Thorsten Marx ㋡ edited this page Nov 10, 2023 · 9 revisions

Extension are writen in good old JavaScript.

Put your extension into the host/extensions folder. All files with the nameing convention name.extension.js are loaded ad system startup.

Add custom http endpoint

Attention: Keep in mind, that all http endpoint extensions are loaded unter the endpoint /extensions. What that means is, you can not put content under this url name.

import { UTF_8 } from 'system/charsets.mjs';
import { $http } from 'system/http.mjs';

$http.get("/test", (request, response) => {
	response.addHeader("Content-Type", "text/html; charset=utf-8")
	response.write("ich bin einen test extension!öäü", UTF_8)
})

The endpoint is available at http://your_host:your_port/extensions/test

Modules

To structure your extension code, you can create modules. But keep in mind, your modules must use the .mjs extension. Otherwise our js engine will not load your modules correctly.

System modules

cms-server comes with some system modules. All system modules are in the systems package, so you can not use system as folder name for custom modules.

http

import { header } from 'system/http.mjs';

exchange.getResponseHeaders().add(header("Content-Type"), "text/html; charset=utf-8");

charsets

import { UTF_8, UTF_16, ISO_88591 } from 'system/charsets.mjs';

exchange.getResponseSender().send("I'm a test extension!", UTF_8);

logging

import { getLogger } from 'system/logging.mjs';
const logger = getLogger("extensions");
logger.debug("debug log from test extension");

template

import { $template } from 'system/template.mjs';

$template.registerTemplateSupplier(
	"myName",
	() => "Thorsten"
)

$template.registerTemplateFunction(
	"getHello",
	(name) => "Hello " + name + "!"
)

Use template extensions in template

<div th:with="name = ${myName.get()}">
	<p th:th:text="${name}"></p>
	<!-- Thorsten -->
</div>
<div th:with="hello = ${getHello.apply('Thorsten')}">
	<p th:text="${hello}"></p>
	<!-- Hello Thorsten -->
</div>

files

import { $files } from 'system/files.mjs';

let content = $files.readContent("extras/products.json")

tags

With #65 we added support for tags.

$tags.addTag(
	"youtube",
	(params) => `<iframe width="${params.getOrDefault("width", "560")}" height="${params.getOrDefault("height", "315")}" src="https://www.youtube.com/embed/${params.getOrDefault("video", "")}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>`
)

Usage in markdown

[[youtube video='C0DPdy98e4c'/]]
Clone this wiki locally