• oguruma

    (@oguruma)


    I have a gallery plugin based on the Javascript Lightgallery plugin. Currently I have a single php function that gets the images from the database and echos out the html and javascript.

    It’s kind of messy using echo to spit out javascript. The javascript file basically just sets various settings for how the gallery is to look and initializes the gallery. I need php for this part because I have to loop through the “gallery” array and spit out an array of the images into the javascript.

    A more appropriate way to do this is probably to use either Ajax or the REST API.

    What are the advantages of using one over the other? And in what cases is using AJAX more appropriate than REST?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Not sure I’m following.

    Ajax would be useful for fetching new images dynamically that weren’t in the initial page load. You can add a custom endpoint to the REST API and then use Ajax (these days, the Fetch API) to interact with it from the client-side. So if this is what you’re trying to do, it’s not really a REST versus Ajax situation. They work together.

    Is that what you’re trying to do? Or once the page loads, the images that are on the page don’t change?

    If the latter, it sounds like you might just need to refactor your existing code.

    Could share the code you’re working with?

    Rajnish Vijya

    (@rajnishvijya)

    Using Ajax or Fetch API with a custom REST endpoint is suitable. Ajax and REST can work together for dynamic updates. If the displayed images remain static, consider refactoring PHP for code organization instead of adding Ajax or REST

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘REST vs Ajax for gallery?’ is closed to new replies.