Start page / Template development / Template syntax / Functions / in instructions / class

class(...)

Contents

Given a fully qualified class name as a parameter, the function class(...) provides the requested class object. This function may be used in template set definitions of templates.

Important This function should be used with caution, as its use can quickly lead to a multitude of dependencies. Operating on class objects also tends to complicate output channel code syntax, which is usually simple and easily understandable, and can make output channel code hard to maintain.

It is recommended that this function only be used for resources that are available in the server scope. A limitation of this is that classes from JAR files with server-wide scope cannot use classes from JAR files with module-local scope. The reverse path, however, is possible.

This function is not suitable for project-specific module implementations (scope module), as these should be defined module-locally if possible. In this use case, executables should be used.

Use cases

The following sections introduce several practical use cases for this function.

Using Typed Filters

The function may be used to filter a result set for specific object types, e.g.

$CMS_FOR(for_section,#global.page.children(class("de.espirit.firstspirit.access.store.pagestore.Section"),true))$

This call recursively iterates through all child objects of a page. Using the function class(...), a filter is configured to only observe objects of type de.espirit.firstspirit.access.store.pagestore.Section.

Rounding to Whole Numbers

This function also enables use of static methods of globally accessible classes:

Syntax: class(...).staticMethod(...)

For example, the class Math provides a number of mathematical functions (in the form of static methods) that may be used to round numerical values to the nearest whole number:

$CMS_VALUE(class("java.lang.Math").round(5.5))$

Here, the method Math#round is used to round the supplied value to the nearest whole number and output that result. Of course, the result of this mathematical operation may also be stored in a variable:

$CMS_SET(rounded, class("java.lang.Math").round(5.5))$ 
$CMS_VALUE(rounded)$

Creating and Using Counters

Another use case for the function class(...) is to instantiate an object of a specific type.

Syntax: class(...).new([...])

This instantates an object of the desired type by calling the class constructor with an optional set of parameters. In the following example, this procedure is used to instantiate a counter of type AtomicInteger in a template in order to count and output entries for a table of contents.

$CMS_SET(_chapter, class("java.util.concurrent.atomic.AtomicInteger").new)$
$CMS_VALUE(_chapter.incrementAndGet)$

Class dependent output with "isCase"

You can output values depending on the class, for example java.lang.String, java.math.BigInteger etc., by using the following syntax. The isCase method is used for this. This corresponds to the Java construct “instanceof” (type checking, for example var instanceof java.lang.String).

$CMS_IF(class("java.lang.String").isCase(_var))$
String
$CMS_END_IF$

If “_var” is a string (class java.lang.String) in this example, “String” will be output, otherwise there will be no output.

However, a check against more than one class can be carried out using $CMS_SWITCH(...)$.

© 2005 - 2024 Crownpeak Technology GmbH | All rights reserved. | FirstSpirit 2024.5 | Data privacy