Why won't Jade indent the code? - node.js

I cannot get Jade to indent the code in the for loop correctly:
div.container
- var cell = 0
- for(var i = 0; i < results.Items.Item.length; i++)
- if(cell == 0)
div.row
div.span3
div.cell
span #{results.Items.Item[i].ItemAttributes.Title}
- else
div.span3
div.cell
span #{results.Items.Item[i].ItemAttributes.Title}
- if(cell < 3)
- cell++;
- else
- cell = 0
The code in the else statement does not go inside div.row but instead renders under div.container, or on the same level as div.row. I need the code in the else statement to render inside div.row. How can I get Jade to do this?
What I'm trying to do is to get div.row to render and then after every 4th div.cell (as counted by the cell object) render another div.row

I believe you have to do it like this:
div.container
- var cell = 0
- for(var i = 0; i < results.Items.Item.length; i++)
div.row
div.span3
div.cell
span #{results.Items.Item[i].ItemAttributes.Title}
- if(cell < 3)
- cell++;
- else
div.row
- cell = 0

use curly braces "{" and "}"
div.container
- var cell = 0
- for(var i = 0; i < results.Items.Item.length; i++){
- if(cell == 0){
div.row
div.span3
div.cell
span #{results.Items.Item[i].ItemAttributes.Title}
- }
- else{
div.span3
div.cell
span #{results.Items.Item[i].ItemAttributes.Title}
- }
- if(cell < 3)
- cell++
- else
- cell = 0
- }

Related

How to write csv file from the results after some statistics using ImageJ macro?

After some image processing using ImgaeJ macro, I have a ‘Results’ tab which contains 2 columns A and B. Lets’s say I have 50 rows of data.
Now I want to subtract the value of last row from all other 49 rows above in column B.
After this, I want to write all the values in “.csv” file (column A, B and C with 49 values each).
Below is the part of the code. I think the only problem is fetching the values from arrays that the script can write to the csv file.
Array.getStatistics command only exports the mean, std values for a given column. I'm interested in fetching all 49 values.
directory = getDirectory("Choose a Directory");
resultFilename = directory + Dialog.getString() + ".csv";
A = newArray(nResults() - 1);
B = newArray(nResults() - 1);
D = getResult("B", nResults() - 1);
for (i = 0; i < nResults() - 2; i++) {
A[i] = getResult("A", i);
B[i] = getResult("B", i);
C[i] = A[i] - D;
}
Any idea about what is the command to get the values of A[i], B[i] and C[i]?
Looking forward for some help here.
Thank you.
One solution is to write to the file as you do the calculations. I have modified your example (untested) to show how this works.
directory = getDirectory("Choose a Directory");
resultFilename = directory + Dialog.getString() + ".csv";
f = File.open(resultFilename);
A = newArray(nResults() - 1);
B = newArray(nResults() - 1);
// no C array is made so:
C = newArray(nResults() - 1);
D = getResult("B", nResults() - 1);
for (i = 0; i < nResults() - 2; i++) {
A[i] = getResult("A", i);
B[i] = getResult("B", i);
C[i] = A[i] - D;
// should the line above should be C[i] = B[i] - D;
print(f, d2s(A[i],6) + " \t" + d2s(B[i],6) + " \t" + d2s(C[i],6));
}
File.close(f);
Note that you don't need to make the arrays at all and can just write to the file (again this is untested):
directory = getDirectory("Choose a Directory");
resultFilename = directory + Dialog.getString() + ".csv";
f = File.open(resultFilename);
D = getResult("B", nResults() - 1);
for (i = 0; i < nResults() - 2; i++) {
ai = getResult("A", i);
bi = getResult("B", i);
ci = ai - D;
// should the line above should be ci = bi - D;
print(f, d2s(ai,6) + " \t" + d2s(bi,6) + " \t" + d2s(ci,6));
}
File.close(f);
I have used " \t" (tab character) as a separator not comma.

Maximum Rectangle with all same number

Given a array n X m. You need to find the largest rectangle which contains all same number in it.
Example:
1 2 2 5
1 2 2 4
2 2 2 3
here the answer should be 6.
I can solve this if the question ask about largest square. Here is my approach -
for(int i=0; i<n; i++) for(int j=0; j<m; j++) {
if(!i && !j) dp[i][j] = 1;
else if(a[i][j] == a[i-1][j-1] &&
a[i][j] == a[i-1][j] &&
a[i][j] == a[i][j-1])
dp[i][j] = min({dp[i-1][j-1], dp[i][j-1], dp[i-1][j]}) + 1;
else dp[i][j] = 1;
}
Then the answer should be the maximum number in the dp table. How can I modify this to get the largest rectangle?
You will need a 3d array to store length and breadth of rect ie dp[INT_MAX][INT_MAX][2].
By replacing-
else if(a[i][j] == a[i-1][j-1] &&
a[i][j] == a[i-1][j] &&
a[i][j] == a[i][j-1])
dp[i][j] = min({dp[i-1][j-1], dp[i][j-1], dp[i-1][j]}) + 1;
With-
else if(a[i][j] == a[i-1][j-1] &&
a[i][j] == a[i-1][j] &&
a[i][j] == a[i][j-1])
{
//for length along j
dp[i][j][0]=min(dp[i][j][0]+1,dp[i-1][j-1][0]+1,dp[i][j][0]);
//for length of rectangle along i
dp[i][j][1]=min(dp[i][j][1],dp[i-1][j-1][1]+1,dp[i][j][1]);
}
Rest are trivial cases where the rectangle is single lined and can be solved using simple if-else.

jade - using if statement

I want to get following if condition in jade.
each item,count in display
if(count % 3 === 0)
{
ul.thumbnails
}
li.span6 //nested under ul
//more code nested under li
I googled searched a lot, but with no luck.
Basically, I want to make a new list for every count which is divisible by 3
I tried this:
mixin display_list
li
//code
each item,count in display
-if(count === 0 )
ul.thumbnails
mixin display_list
-else
mixin display_list
It still doesn't work!
Since Jade forces you to indent stuff that is nested, I think the only way (not sure, but it's certainly the most straightforward) is to do it like this:
- var i = 0;
- while(i < display.length)
ul.thumbnails
- var k = i + 3
- while(i < k && i < display.length) // Will repeat three times, unless display.length isn't large enough
li.span6 //nested under ul
//more code nested under li
- i++
Assuming display is an array
(This answer has been updated, the former answer was completely wrong)
Update 2: Fixed that k could be greater than display.length
I found a way but I don't think it is the correct way to do it.
a = [1,2,3,4,54,6,7,8,9,4,5]
each item, i in a
if i % 2 == 0
|<ul>
li #{item}
if (i+1) % 2 == 0
|</ul>

Make Rows with large data set in Jade Templating in Node.js

I have about 100 items that I use jade iteration to write each one of them to html. However, I'm struggling to find an elegant way to separate the items into rows of three.
To be clear, I want something like this:
.row
.item1
.item2
.item3
.row
.item1
... and so on...
I've tried things with inline javascript like this with no luck:
- var a = 0;
each item in list
- a++;
- if(a % 3 == 0)
.row-fluid
.span3(id='#{item.id}')
p #{item.id}
- else
.span3(id='#{item.id}')
p #{item.id}
note: this kills the list
while list.length > 0
.row
for item in list.splice(0, 3)
.span3(id=item.id)
p= item.id
let me know if this works as i just wrote it off the top of my head
- var i = -3;
while i <= list.length
- i += 3;
.row
each item in list.slice(i, i+3)
.span3(id='#{item.id}')

Loop over two conditions in a template in play framework?

What is the way of doing this in the view template in playframework? I have tagged with groovy, because apparently, the play template engine is based on groovy.
%{
for(int i=0, int j = 0; i < userdata.size(), j < user.size();i = i + 4, j++){
}%
<div style="text-align: center;">
<h3>
${foo.get(j)}
</h3>
</div>
If this is not possible, or just for curiosity sake:
I also tried passing foo as a hashmap, the keys of which are already present in userdata. I tried something like this but to no avail:
${foo.each{ k, v -> println "${k}:${v}" }}
Since you are talking about groovy, I presume you are using playframework 1.x. Playframework 2 uses scala templates.
You can loop over two conditions, like you would do in any other language. The syntax is just a little different.
Java:
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++) {
System.out.println(String.format("i: %d, j: %d", i, j));
}
}
playframework template:
#{list items:0..10, as:'i'}
#{list items:0..10, as:'j'}
<p>i: ${i}, j: ${j}</p>
#{/list}
#{/list}
Check out the documentation for the #{list} tag
It seams that you can't do this with play template builtin tags. Furthermore, I also have a compilation failure using for loop with multi-parameter initialization in a play 1.2.4 template. You can make it work with a while loop :
%{
int j = 0, i = 0;
while (i*i <= j) {
}%
${i}^2 <= ${j}
%{
i++;
j = j+2;
}
}%
//prints
//0^2 <= 0
//1^2 <= 2
//2^2 <= 4

Resources