Mohammed Alaif H
Mohammed Alaif H

Reputation: 1

i want to call the node/nest api in crafter cms

iam trying to call the nest/node api in crafter cms but it is throwing errors

1)This is my Groovy controller code

import groovy.json.JsonSlurper



def apiUrl = "http://localhost:3000/"
def json = new URL(apiUrl).text //assuming it's json
def object = new JsonSlurper().parseText(json)



templateModel.myObject



2)This is my ftl code

<#import "/templates/system/common/crafter.ftl" as crafter />



<#assign x = contentModel.name_s!"" />



<@crafter.h1>${myObject.x}<@crafter.h1 />

3)after saving the above codes iam getting this error

!(https://i.sstatic.net/XQb7f.png)

Upvotes: 0

Views: 110

Answers (2)

Mohammed Alaif H
Mohammed Alaif H

Reputation: 1

This is my JSON data:

{
    "_id": "632833ecf89459939bd47dd5",
    "name": "Random Blood Sugar",
    "price": 350,
    "description": "Random Blood SugarTimely screening can help you in avoiding illnesses and with Healthians Random Blood Sugar you can keep a track of your health in the best way because Healthians is India's leading health test @ home service with more than 20 lac satisfied customers across 140+ cities.",
    "imgURL": "https://i0.wp.com/post.healthline.com/wp-content/uploads/2021/10/blood-test-sample-tube-1296-728-header.jpg?w=1155&h=1528",
    "__v": 0
  },

This is my ftl code:

<#import "/templates/system/common/crafter.ftl" as crafter />
<!doctype html>
<html lang="en">
  <head>
    <#-- Insert your head markup here -->
    <@crafter.head />
  </head>
  <body>
    <@crafter.body_top />
    <#-- Insert your body markup  here -->
    <@crafter.h1 $field="demo_s">${myObject.price}</@crafter.h1>
    <@crafter.body_bottom />
  </body>
</html>

but iam getting the same error. @Russ Danner

Upvotes: 0

Russ Danner
Russ Danner

Reputation: 693

Is the response at http://localhost:3000/ the following URL JSON?

If not, trying to parse it is going to throw an error.

Also, in your Freemarker template you have an error here:

<@crafter.h1>${myObject.x}<@crafter.h1 />

Since you assigned contentModel.name_s to x, this should simply be:

<@crafter.h1>${x}<@crafter.h1 />

Assuming the response from Node is JSON, and for the sake of the example, let's say it looks like this:

{ mySum: 42, myMessage: "Hello World", theBest: "CrafterCMS" }

Then you can use these values in your freemarker template with:

${myObject.mySum}

myObject is available in the template as myObject because you added it to the template in the controller.

Upvotes: 1

Related Questions