0

I implemented a Groovy/Sling version of this. The application is fully functional (except for the search, which will be implemented soon). The source node is available here.

The problem I am facing now towards the end of the project in the modularisation of scripts/views with Groovy. I implemented a BindingsValuesProvider which works fine.

package io.github.floriansalihovic.petclinic.scripting;

import groovy.xml.*;
import org.apache.felix.scr.annotations.*;
import org.apache.sling.scripting.api.*;
import org.slf4j.*;
import javax.script.*;
import java.io.*;

@Component
@Service
public class MarkupBuilderBindingsValuesProvider implements BindingsValuesProvider {
    private final static Logger logger = LoggerFactory.getLogger(MarkupBuilderBindingsValuesProvider.class);
    @Override
    public void addBindings(Bindings bindings) {
        final PrintWriter out = (PrintWriter) bindings.get("out");
        if (null == out) {
            logger.error("Expected print writer is not available.");
        } else {
            logger.info("Providing binding markupBuilder:{}.", MarkupBuilder.class.getName());
            bindings.put("markupBuilder", new MarkupBuilder(out));
        }
    }
}

I can use "markupBuilder" in scripts directly. After implementing components /apps/petclinic/components/footer, /apps/petclinic/components/header and /apps/petclinic/components/navigation as well as several page components in /apps/petclinic/components/pages, implementing a view with the MarkupBuilder is quite easy and works in the expected way.

markupBuilder.html {}

When using the components, I had to use them in a way in an unexpected way.

sling.include(resource, 'petclinic/components/header')
sling.include(resource, 'petclinic/components/navigation')
markupBuilder.html {
  body {
    // ... adding the specific pages content    
  }
}
sling.include(resource, 'petclinic/components/footer')

I would have thought that i'd use the sling.include with in the closure. But that's definitely not the case, because using it that way would wrap and html element around the included scripts.

I thought that the include would "just" act as a route, but apparently it does a bit more or the markup builder does more then expected (wrapping the included components in an additional HTML tag).

Is there any additional information on how to use Groovy and it's behaviour?

user1725145
  • 3,993
  • 2
  • 37
  • 58
Florian Salihovic
  • 3,921
  • 2
  • 19
  • 26

1 Answers1

0

Apparently its how the the MarkupBuilder and sling.include work together. Working examples can be found the in the source code.

Florian Salihovic
  • 3,921
  • 2
  • 19
  • 26