Check the existence of multiple parameters in dust.js - dust.js

I've just started using dust.js. And I've come across a problem.
I need to specify a condition (json parameters existence) in template. Depending on it some inner html should be rendered or not. So according to the description I should use smth like this:
{?param_name}
...
{:else}
{/param_name}
But I need to check a complex condition, so I go for logic helper - {#if..}. And it turns out that I'd like to use path in condition. So.. The result looks:
{#if cond="{myObject.property1} || {myObject.property2} || {myObject.property3}"}
..
{/if}
And the problem is when some property is undefined the evaluation stops on smth like:
(true) || () || ()
producing a syntax error.
Could you please tell what approach should I use to check the existence of multiple params?
Thanks in advance

The suggested way is
{#if cond="('{myObject.property1}'.length} && '{myObject.property1}') || ...
But I find this pretty horrible.
I would write a specific helper to do this. Look at the code for #if - https://github.com/linkedin/dustjs-helpers/blob/master/lib/dust-helpers.js#L196

Related

Include boolean params with g:include are passed as Strings to the included view

I don't know if the following change is an issue or it is intended.
<g:include view="line.gsp" params="['label':'test', 'progress':false]"/>
Then the expression in line.gsp aways evaluates to true, because the type of the 'progress' param is String, not Boolean.
class is: ${params.progress.getClass()}
<g:if test="${params.progress}">
this should not be displayed
</g:if>
Note that the same is applicable for other types, not just Boolean.
I am using grails 3.3.8
This didn't happen in grails 2.5.2.
I didn't find anything about this online so that's why I am asking here. Thanks.
Edit:
As suggested by Daniel, I've tried with grails 3.3.2 also.
I just created an app with grails create-app and modified the existing index.gsp to include line.gsp, as shown in the code above.
Here is a screenshot:
I realize you have already found a workaround to this, but it was bothering me that your code was not working the same way mine was, so I decided to investigate a bit further in case anyone else runs into this issue.
As other people here have mentioned, params.anything will typically return a String value. This is because params are typically encoded as URI parameters (like ?progress=false) and not autoboxed into any other type. (This is perfectly reasonable; there would be no good way for Grails to know what type they should be.) However, it is possible (and occasionally reasonable) to render your view or template from a controller like render view: whatever, model: [your: model, params: params] where you specifically include params in the model. (Perhaps you have a lot of parameters that you do not need to individually recreate in the model.) In this case, the params map will exist as both URI parameters (?progress=false) and page-scoped model entry (params: [progress: Boolean.FALSE]). Page scope takes priority over URI parameters, and so your types will be preserved.
In my testing, I added your code to an existing page where I was already passing params into the model, and so type was preserved for a newly added parameter as well. (Note that once params are in page scope, they're there for included templates or views as well.) Consequently, I saw progress type of Boolean, while in a basic example it would be type String.
TL/DR: either assume params are strings, or explicitly include params in your page-scoped model when rendering.
Yes, this is true. When you are calling params.something, you are actually accessing GrailsParameterMap which is a Map. So your condition actually evaluates as Map->get() which will be Object->toString() and of course it is true cause it's not null and not empty. So the solution will be the following:
<g:if test="${params.getBoolean("progress")}">
this should not be displayed
</g:if>
You can test this as follows
<g:if test="${params.progress && params.progress == 'false'}">
this should not be displayed
</g:if>

Exclude part of a string when testing in rails

How would I take a test statement that tests for two equal strings, such as, if #current_lesson.code == #current_course.answer, and exclude some parts of the answer. So, if I want to make the user write a <p> tag with anything in it, how would I make it correct, as long as they have the <p> tag?
I would use Regexp to check if you have a <p> tag with text inside the string. If you use Rspec, i'd use the matches matcher.
The code would be something like this:
expect(#current_lesson.code).to match(/\<p\>.+\<\/p\>/)
Didnt check the regexp, it is just to prove a point :).
You can use the "include" method for string object:
Example :
if #current_course.answer.include? #current_lesson.code
#Your code logic here
end

Iterate through array using Map and Select method Together in Ruby

I am developing an application where I'm stuck with the following code.
I have an array of links which is holding some links posted by a user in a form.
say for example my array is bunch1 = ["google.com","http://yahoo.com"]
Now, before I store them into database I need to make sure that each link has "http://" added at the beginning because I have 'validate:' logic in my ActiveRecord object.
So my logic is that I will iterate through the array and check if "http://" string segment present before each link in the array. So clearly I have to add "http://" string segment before "google.com" in my array.
So I have written a code like this:
bunch2=bunch1.map { |y| y="http://"+y }
But it creates a bunch2 array like bunch2=["http://google.com","http://http://yahoo.com"]
As you can see it adds an extra "http://" before "http://yahoo.com" .
To solve this problem I modified the above code like this:
bunch2 = bunch1.select { |x| x !~ /http/ }.map { |y| y="http://"+y }
but it's generating a array like
bunch2 = ["http://google.com"] because the regular expression with select method is eliminating the yahoo.com
Can somebody please give me solution for this problem. Thanks in advance...
Why not test in the call to map?
bunch2 = bunch1.map {|y| y !~ /^http/ ? "http://#{y}" : y }
Ok, guys I have found the solution to this problem. So the code don't need a select method at all. It just needs a ternary operator for this. So my one liner code goes like this:-
#bunch2=#bunch1.map { |x| x.match(/http:/) ? x : "http://"+x }
The above code using the match method for matching with regular expression. If it finds a match then the element is unchanged otherwise the "httP://" string is added at the beginning.

jslint - jslint4java - how to skip checking Required Blocks condition in javascript

I must use a condition like this in my Javascript code
if (condition) statement;
but it returns an error
(" Expected '{' and instead saw ' '. ")
while validating using jslint4java. Is there is any way to skip this checking?
I don't think so. The way to find out is to visit jslint.com and see what option you can use, then pass that option to jslint4java. But I've just checked and none of the options appear to do what you want.

Convert Line breaks to html break for all field getters in Symfony project

I am working on a Symfony project and I currently have this:
<?php echo preg_replace('/\n/','<br />', $review->getComments()); ?>
and would very much like to be able to make all getters add html line breaks so i don't have to pepper my code with preg_replace. the $object->getFieldname methods are work automatically so I am looking to extend this somewhere to globally add a new method. What is the best approach here?
Seems like everyone forgot about nl2br() which is a function that does exactly that in PHP.
nl2br($review->getComments());
EDIT: At the time of this writing, everyone else uses preg_replace().
I think the best idea would be to add a getCommentsHtml() method onto your review object, which does something like:
return preg_replace('/\n/','<br />', $this->getComments());
Then you can use $review->getCommentsHtml() to format them using HTML. Also as Charlie mentioned, maybe str_replace would be better to use, as using a regular expression to change \n's into <br />'s may be a little bit of overkill :)
So if you don't want to have your code littered with replaces like this, I think putting a helper method on the classes that you'd like to format nicely would be the best way to go :)
How about:
str_replace("\n",'<br />', $review->getComments());

Resources