ultraq / thymeleaf-layout-dialect

A dialect for Thymeleaf that lets you build layouts and reusable templates in order to improve code reuse

Home Page:https://ultraq.github.io/thymeleaf-layout-dialect/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: I am having a hard time figuring out what LayoutDialect static initializer does

silkentrance opened this issue · comments

class LayoutDialect extends AbstractProcessorDialect {

	static final String DIALECT_NAME = 'Layout'
	static final String DIALECT_PREFIX = 'layout'
	static final int DIALECT_PRECEDENCE = 10

	/**
	 * Apply object extensions.
	 */
	static {
		[
			// Context extensions
			IContextExtensions,

			// Model extensions
			IAttributeExtensions,
			ICloseElementTagExtensions,
			IModelExtensions,
			IProcessableElementTagExtensions,
			IStandaloneElementTagExtensions,
			ITemplateEventExtensions,
			ITextExtensions

		]*.apply()
	}

Searching for this using google did not give me any viable results, i.e. I searched for groovy static initializer object extensions and so on.

How does this work?

And what is this []* syntax. I have been looking for it in the official groovy docs but was not able to find any reference to it. Of course, I could assume that it will somehow magically iterate over all elements in the array and call the apply method on it, but still, interfaces do not have an apply method.
Some groovy black magic?

I should have read the code first :)

Hey there, I'm glad you figured it out 🙂 I'll add that while the static initializer is the current way I'm adding extra methods to many of Thymeleaf's classes, I've since learned how to get Groovy's extension modules feature working and will likely opt for that in future. It's currently sitting in one of the many branches I'm experimenting with, which you can see here if interested: https://github.com/ultraq/thymeleaf-layout-dialect/tree/extensions-module