If statement in underscore to check a condition? - html

I want to check if this array (member.team_member_designations) being passed through the template is an array or a string. If it's an array, I want it to move forward with the HTML below it, otherwise, if it's a string, to just print the string. How do I write this in the underscore template? Right now, I'm just checking for an array but want to include a check for string and the condition to print it if its a string.
HTML:
<%- _.isArray(member.team_member_designations) %>
<% _.each(buildDesignationDictionary, function(designation) { %>
<p> <%- designation.title %> </p>
<% }); %>

use <% code %> to evaluate javascript.
<% if( _.isArray(member.team_member_designations)){ %>
<% _.each(buildDesignationDictionary, function(designation) { %>
<p> <%- designation.title %> </p>
<% }) %>
<%}else if( _.isString(member.team_member_designations) ){ %>
<%- member.team_member_designations %>
<% } %>

Related

Using percent sign in EJS

I'm having difficulties getting my code to run.
I'm importing a JSON object alkodata, which is parsed from a .xlsx file.
It contains a alkodata.alkoholi-% object.
The problem is that node throws a
"SyntaxError: Unexpected token % in C:\filepath\something.ejs while compiling ejs"
error because of the percent sign in the Json-object.
Does EJS have a escape character to prevent this?
<% include ./partials/header.ejs %>
<h1>Tänne tulee mun about AlkoAppo sivu!</h1>
<h2><%= message %></h2>
<% for(var i = 0; i<10; i++) { %>
<p>nimi: <%= alkodata[i].nimi %> </p>
<p>Hinta: <%= alkodata[i].hinta %>e </p>
<p>Vahvuus: <%= alkodata[i].alkoholi-% %> </p>
<% } %>
<% include ./partials/footer.ejs %>
If your key value has a character like that then I would suggest using the bracket syntax to access your property:
<% include ./partials/header.ejs %>
<h1>Tänne tulee mun about AlkoAppo sivu!</h1>
<h2><%= message %></h2>
<% for(var i = 0; i<10; i++) { %>
<p>nimi: <%= alkodata[i].nimi %> </p>
<p>Hinta: <%= alkodata[i].hinta %>e </p>
<p>Vahvuus: <%= alkodata[i]['alkoholi-%'] %> </p>
<% } %>
<% include ./partials/footer.ejs %>

Import html to rails method (in helper.rb)

I need piece of code for couple of times, and to follow DRY method (don't repeat yourself) i want to escape using this piece and retyping it for 5 times or more. So I want to define method what will contain both ruby and html, and ruby in html and js to, code. But I have problems with realization.
My html.erb look like this:
<% div_count = 1 %>
<div>
<% for post in #posts %>
<div class="post-on-main-page-<%= div_count %>"
id="js-count-<%= div_count_for_js %>">
<script>
$("#js-count-<%= div_count_for_js %>").click(function(){
window.location.href = "<%= post_url(post) %>";
});
</script>
<%= image_tag(post.picture.url) %>
<span><h5><%= post.theme %></h5></span>
</div>
<% end %>
<% div_count += 1 %>
<% if div_count == 4 %>
<% div_count = 1 %>
<% end %>
</div>
And I want create method, I think in application_helper.rb will do with this piece of code. What will look something like that:
def method_name(parameter)
<% div_count = 1 %>
<div>
<% for post in parameter %>
<div class="post-on-main-page-<%= div_count %>"
id="js-count-<%= div_count_for_js %>">
<script>
$("#js-count-<%= div_count_for_js %>").click(function(){
window.location.href = "<%= post_url(post) %>";
});
</script>
<%= image_tag(post.picture.url) %>
<span><h5><%= post.theme %></h5></span>
</div>
<% end %>
<% div_count += 1 %>
<% if div_count == 4 %>
<% div_count = 1 %>
<% end %>
</div>
end
The result what I want for html.erb
<%= method_name(#posts)%>
How can I make this to work?
You can move this code to some html. And when you want to use it, you just call render method.
<%= render 'html_file' %>

.Net : Parse html containing variables

I have this html containing fields, if and foreach with this syntax:
<% if ( obj.DayPeriod == "Morning" ){ %>
<h1>Good Morning.</h1>
<% if ( obj.User.Name == "Eric" ){ %>
<div>Welcome back Eric</div>
<div>You have <%= obj.User.ChildrenNumber %> children.</div>
<% } %>
<% } %>
<% } else { %>
<h1>Good Afternoon.</h1>
<% } %>
It can also contains foreach like this:
<% for each (var c in obj.User.Children) { %>
<tr>... <td>... <%= c.#name %>...
<% } %>
I also have a xml feed with the structure that we can see in the code sample above:
<obj>
<DayPeriod>Morning</DayPeriod>
<User>
<Name>Eric</Name>
<ChildrenNumber>2</ChildrenNumber>
<Children>
....
</ChildrenNumber>
</User>
</obj>
Do you have any idea how I could parse and generate the html replacing the variables by what is contained in the xml?
Precision : I cannot change the format and I am on the .Net platform.
The result here would be (of course, this is a simplified example and data is dynamic):
<h1>Good Morning.</h1>
<div>Welcome back Eric</div>
<div>You have 2 children.</div>

If i pluck 2 values, how can i read them in html ruby

In my controller i find a uniq notebook and user name.
but i want to be able to check in my html code that it shows only one type of user.
in controller
def index
#allnotebooks = Note.uniq.pluck(:string, :notebook)
#notes = Note.all
end
in my html
<% #allnotebooks.each do |notebook| %>
<% if notebook.string == c_user.name %>
<option><%= notebook %></option>
<% end %>
<% end %>
notebook.string does not work. what am i missing
Also you can do in different way other than using pluck, using select you can do it
like-
In controller code-
def index
#allnotebooks = Note.uniq.select([:string, :notebook])
#notes = Note.all
end
in your html
<% #allnotebooks.each do |notebook| %>
<% if notebook.string == c_user.name %>
<option><%= notebook %></option>
<% end %>
<% end %>
Thanks!!!
Pluck returns an array.
Try notebook.first or notebook[0]
Docs here: http://apidock.com/rails/ActiveRecord/Calculations/pluck
Second example at the bottom applies here.
In your example it should be:
<% #allnotebooks.each do |notebook| %>
<% if notebook[0] == c_user.name %>
<option><%= notebook[1] %></option>
<% end %>
<% end %>
btw you might want to improve this by only loading notebooks where the string equals your user's name.
I am not a good ruby guy, You can write it like
<% #allnotebooks.each_with_index do |notebook, index| %>
<%= notebook[index].string %> => <%= notebook[index].notebook %>
<% end %>
for "a" unique notebook, you do you need to loop it?
just to say:
#allnotebooks.each{|notebook| <%= notebook[0] %> <%= notebook[1] %> }
But there can be better ways.

Can I find out if variable is an array using Template Toolkit?

I'm passing the results of a multi-select box to a page so that the selections can be shown on screen. As it's multi-select, the result can either be a scalar or an array reference. Is there a way of finding this out? I can't find anything online, but I thought there might be a .array or .array_ref token that could be used for validation.
I'm using Template Toolkit, Perl and Dancer.
So here is what I've got for a scalar:
<% IF multitext %>
Text: <% multitext %>
<% END %>
What I want is something like...
<% IF multitext %>
<% IF multitext.array_ref %> <!-- whatever works! -->
<% FOREACH text IN multitext %>
Text: <% text %>
<% END %>
<% ELSE %>
Text: <% multitext %>
<% END %>
<% END %>
If <%- multitext.0 -%> returns a non-zero value, it's an arrayref.
If <%- multitext.keys.size -%> returns a non-zero value, it's a hashref.
The way I usually handle it is to force it to be an array if it's a scalar, eg:
<%- SET items = multitext.0 ? multitext : [ multitext ];
FOREACH item IN items;
...
END; -%>
Several years later...
You could use the .list vmethod to guarantee it's an array e.g.
<% FOREACH text IN multitext.list %>
Text: <% text %>
<% END %>
See http://template-toolkit.org/docs/manual/VMethods.html#section_list