REST response from an API


(Tim Trodd) #1

Hi,

We are looking into using an external system for our CPD courses.

We would ideally like to be able to use a REST asset which will pass along a course ID to the external system, which will then send us back the course details as response.

I then want to print some of the details within a paint layout.

I have previously used the REST asset just printing the results out using the javascript processing field and nesting this in.

But to do the above I would have to nest in a lot of REST assets to the different sections of the paint layout.

Is there a way to grab the response in a global variable or keyword to be able to use else where within the page e.g. not within the REST asset itself but within the paintlayout it get’s nested into?

Cheers,
Tim

Matrix Version: 5.5.3.0


(Tim Trodd) #2

hmm just had a thought.

I could probably print out the whole object as the REST asset with no styling etc. and then just call that within a script in the paint layout using global asset contents and do it that way.


(Aleks Bochniak) #3

We’ve done something similar on our site


(Peter McLeod) #4

Hi

Yeah, that’s a good option.

If you still wanted to keep it consolidated with in the REST JS asset, you could do mixture of the these approaches where you pass both the course and a reference to the particular property in the REST data that you need for the section in the paint layout. Eg:

%globals_asset_contents_raw:123456^with_get:section=propertyName%

Then in a REST JS asset:

  • Pull in the all data
  • In the JS section set the global ‘section’ var to evaluate against:

var section = "%globals_get_section%";

  • Then get that property from your response, transfer to html and return it.

You would probably want to set an appropriate cache time though, depending on the frequency it would get called.

Thanks
Peter


(Tim Trodd) #5

Thanks Peter! I will give it a go.