what is the difference between chunk.write and chunk.render - dust.js

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

Related

action script 2.0 simple string comparing with conditional statement

So I have this crazy problem with comparing 2 strings in ActionScript 2.0
I have a global variable which holds some text (usually it is "statistic") from xml feed
_root.var_name = fields.firstChild.attributes.value;
when I trace() it it gives me the expected message
trace(_root.var_name); // echoes "statistik"
and when I try to use it in conditional statement the rest of code is not being executed because comparing :
if(_root.overskrift == "statistik"){
//do stuff
}
returns false!
I tried also with:
if(_root.overskrift.equals("statistik"))
but with the same result.
Any input will be appreciated.
Years later but on a legacy project I had the same problem and the solution was in a comment to this unanswered question. So let's make it an anwer:
Where var a:String="some harmless but in my case long string" and var b:String="some harmless but in my case long string" but (a==b) -> false the following works just fine: (a.indexOf(b)>=0) -> true. If the String were not found indexOf would give -1. Why the actual string comparison fails I coudn't figure out either. AS2 is ancient and almost obsolete...

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}

Dart List within a Map

I have a Map in Dart (originally loaded from JSON) that looks something like this:
somevar = {
'Title': 'Some object',
'items': [{'title': 'Item 1 Title'}, {'title': 'Item 2 Title'}]
}
For some reason somevar['items'] doesn't behave quite like a list.
I get Exception: NoSuchMethodError : method not found: 'iterator' if I attempt to iterate over the list.
I also get a similar error if I try somevar['items'].length
If I manually load this "list" like this: someList = new List(somevar['items']); then it works as expected.
Any idea why this is that case, and what I'm doing wrong? For me the natural expectation would be that a "list" parsed from JSON will behave exactly like the List() object.
Never mind, seems that I had a deeper issue in my code that cause my somevar variable to be null (even though it should have the map.
Anyway, I'm marking this as solved for now so not to waste anyone's time.

XSLT 2.0. Loop inside a Function

Hitting my wall here...
I've got the following data where a Primary Employee may have multiple dependents. I need to create a function that will match the Employee's SSN (ab:SSN) against the Dependent_SSN and determine if one of them is a 'Spouse'. If so, then we'll return the Dependent_SSN of the 'Spouse'.
If not, we'll move on and return the next non-'Spouse' Dependent_SSN.
I'm trying to create a function as I think I'll need this more than once. The code snippet resides inside of an existing template that is doing other looping functionality.
I've tried this but Oxygen returns an error:
<xsl:function name="ab:PQB">
<xsl:param name="EE_SSN">
</xsl:param>
<xsl:for-each select="/ab:Report_Data/ab:Report_Entry[ab:Employee_ID=$EE_SSN]/ab:Report_Data/ab:Report_Entry[ab:Employee_ID=$EE_SSN]ab:dependents/ab:Dependent_SSN">
</xsl:for-each>
The Error returned is :
"Engine name: Saxon-PE 9.3.0.5
Severity: fatal
Description: Unexpected token name "wd:dependents" beyond end of expression"
I know I need to test the higher level SSN against looping through the dependents, but like I said "I'm against my wall" :)
Data is here:
<ab:Report_Entry>
<ab:SSN>888881006</ab:SSN>
<ab:Last_Name>Smith</ab:Last_Name>
<ab:First_Name>Kimberly</ab:First_Name>
<ab:dependents>
<ab:Dependent_SSN>888881009</ab:Dependent_SSN>
<ab:Relation ab:Descriptor="Spouse">
</ab:Relation>
</ab:dependents>
<ab:dependents>
<ab:Dependent_SSN>888881004</ab:Dependent_SSN>
<ab:Relation ab:Descriptor="Child">
</ab:Relation>
</ab:dependents>
<ab:dependents>
<ab:Dependent_SSN>888881003</ab:Dependent_SSN>
<ab:Relation ab:Descriptor="Child">
<ab:ID ab:type="Related_Person_Relationship_ID">Child</ab:ID>
</ab:Relation>
</ab:dependents>
<ab:dependents>
<ab:Dependent_SSN>888881001</ab:Dependent_SSN>
<ab:Dependent_ID>1032D-4</ab:Dependent_ID>
<ab:Relation ab:Descriptor="Child">
<ab:ID ab:type="Related_Person_Relationship_ID">Child</ab:ID>
</ab:Relation>
</ab:dependents>
</ab:Report_Entry>
Thank you to any advice!
You might want to define the type of the input parameter and the type of the function result and then you should write a function body returning a value of that type. Currently your description sounds rather procedural, that is not going to work with XSLT/XPath.
As for the error, I think in the path /ab:Report_Data/ab:Report_Entry[ab:Employee_ID=$EE_SSN]/ab:Report_Data/ab:Report_Entry[ab:Employee_ID=$EE_SSN]ab:dependents/ab:Dependent_SSN you need one more slash /ab:Report_Data/ab:Report_Entry[ab:Employee_ID=$EE_SSN]/ab:Report_Data/ab:Report_Entry[ab:Employee_ID=$EE_SSN]/ab:dependents/ab:Dependent_SSN to have a syntactically correct path. That should avoid the syntax error you get but is not likely to return the result you want.

Python parsing error message functions

The code below was created by me with the help of many SO veterans:
The code takes an entered math expression and splits it into operators and operands for later use. I have created two functions, the parsing function that splits, and the error function. I am having problems with the error function because it won't display my error messages and I feel the function is being ignored when the code runs. An error should print if an expression such as this is entered: 3//3+4,etc. where there are two operators together, or there are more than two operators in the expression overall, but the error messages dont print. My code is below:
def errors():
numExtrapolation,opExtrapolation=parse(expression)
if (len(numExtrapolation) == 3) and (len(opExtrapolation) !=2):
print("Bad1")
if (len(numExtrapolation) ==2) and (len(opExtrapolation) !=1):
print("Bad2")
def parse(expression):
operators= set("*/+-")
opExtrapolate= []
numExtrapolate= []
buff=[]
for i in expression:
if i in operators:
numExtrapolate.append(''.join(buff))
buff= []
opExtrapolate.append(i)
opExtrapolation=opExtrapolate
else:
buff.append(i)
numExtrapolate.append(''.join(buff))
numExtrapolation=numExtrapolate
#just some debugging print statements
print(numExtrapolation)
print("z:", len(opExtrapolation))
return numExtrapolation, opExtrapolation
errors()
Any help would be appreciated. Please don't introduce new code that is any more advanced than the code already here. I am looking for a solution to my problem... not large new code segments. Thanks.
The errors() function is called after parse() returns because it appears inside the body of parse(). Hopefully that is a typo.
For this particular input, numExtrapolate is appended with an empty buffer because there is no operand between / and /. That makes its length 4 and your check for Bad1 fails. So put a check like this
if buff:
numExtrapolate.append(''.join(buff))

Resources