app.get('/', function (req, res) { Photo.find({}, function (error, photos) { var photoList = ''; photos.forEach(function (photo) { photoList += '\ <a href="/sight-photo" style="background-image: url(' + photo.filePhoto + ')" class="photo_gallery-wrapper">\ <span class="photo_gallery-title">' + photo.titlePhoto + '</span>\ <span class="photo_gallery-desc-hover">\ </span>\ </a>\ '; }); res.render('index.ejs', {photoList: photoList}); }); }); <!-- begin snippet: js hide: false console: true babel: false --> 
 <% if (photoList.length) { <% for(var i=0; i < photoList.length; i++) { %> <a href="/sight-photo" style="background-image: url(<%=photo.filePhoto%>" class="photo_gallery-wrapper"> <span class="photo_gallery-title"><%=photo.titlePhoto%></span> </a> <% } %> <% } %> 

  • one
    Cruel, of course. - Suvitruf

2 answers 2

Controller:

 app.get('/', function (req, res) { Photo.find({}, function (error, photos) { res.render('index.ejs', { photos: photos }); }); }); 

Template, index.ejs :

 <% photos.forEach(function(photo) { %> <a href="/sight-photo" style="background-image: url(<%=photo.filePhoto%>" class="photo_gallery-wrapper"> <span class="photo_gallery-title"> <%=photo.titlePhoto%> </span> </a> <% } %> 
  • I did it differently. Thank you very much for your help - vita_run
  • @vita_run write the answer there so that others can help too. - Suvitruf

The controller should not be html. In the template, transfer the photo and run the cycle there:

 <% if (photoList.length) { <% for(var i=0; i < photoList.length; i++) { %> <!-- Здесь html --> <% } %> <% } %> 
  • previously used hjs. I thought it was the same here. Corrected pattern. Tell me, please, what then in the controller will remain? - vita_run
  • @vita_run remove all code for working with html from the controller to the template. A template to transfer an array of photos and there already in the cycle to form html. - Suvitruf