SIlverstripe <%loop AllChildren %> with Skeleton - frameworks

I'm using Skeleton for my 16 columns grid system on Silverstripe.
My code below:
<% loop $AllChildren %>
<div class="three columns"><h5>$Title</h5></div>
<% end_loop %>
produces:
Children1 Children2 Children3 Children4 Children5
Children6 Children7 Children8 Children9 Children10
My question is how do I put a space between the rows so the output will
be like:
Children1 Children2 Children3 Children4 Children5
Children6 Children7 Children8 Children9 Children10

In SS3 you can use $MultipleOf() (api docs) in your templates to output something every nth item, e.g.
<% loop $AllChildren %>
<div class="three columns"><h5>$Title</h5></div>
<% if $MultipleOf(5) %>
<hr class="extra-space">
<% end_if %>
<% end_loop %>

Related

display a message when mongoDB collection is empty

I have a notices section on my website which are stored in my Mongo Database. If the collection is empty, how would i display a message to say something like "No new notices"
I am looping this on my page using EJS my code is below.
<% for (let notice of notices) {%>
<p class="lead"><%= notice.noticeText %>
<% } %>
I guess you can put a condition like so:
<% if (!notices.length) {%>
<p>desired text if no notice exists</p>
<% } else { %>
<% for (let notice of notices) {%>
<p class="lead"><%= notice.noticeText %>
<% } %>
<% } %}

Why isn't Template Toolkit aggregating my counter?

I'm working on a simple Dancer app to log books a person has read, but in my template to show how many books a person has read, I'm stumbling into an error. I'm trying to go through ever row in the table of reading instances and add 1 to a counter if the reader is the same as the listed person.
Here's the code for the template:
<ul class="people">
<% IF people.size %>
<% FOREACH id IN people.keys.nsort %>
<li><h2 style="display: inline;"><% people.$id.name %></h2><br />
Born <% people.$id.birthday %><br />
<% FOREACH reader IN readings.keys.nsort %>
<% count = 0 %>
<% IF readings.$reader.person_id == people.$id.id %>
<% count = count + 1 %>
<% END %>
<% END %>
<% count %>
<% END %>
<% ELSE %>
<li><em>Unbelievable. No people here so far</em>
<% END %>
</ul>
However, when I display it, count is only 1. Does anybody know what I'm doing wrong, or do you need more code?
Thanks.
Looks like you need to pull the count initialization out of the FOREACH reader loop:
<% FOREACH id IN people.keys.nsort %>
<li><h2 style="display: inline;"><% people.$id.name %></h2><br />
Born <% people.$id.birthday %><br />
<% count = 0 %>
<% FOREACH reader IN readings.keys.nsort %>
<% IF readings.$reader.person_id == people.$id.id %>
<% count = count + 1 %>
<% END %>
<% END %>
<% count %>
<% END %>

Variants of <% when writing code inside views in MVC2

I'm currently learning MVC2, and I have seen three variants of the tags that contain the actual code for a View:
<% ... %>
With a = after the %
<%= ... %>
and with a : after the %
<%: ... %>
What is the difference of these three code containers?
And are there any other variants of these?
<% ... %>
is just a block of code
<%: "blah blah" %>
Is Shorthand for
<%= Html.Encode("blah blah") %>
Which is shorthand for
<% Response.Write(Html.Encode("blah blah")) %>
1.<% ... %> just block of code
<%if (Model.HelloWorld != null){%>
Hello World!!
<%} %>
2.<%= ... %> plain text without escaping
<%=Model.HelloWorld %>
3.<%: ... %> text with escaping equal <%= Server.HtmlEncode(Model.Something) %> Details here
<%:Model.HelloWorld %>

Rails 3 Edit Multiple Records in a Single Form

I've been stuck on this problem for a couple of days now.
I've have some success with Railscasts Episode #198, but that one is for Rails 2. There have been some changes in Rails 3 that make it so the code provided in Episode #198 won't work.
The problem lies within the edit_individual.html.erb:
Original Code (provided by Ryan # Railscasts):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in #products %>
<% fields_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
Modified Code (simply changed fields_for to form_for):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in #products %>
<% form_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
In the new code, each record is placed within a form of their own, all inside one single form (which is the one I only want).
My question is, how can I get the code provided by Railscasts Episode #198 to work in Rails 3?
Here is a link to the Railscast I mentioned:
http://railscasts.com/episodes/198-edit-multiple-individually
Thank You,
c.allen.rosario
I found the solution. Just need to modify the following line in the code provided by Ryan # Railscasts:
<% fields_for "products[]", product do |f| %>
and change it to:
<%= fields_for "products[]", product do |f| %>
Notice, that the <% has been modified to <%=.
final solution:
<% form_tag update_individual_products_path :method => :put do %>
<% for product in #products %>
<%= fields_for "products[]", product do |f| %>
<h2><%= h product.name %></h2>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
I was wondering if anyone could explain this solution to me. From what I understand you should only need a <% in front of the fields_for.
c.allen.rosario
The change in Rails 3 from <% fields_for to <%= fields_for is because it was confusing that form_for, form_tag, etc... were using <% form... %> even though they they were outputting html code.
With Rails 3, since they output html code, they use <%=.
Please note that your first line is deprecated:
<% form_tag update_individual_products_path, :method => :put do %>
should be
<%= form_tag update_individual_products_path, :method => :put do %>
Same for all form tags.

rails 3 form_for doesn't output anything

Hi I have any form with nested form, for example
<% form_for :main do |f| %>
trying to insert code here
<% fields_for :nested do |nested_form| %>
<%= nested_form.text_field :description %>
<% end %>
<% end %>
And then I am trying to insert anything to a main form, nested form doesn't produce any output. It outputs only when it is the only object in main form. Any suggestions?
From the Rails 3 documentation examples you need to write your form_for like this:
<%= form_for :main do |f| %>
# trying to insert code here
<%= fields_for :nested do |nested_form| %>
<%= nested_form.text_field :description %>
<% end %>
<% end %>
Note the <%= for both form_for and fields_for