RkuangDev
RkuangDev

Reputation: 65

Rendering a ejs file clients side

Currently trying to work out some ajax on cloning an 'item' template, after each post request im trying to insert a ejs template with the new item's (cloned item's) info, into the main ejs page. But I can't seem to find the correct way, to execute that properly. I've followed the documentations have the front end ejs script.

Currently getting error: resp is not defined

Item display is the ejs template for the all items 'inventory-scroll-list' is the div with all the items resp is the newly created/cloned item (mongoose)

// cloning item with ajax (need to display new item)
$('.cloneItemButton').click(function () {
    const itemId = $(this).attr('id')
    $.ajax({
        method: 'POST',
        url: '/item/' + itemId + "/clone",
        success: function (resp) {
            console.log('Item Cloned: ' + resp._id)
            var html = ejs.render('<%- include("/itemDisplay",  { item: resp }); %>')

            $('.inventory-scroll-list').append(html)
        },
    })
})

Upvotes: 0

Views: 704

Answers (1)

Deepak Gupta
Deepak Gupta

Reputation: 658

var html = ejs.render('<%- include("/itemDisplay",  { item: resp }); %>')

The resp here is a string and won't work because /itemDisplay is not accessible to the browser.

if the ejs file is small and does not depend on other ejs files then you can directly store the contents of that file in a variable

template = 'The entire contents of the ejs file';
var html = ejs.render(template,  { item: resp});

Upvotes: 1

Related Questions