Inline if statement in grails - grails

Is there a way in Grails to do conditionals inline on an HTML attribute, for example:
<div class="${if(sec.isLoggedIn()) loggedInClass}">
I'm trying to add a class to certain elements if the user is logged in.

This might work for you:
<div class="${(sec.isLoggedIn()?loggedInClass:null)}">
Or Try this:
<div class="${(sec.isLoggedIn()?'loggedInClass':'null')}">

You can do:
<div class="${sec.loggedIn ? 'loggedInClass' : ''}">

Related

<g:set> variable's value is not being rendered properly

I've used <g:set> tag like this:
<g:set var="extraStyle" value="style='min-width:120px;'"/>
and used the extraStyle variable like this:
<div class="myClass" ${extraStyle}> ${myValue}</div>
And it should be rendered as:
<div class="myClass" style="min-width:120px;"> XYZ </div>
But, I am getting this instead:
<div class="myClass" style="'min-width:120px;'"> XYZ </div>
Due to which, min-width style is not being applied. What am I doing wrong here?
Grails version: 3.1.6
You could try just setting the style value e.g.
<g:set var="extraStyle" value="min-width:120px;"/>
<div class="myClass" style="${extraStyle}"> ${myValue}</div>
I think Mike's answer is correct, and although I do not know the context of your project I think it might in the long run be better to add a class dynamically to the element.
Something like
<div class="myClass ${extraClass}">...</div>

Angular 2 Dart: Template syntax - how to concatenate strings?

Feels like a dumb question but I do not get it. How can I do fast string concatenation in Angular 2 Dart templates?
I have a seperate html file for my component lets say my_component.html:
Works:
....
<div id="abc">
{{order.pickupPlace.email}}
</div>
...
Works:
....
<div id="abc">
{{ ((order.pickupPlace.state) ? order.pickupPlace.state+" ":'')}}
</div>
...
Does not work:
....
<div id="abc">
{{"<br/>"+order.pickupPlace.email}}
</div>
...
Does not work:
....
<div id="abc">
{{order.pickupPlace.name+" "+order.pickupPlace.email}}
</div>
...
Have tried to find an answer in the docs here (https://webdev.dartlang.org/angular/guide/template-syntax#!#expression-operators) but no luck.
Of course I could use *ngIf on every element which I output conditionally but is there a way for simple string concatenation?
The best way is to declare a getter inside your Component controller that does the concatenation for you, you will get dart syntax support and the html template will looks cleaner.
String get myConcatenation => "${order.pickupPlace.name}${order.pickupPlace.email}";
<div id="abc">
{{myConcatenation}}
</div>
The last two examples can be made to work easily:
<div id="abc">
<br/>{{order.pickupPlace.email}}
</div>
And:
<div id="abc">
{{order.pickupPlace.name}} {{order.pickupPlace.email}}
</div>
Angular handles this pretty well. If you need some more complicated logic you can move it to the Dart code (but you cannot use HTML there easily).
If you find creating lot of weird logic consider creating more smaller components that can handle this for you.

ASP.NET MVC Razor conditional rendering an element

How does one conditionally render an HTML element in Razor 2?
For instance, suppose I had the tag
<div class="someclass">
<p>#somevalue</p>
</div>
And I wanted to suppress the <-div-> tag from rendering if the value of #somevalue was equal to 1. Is there a simple way to do this in Razor similar to how I might "hide" the <-div-> tag with Knockout.js in a browser, where I might :
<div class="someclass" data-bind="showWhenTrue: someValue != 1">
<p data-bind="text: someValue"></p>
</div>
At the moment, the best Razor alternative I have is to do this:
#if (someValue != 1) {
<div class="someclass">
<p>#somevalue</p>
</div>
}
There are many ways to do this. First, it should be noted that your knockout code doesn't actually remove the html from output, it just sets its display to hidden.
The razor code you have actually removes the code from the rendered HTML, so that's a very different thing.
To answer your question, we need to know what it is you're trying to achieve. If you just want to hide the display, you can simply do something like this:
<div class="someclass" style="display: #{ somevalue == 1 ? #:"none" : #:"block" };">
<p>#somevalue</p>
</div>
You could also do it with a class:
<div class="someclass #{ somevalue == 1 ? #:"HideMe" : #:"ShowMe" }">
<p>#somevalue</p>
</div>
If you want to remove the code from the output, then you can just do what you've done.. i'm mot sure what you find so objectionable about it... but if you want other alternatives, you could create an Html helper, you could use a razor helper, you could use a Display or EditorTemplate....
The list is actually quite long and i'm just scratching the surface...
An elegant (and re-usable) solution is to write an extension method for Html to do conditional rendering of text ( a bit like IF() in Excel) - e.g.
public static MvcHtmlString ConditionalRender(this HtmlHelper helper, bool condition, string trueString, string falseString = "")
{
return MvcHtmlString.Create((condition) ? trueString : falseString);
}
You can then use it in your code as such:
<div class="someclass" style="display: #Html.ConditionalRender(somevalue == 1, "none","block")">
<p>#somevalue</p>
</div>

Div's visibility based on model property in mvc

The following code not working. please help me to figure out this
<div style="visibility: #(#Model.HasBackUp ? "block" : "none")"/>
You have to remove the # in front of the Model so your code should look like this:
<div style="display: #(Model.HasBackUp ? "block" : "none")"/>
<div style="display: #(Model.HasBackUp ? "block" : "none")"/>

update html content in all duplicate id's Jquery

I am trying to update the HTML content inside div using jquery HTML function.
<div id="test"></div>
.
.
.
<div id = "test" style="display:none"></div>'
When I am updating the content.
(#test).html(result)
I need to update both the div .How can I achieve this.
Thanks
ID must be unique, you should use class instead
<div class="test" id="test1"></div>
<div class="test" id="test2"></div>
$('div.test')
.html('<p>All new content.You want!</p>');
Id must be unique in HTML, you should use class instead
HTML:
<div class="test"></div>
.
.
.
<div class= "test" style="display:none"></div>'
JavaScript:
$(".test").html(result)
Use class instead of ids and try this
$('.test').html(result)
Try this
$('div').html(result);
or
Instead of id you can use class.
$("div.class_name").html(res);
I assume your html code are:
<div id="#test">xxxx</div><div id="test">cccc</div>
then if you call $("#test"); these will return array with 2 length; so, you had to call each array to update all. I asumme you will update with value 'ddd' to both of #test, then
for(i=0;i<$('#test').length;i++){
$('#test')[i].html('ddd');
}

Resources