Silverstripe loop - templates

<%loop $Video.Limit(4) %><% end_loop %>
<%loop $Video.Limit(5,9) %><% end_loop %>
Two divs are being used to display the data,the above code is not working.
Any help is accepted.

as #Zauberfisch stated here you need to use limit(4) (note the lowercase) in your template.
Another problem is the missing space between <% and loop.
Assuming $Video is some kind of SS_List (ArrayList or DataList), you could try
//show the first 4 videos
<% loop $Video.limit(4) %>$Title <% end_loop %>
//gets the next 9 videos, offset is 5
<% loop $Video.limit(9,5) %>$Title <% end_loop %>
See API docs for limit

Related

how to iterate over all strings in a single record

I have a model called train that has 240 strings in it. Each string is simply named S1 through S240. I am wondering if there is a way to iterate over each string in a single record. I have a view that displays the strings. Some of the strings will have something in them and some will be empty and I just want to display the ones that are not empty in view. So I need an iterator or something to check each one and see if it is empty or not and display it if it's not empty.
I can do it using this:
<% if #train.s1 != nil then %>
<%= #train.s1 %><br>
<% end %>
Obviously I don't want to do that for 240 strings.
You probably can do something like iterating over the model's attributes.
<% #train.attributes.each do |attr_name, attr_value| %>
<% !if attr_value.blank? %>
<%= attr_value %>
<% end %>
<% end %>
This won't really work for you if you have more attributes than just those strings. Why don't you work with an association? Train has_many Strings and String belongs_to Train. So you could then iterate over #train.strings.each?
well in that case you can do like
<% (1..240).each do |index| %>
<% if #train.try("s#{index}") != nil then %>
<%= #train.try("s#{index}") %><br>
<% end %>
<% end %>

Rails 4, Draper: authenticated user and views

<% if user_signed_in? %>
<!-- lots of html/erb -->
<% end %>
This view pattern seems to not separate concerns.
I wrap several views in my app with logic demanding the user is signed in and would instead like to separate concerns and put the <% if user_signed_in? %> logic where it belongs...this seems like a decorator thing to me (hence the Draper tag).
What is best practice here?
Not sure understood your question, but try to answer.
At first to separate logic you dont need to use decorators in front of all, they serves for a little another thing.
To separate logic you can use simple partials depending on current user state, for ex:
<% if user_signed_in? %>
<%= render 'file_with_html_for_signed_user' %>
<% else %>
<%= render 'file_with_html_for_non_signed_user' %>
<% end %>
You can declare this statement in your layouts/application.html.erb

SilverStripe loop through hidden pages

How do I iterate only through all hidden pages in SilverStripe?
$Children excludes hidden pages. $AllChildren includes all pages. Is there a way to include only hidden pages?
We can write a function to return only the hidden children like this:
public function HiddenChildren() {
return $this->AllChildren()->filter('ShowInMenus', false);
}
Then in our template we can loop through the hidden children like this:
<% loop $HiddenChildren %>
$Title
<% end_loop %>
Well, a simple if in the template might do the trick:
<% loop $AllChildren %>
<% if not $ShowInMenus %>
$Title
<% end_if %>
<% end_loop %>
See https://docs.silverstripe.org/en/3.1/developer_guides/templates/syntax/#negation

Rails .each enumerator with a special case for the first line

I'm trying to put an enumerator to run over the top of a (refills/bourbon) front end framework. The element is a tab accordian which has repeatable elements but a special is-active class tag which flags to javascript which one should be expanded and the focus of a users attention.
I want to design an enumerator to create tabs and fill content based on how many records there are in ActiveRecord so I'm using an enumerator - but is there a smart way I can create a clean exception for the first <a> it outputs so it includes the is-active tag?
Currently my murky hack is below:
<div class="vertical-tabs-container">
<div class="vertical-tabs">
<% tab_num = 0 %>
<% #user.items.each do |item| %>
<% tab_num += 1 %>
<% if tab_num = 1 %>
<%= content_tag( :a, #user.item, :class=>"js-vertical-tab vertical-tab is-active, :rel="tab#{tab_num}", :href="javascript:void(0) %>
<% else %>
<%= content_tag( :a, #user.item, :class=>"js-vertical-tab vertical-tab, :rel="tab#{tab_num}", :href="javascript:void(0) %>
<% end %>
</div>
... content goes here...
</div>
There seems something deeply un-rails about that tab_num bit... and I sense a disturbance in the MVC force for putting this in my view... Plus this way I'd have to have another enumerator for the body element of the vertical-tabs-container...
Anyone got a better idea of how to do this neatly?
Firstly, this looks like it belongs in a helper or a presenter/decorator.
The code itself could be simplified. Helper method:
In your view:
<div class="vertical-tabs-container">
<div class="vertical-tabs">
<%= user_tabs(#user) %>
</div>
... content goes here...
</div>
Then in your helper:
def user_tabs(user)
output = ""
user.items.each_with_index do |item,i|
output << content_tag(:a, item, :class=>"js-vertical-tab vertical-tab #{i == 0 ? '' : 'is-active'", :rel="tab#{i}", :href="javascript:void(0)
end
output
end
However if you're going to start adding content and tabs seperately, take a look at draper and define methods for tab and body on the item element.

Adding a Class to a link_to Helper With Only 1 Parameter

I have the following:
<% #users.each do |user| %>
<% if user.profile %>
<%= link_to user do %>
<h2><%= user.profile.first_name %> <%= user.profile.last_name %></h2>
<% end %>
<% end %>
<% end %>
The above code works fine. What this code does is that it will output the first and last names of every user. These names are clickable and will take me to that user's page. My main issue is with the 3rd line. The issue I am having is that I am trying to get rid of the link underline, but I am unsure as to how to pass a class into it. Below is my attempt. My class "no-text-dec" is just one line of "text-decoration: none;"
<%= link_to (user, class: "no-text-dec") do %>
I'm new to Rails, but I understand that link_to has a body, url options, and then html options in that specific order, but how can I make it work in this case? The above line makes my application is crash, but it's the only thing I can think of that makes sense. I'm assuming it's because I am not giving it its body argument, but I'm not sure what that would be.
This should work fine if user contains url/path correct
<%= link_to(user, class: 'some_class') %> do
<span>Delete</span>
<% end %>
The space after method in sending argument in helper method link_to is crashing your application
you can give a try at irb
def test(a,b)
puts a; puts b;
end
test ("Ad","Cd")
It should throw an error