How to fetch data on custom template from Rest api

I am trying to use third party integration with the ghost custom template so i have created a template and inside that i am calling a js script to get that data from api and after that i need to pass that data to the handlebars instead of directly appending html i want to use its own compiler and pass the data which i am getting from api.

Anyone knows if that is possible through this approach?

Thanks

You can definitely call the API with javascript from within your theme files, but you can’t pass the contents back to handlebars. When a request comes to your server, server figures out what handlesbars page to use and what content, then it parses the handlebars and hands the user’s browser the HTML, which can include javascript. The user’s browser can run your javascript and get more content from the API, but there’s not a way to pass the javascript results back to the server to do more parsing, nor is there a way to get the server to run your javascript.

But… you can absolutely take that API results and use them to put content on the page that fits nicely with the HTML generated from the server. You’ll just need to do it with javascript rewriting/adding to parts of the page.

Thanks for the reply got the answer.

But is there any other server side approach available in ghost where we can show custom data from api when page loaded.

Thanks in advance

You are limited to what handlebars can do, but there’s a lot there. Can you say more about what you’re trying to accomplish?

Okay let me try to explain a bit.

I have one website already created and well managed in ghost everything is fine there.

Now we have different web app for some services we offer in that app only,
Now there is one integration i am trying to do so that i can show some of the services from apis inside one of my page inside ghost because we have landing page in ghost so we want to show some data here first.

This is something i want to create.

I’m not totally sure I’ve understood your plans, but you can absolutely use javascript (running in the user’s browser) to get some content from your external API and display it on the Ghost page.** You can’t (without hacking the Ghost core) get the Ghost server to request and parse content from elsewhere.

**If that API has a key/secret, then you’re going to need a different plan, since most API keys aren’t safe for use in the browser.

Ghost would need a middleware function, that would solve a lot of missing features in Ghost.
Fetching third party data, via a API, then passing the data to the handlebars template.