SilverStripe: Loop If Statement - loops

I'm building a single page portfolio with SilverStripe.
So far I am able to loop through my sites and all $Title[s] and $Content[s] are visible in one <div>. Now I want to attach a certain CSS-class if the current page (which is looped) is named "Contact".
Something like:
//Pseudocode:
<loop start>
if ($Title == 'Contact') <div class="a"></div> else <div class="b"></div>
<loop end>
Does anybody know how to do this?

<% if $Title == "Contact" %>
// Do something ...
<% end_if %>

you could try a custom getter method such as :
function DivClassName() {
return $this->Title == 'Contact'?'a':'b';
}
and use the following in your template.
<div class="$DivClassName"></div>
keeps the logic out of your templates :)

<% if Title = "Contact" %>
<div class="a"></div>
<% else %>
<div class="b"></div>
<% end_if %>

Related

How to mark the simple_form checkbox based on a Array object?

The Rails view has two parameters
list_of_attributes = ["a1", "a2", "a3", "a4"]
user_selections = ["a2", "a4"]
I am able to display the appropriate checkboxes and any associated user selections using the following simple_form definition
<% list_of_attributes.each do |item| %>
<label>
<%= check_box_tag "user_selections[]", item, user_selections.include?(item) %>
<%= item %>
</label>
<% end %>
How can I define the above behavior using simple_form f.input syntax? With the following definition, I am able to display the appropriate checkboxes, but any user selections is not 'checked'.
<%= f.input key, as: :check_boxes, collection: list_of_attributes,
:label => key, class: "form-control" %>
Thanks for your help.
Adding checked attribute got the f.input definition functional.
<%= f.input key, as: :check_boxes,
collection: list_of_attributes,
:label => key,
checked: user_selections,
class: "form-control" %>
<% end %>
Try this. This worked for me using bootstrap CSS. Check line 25 in my github repo here
<div class="form-group">
<div class="row">
<div class="col-sm-offset-3 col-sm-8">
<%= f.collection_check_boxes :your_model_ids, Your_model.all, :id, :list_of_attributes do |cb| %>
<% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text} %>
<% end %>
</div>
</div>
</div>
And then use this in your CSS file for the custom input_checkbox class above
.input_checkbox input {
width: auto !important;
}

How to select options from array with form_tag and checkboxes

I have an array #sources that contains names (strings) of sources. In a view I want to list every source with a checkbox in a remote form. Then in the linked controller action I would like to have an array in the params hash, containing only those sources, that the user has checked before submitting.
I tried doing that manually, like so:
<%= form_tag select_sources_user_wordsearch_path(#user, #wordsearch), {method: :post, remote: true} do %>
<% #sources.each do |source| %>
<div class="form-group">
<div class="checkbox">
<label class="pull-left">
<input type="checkbox" class="pull-right"> <%=source%>
</label>
</div>
</div>
<% end %>
<div class="form-group">
<%= submit_tag "Start Search",id:"search_commit", class: "btn btn-primary" %>
</div>
<% end %>
But when submitting the form, I don't even see a params hash:
Started POST "/users/1/wordsearches/77/select_sources" for 127.0.0.1 at 2015-08-15 16:18:06 +0200
Processing by WordsearchesController#select_sources as JS
Parameters: {"utf8"=>"✓", "commit"=>"Start Search", "user_id"=>"1", "id"=>"77"}
So I was looking around to see what's going on with the form_tag helper and check_box_tag. And in check_box_tag's API page (see comments) I found that it is possible to pass a collection into it and receive the result of the selection in an array in the params hash. Basically, what I described above.
I am wondering how this would be done inside a form_tag for a remote form, with an array (instead of a collection)?
In the form_tag API page I couldn't find anything about how the data is transferred to the controller. In the check_box_tag there is nothing mentioned about how to handle collections.
Can anyone tell me how this is done "the Rails way"?
It has to be easier than I think..
First question: why don't you use the the form_for helper? Could be something like form_for #wordsearch, url: select_sources_user_wordsearch_path(#user, #wordsearch), {method: :post, remote: true} do.
You may also have a look at the collection_check_boxes helper in the API.
Afaik this can only be used with form_for and the corresponding form object.
<%= form_for #wordsearch do |form| %>
<%= form.collection_check_boxes(:source_ids, #sources, :id, :labeling_method ) %>
<% end %>
Assuming #sources is some kind of an object. In your case
<%= form_for #wordsearch do |form| %>
<%= form.collection_check_boxes(:sources, #sources, :to_s, :to_s ) %>
<% end %>
could work as well. (Did not try it!)
Another way would be to just send the values manually like so:
<% #sources.each do |source| -%>
<%= check_box_tag "wordsearch[sources][]", source -%>
<%= source -%>
<% end -%>
Hope this helps.

How do i nest div elements in HTML?

is there a possibility to code a loop in HTML to nest div elements in that way(the child elements are the interesting ones):
<div class="parent">
<div class="child">
<div class="child">
<div class="child">...
I don't know the main language of your project, but a simple solution that comes to mi mind and think it works is doing something like this:
<div class="parent">
<% simple for statement in your main languaje %>
<div class="child">
<% end %>
<% same simple for statement in your main languaje %>
</div>
<% end %>
</div>

Strange Eco Template Behavior

I am using Backbone and Eco templates in my Rails application. My template has the following code:
<% #collection.each (model)-> %>
<% console.log model.get('name') %>
<p><%= model.get('name') %></p>
<p><%= model.get('description') %></p>
<% end %>
For some reason, the HTML is blank. The name and description are not displayed. However, the console.log method outputs the correct data. What am I doing wrong?
Well I figured out the missing character. Apparently, Eco templates require a colon after the arrow:
<% #collection.each (model)->: %>
Not sure why this is the case. It's never mentioned in the readme.

Backbone underscore sum value

I have an app in backbone and I want to know if is possible to sum inside a template value of the object.
For example I have this piece of template in underscore:
<% _.each(room1.combinations, function(room2) { %>
<div>
<div class="tot"><p>TOTAL:<span id="totale_<%= room2[0].attributes.id %>"></span></p>
</div>
<form method="POST" action="">
<% _.each(room2, function(room) { %>
<span><%= room.attributes.price %> EUR</span>
<% }); %>
<input type="button" class="submit-ricerca prenota-bt" name="buy" value="BUY">
</form>
</div>
<% }); %>
I want to put into the span with class total the sum of the price of each element inside it.
Is possible?
Thanks
Yes, it is possible. Just sum the prices (using reduce) and put them there:
<p>TOTAL:<span id="totale_<%= room2[0].attributes.id %>"><%=
_.reduce(room2, function(sum, room){return sum+room.attributes.price;}, 0)
%></span>

Resources