Is there a way to use {#select } tag inside array in dust? - dust.js

Here is the code snippet I tried to achieve index based selection of elements from an array:
{#result}
{#select key={$idx}}
{#lte value=3}
<p>{notes}</p>
<p style="color:grey;">{createdBy}-{createdDate}</p>
{/lte}
{/select}
{/result}
But above code throws error "SyntaxError: Expected end tag for result but it was not found". Can anyone please suggest any fix for this error?

It looks like the error is the curly braces surrounding $idx. Dust references in parameters don't use curly braces (e.g. {#select key=$idx}) or they must have quotes around them (e.g. {#select key="{$idx}"}). So, your template would look something like:
{#result}
{#select key=$idx}
{#lte value=3}
<p>{name}</p>
<p style="color:grey;">{createdBy}-{createdDate}</p>
{/lte}
{/select}
{/result}

Related

string.len() always returns the error "unexpected symbol"

function Main(Inhalt)
print(string.len(Inhalt))
end
Main(Bla)
This is just a example, I run in multiple problems like: "input:3: bad argument #1 to 'len' (string expected, got nil)" (Like here), or anything else with unexpected.
I'm kinda new to this, so please explain it to me from ground up I'm trying to figure out for a pretty long time. I already tried to convert this to a string with tostring(), but yes I'm missing something. Thanks for your help.
In this case Bla either needs to be a string which you can fix by putting quotes around it
function Main(Inhalt)
print(string.len(Inhalt))
end
Main("Bla")
or needs to be a variable that contains a string
Bla="test string"
function Main(Inhalt)
print(string.len(Inhalt))
end
Main(Bla)
Not a lua expert but it seems like you're trying to get the length of the string value Bla. The way you've written it right now does not indicate Bla is of string type. If you change it to the following, this should work.
function Main(Inhalt)
print(string.len(Inhalt))
end
Main("Bla")
Try this:
string1 = "Bla"
Main(string1)
In your code snippet Bla is not defined. Strings are surrounded by "".

A tool/editor to surround single-line (Java/C#/JS) if statement with curly braces preserving it on the same line

Here's what I have:
if (something) throw new Exception("whatever");
I would like to find a tool which will do a one-time pass over all my code and change statements like the above one to this:
if (something) { throw new Exception("whatever"); }
Note that I want everything to stay on the same single line (if it was like this).
Any tool/editor/plugin/script/anything which can do the job, just once, for all my code - is helpful.
Thank you.
Apparently IntelliJ can do this via a combination of 2 settings:
Code Style > Java > If() statement > Force braces > Always
Code Style > Java > Keep when reformatting > Simple blocks in one line

Nokogiri get element if type is in brackets

I have a content like this:
[caption id=\"attachment_3182\" align=\"aligncenter\" width=\"800\" caption=\"blah blah\"]<img class=\"size-full wp-image-3182\" title=\"blah\" src=\"http://www.test.com/blah.jpg\" alt=\"\" width=\"800\" height=\"533\" />[/caption]
<div>other code here</div>
I want to get all caption elements from it, so I'm trying to do something like this:
doc.css("[caption]") and doc.xpath('.//[caption]')
but have had no success.
Try doc.css("[caption]").attr("caption")
I transformed [caption] to a <caption> tag. In my case it was:
text.gsub!("[caption", "<caption").gsub!('"]', '">').gsub!("[/caption]", "</caption>")
after that I was able to get the <caption. tag with Nokogiri.

dust js partials not working inside loop

I'm trying to do something similar to the below code.(trying to print an partial inside a loop)
{#projects
{<greeting}
Hello {.name}
{/greeting}
{/projects}
But im getting an output like below:
Hello
Hello
....
As you can see that partial is not printed inside the loop.
My actual code http://jsfiddle.net/WKxzb/1/
Most of the documentation examples suggest something like this fiddle: http://jsfiddle.net/cnftm/
In short, in greeting.tl:
Hello, {name}!
then in projects.tl:
{#projects}
{>greeting /}
{/projects}
If where you store name didn't happen to be named name:
{#projects}
{>greeting name=someOtherVar /}
{/projects}
Anything you want to be parametrized has to be, ahem, passed as a parameter, and no body is supported.
The exception to these is blocks with inline partials, which are like this fiddle: http://jsfiddle.net/PWYBr/1/
In short, in greeting.tl:
Hello, {+name}Anonymous Coward{/name}!{~n}
then in projects.tl:
{#projects}
{>greeting/}
{<name}
{name}
{/name}
{/projects}
This works great for loops, but you can't reuse the greeting partial ANYWHERE else on the page... basically, whoever writes {<name} last wins, such that this will do weird things:
{>greeting/}
{<name}everybody{/name}
{#projects}
{>greeting/}
{<name}
{name}
{/name}
{/projects}

what is the difference between chunk.write and chunk.render

I saw this,
chunk = chunk.write("<li>").render(bodies.block, context.push(items[i])).write("</li>\n");
Before seeing this code, i thought, render as something similar to flush, and write as something similar to "write in buffer", naturally leading to a code like below.
for loop
chunk.write("something")
end for loop
chunck.render();
But, as you can see in the first code, render is coming in between the writes. Can somebody explain the difference between these two functions.
#JAiro:
After reading your answer i tried the below code:
temaplate: You have {render} {write}
data:
{
"name": "Mick",
"render": function(c,ct,b){
chunk.render("Rendered {~n}");
},
write:function(c,ct,b){
chunk.write("Written {~n}")
}
}
Expected output:
you have Rendered
Written {~n}
Please note the {~n} after the word "Rendered" is interpreted but the {~n} after "Written" is not interpreted.
But the actual output is not same as the expected output.
Could you post a jsfiddle, that will help me in understanding.
The actual output is an empty string, which also indicate that there could be an error in the code.
The chunk.write method writes strings directly to the buffer.
On the other hand, chunk.render resolves variables contained in its argument and then writes the resulting string to the buffer.
You don't have to override the write and render function in the context.
Let me show you how it works.
Template
Hello {name}!, how are you?
Dust compiles the template to convert them in javascript. After compiling that template you are going to obtain something like:
return chk.write("Hello ").reference(ctx.get("name"), ctx, "h").write("! how are you?");
As you can see for "Hello" and "how are you?", dust uses chunk.write because it knows what it should print. However, dust doesn't know the value of {name} until it gets the context (JSON).
For that reason, it uses chunk.reference, because it will have to resolve the value of the variable name in the future. Dust is going to obtain the value of name from the JSON data.
You can read more about dust.js here:
http://linkedin.github.com/dustjs/wiki
And you can see working examples and try yours here:
http://linkedin.github.com/dustjs/test/test.html

Resources