Thank you very much for your tips. I used your script and edited it a bit and now it works. Though I still don't understand why they use loops in the Todo-tutorial if it is not a bad practice. And I still don't understand why my original method doesn't work, even though it might not be a best practice. This is my current code that seems to do the job:
- function renderContents($element, Blocks, Containers) {
- var count = Blocks.length + Containers.length;
- var $newElement;
- var b = 0; var c = 0;
- for(var i=0; i < count; i++){
- if (typeof Blocks[b] !== "undefined" && parseInt(Blocks[b].BlocksPage.index) <= i) {
- $element.append($.View("//view/block.ejs", Blocks[b]));
- b++;
- } else {
- $newElement = $(document.createElement('div')).model(Containers[c]);
- renderContents($newElement, Containers[c].Blocks, Containers[c].Containers);
- $element.append($newElement);
- c++;
- }
- }
- }
- Page.findOne({id: <?=$page['id']?>}, function( page ){
- // print out the todo name
- console.log(page);
- $('body').model(page.Layout);
- renderContents($('body'), page.Layout.Blocks, page.Layout.Containers);
- $('html').model(page);
- $('.cake-sql-log').appendTo('body');
- //$('body').html('//view/layout.ejs', page.Layout);
- });