1

I am able to get the xml returned by the below groovy script. However, how can I get the output within a Page? Can anyone help me how I can create a content model, create content items using that model, and have a FreeMarker template generate HTML based on the model.

Thanks in advance.

 import org.craftercms.core.cache.CacheLoader
 import org.craftercms.core.service.CachingOptions
 import groovy.json.JsonSlurper

 def cacheService = applicationContext["crafter.cacheService"]
 def cacheContext = siteContext.getContext()
 def myCacheKey = "aServiceCallResponse"
 def loader = new ExternalServiceLoader()
 def value = ""
 def responseItem = cacheService.get(cacheContext, myCacheKey)

 if(responseItem == null) {
    def myObject = loader.load()
    value = myObject

    // cache the value with a loader to periodically refresh its value
    def cachingOptions = CachingOptions.DEFAULT_CACHING_OPTIONS
    try {
        cacheService.put(cacheContext, myCacheKey, myObject, [], cachingOptions, loader)
    }
    catch(err) {
        logger.error("error adding ${myCacheKey} to cache: ${err}")
     }
 }
 else {
    value = responseItem
 }
 return value

 class ExternalServiceLoader implements CacheLoader {

 Object load(Object... parameters) throws Exception {
       def externalServiceHost = "http://api.population.io/1.0"
       def externalServiceURL = "/population/United%20States/today-and-tomorrow/"
       // call the service
       def response = (externalServiceHost+externalServiceURL).toURL().getText()
       // parse service's the JSON response to an object
       def result = new JsonSlurper().parseText( response )
       return result
    }
 }

1 Answers1

2

What kind of groovy script do you currently have? Also, are you using CrafterCMS 3.x?

If you already have it as a REST script, the easiest is probably to render it client side by calling your service via JavaScript (i.e. AJAX). Then you can use the data from the call to render the page in whatever way you prefer - e.g. React, Vue, jQuery. If you do this, best you return JSON instead of XML.

If you want to go down the FTL path, as far as I understand from your question, sounds like you'd need to make your groovy script a controller script. Those need to return the path to the FTL you want to render and they need to be in {site}/scripts/controllers/*. From the template, then you can get access to templateModel and any props you put in there.

Have a look at the docs: https://docs.craftercms.org/en/3.0/developers/projects/engine/api/groovy-api.html

The second part of your question...

Can anyone help me how I can create a content model, create content items using that model, and have a FreeMarker template generate HTML based on the model.

In general, to create content models, you'd go to site config > content types > "Create new type". When creating a new type you must associate the model (content type) with a template. Then to create content items, you go to your site dashboard, and from the Pages tree you right click to create New Content and select your recently created content type. From there on everything works automatically (FTL, rendering, model variables available from your FTL, etc.)

You could also create components instead of pages if those were more suitable for what you're doing.

Roy Art
  • 578
  • 5
  • 10