Decrement/Subtraction of number in string in EJS - node.js

I have this line in EJS file:
<%= nominations[i].month %>
I want to decrement it in the EJS by 1.
So I tried:
<%= (nominations[i--].month) %>
But this didn't change the output value.
I tried subtraction also but the output still stayed the same.
<%= (nominations[i-1].month) %>
I also tried:
<%= (nominations[i].month -1) %>
Update: I didn't realize the number is in string format, the solution:

Here is solution in case it help someone else in future.
I didn't realize that nominations[i].month is actually string data type.
I manage to subtract the number by 1.
First converted the string to the number
<%= parseInt(nominations[i].month) %>
And then I subtract by 1
<%= parseInt(nominations[i].month-1) %>

Related

Kentico UniPager Pagination absolute url links

UniPager Pagination. Is it possible to put absolute links instead of relative ones in "PreviousGroupURL" and "NextGroupURL"?
My code:
<PreviousPageTemplate>
<a href="<%# URLHelper.GetAbsoluteUrl(Convert.ToString(Eval("PreviousURL", true))) %>" >Previous</a>
</PreviousPageTemplate>
<NextPageTemplate>
Next
</NextPageTemplate>
<PreviousGroupTemplate>
...
</PreviousGroupTemplate>
<NextGroupTemplate>
...
</NextGroupTemplate>
HTML source page:
Previous
1
<span>2</span>
3
<a href="http://local/en/test?strona=3" >Next</a>
</div>
In your transformation use something like:
<%# GetAbsoluteUrl(Eval<string>("NextGroupURL")) %>
From https://devnet.kentico.com/docs/11_0/api/html/Methods_T_CMS_Helpers_URLHelper.htm
Please refer below URLs - -
https://docs.xperience.io/k11/references/kentico-controls/generic-controls/paging-controls/unipager
https://devnet.kentico.com/questions/repeater-and-unipager-in-a-custom-control-(widget)
https://devnet.kentico.com/questions/kentico-unipager-not-working

Comparing two string variables in puppet template

I'm trying to write an if statement that compares two variables in a puppet erb template:
<% #array_of_ip_addresses.each_with_index do |ip, idx| -%>
<% if #ip == #ipaddress_eth0 -%>
<%= "doing something with #{idx}" -%>
<% end -%>
<% end -%>
I cannot figure out why, but the condition on my if statement keeps returning false (needless to say I confirmed there should be a match).
What dumb thing am I missing?
ip is a block scope variable and not a variable instantiated from the invoking code (in this case, Puppet's template function), so you should not specify it as a class instance variable with #. When you remove that and specify it as a block scope variable ip, the template will look like:
<% #array_of_ip_addresses.each_with_index do |ip, idx| -%>
<% if ip == #ipaddress_eth0 -%>
<%= "doing something with #{idx}" -%>
<% end -%>
<% end -%>
Using the class instance variable #ip would likely result in a resolution of nil, which would definitely almost always be false as nil != #ipaddress_eth0 unless Facter failed to resolve a value for your eth0 ipaddress, which would be quite uncommon (but still possible).

Kentico Trim in a transformation with Eval

I'm trying to trim ending white space from AlertTitle in an ascx transforamtion. I know there is TrimEnd, but i'm drawing a blank getting it to work.
The V9 Documentation has a method for this(https://docs.kentico.com/display/K9/Adding+custom+methods+to+transformations) but i don't want to fix the length.
Here's the transformatin code snippet.
<asp:placeholder id="alert" runat="server" Visible="false">
<li data-date="<%# Eval("AlertDate") %>">
<p class="alert-date"><%# FormatDateTime(Eval("AlertDate"), "MMMM dd, yyyy") %> </p>
<p class="alert-copy"><%# Eval("AlertTitle") %> <%# IfEmpty(Eval("AlertCopy"),"", "... <a href='" + GetDocumentUrl() + "'>" + CMS.Helpers.ResHelper.GetString("kff.Generic-ReadMore") + "</a> &raquo") %></p>
</li>
</asp:placeholder>
In addition to using Trim() or TrimEnd() in the transformation, you can also set it up so Kentico will automatically trim the fields when the form is submitted by checking the "Trim" checkbox under "advanced" Editing control settings.
Like so:
You probably need to cast the ouput of Eval to a string first:
<%# ((string)Eval("AlertTitle")).TrimEnd() %>
In v8 and newer, you can also use a different version of Felix's answer
<%# Eval<string>("AlertTitle").TrimEnd() %>

Rails displaying a string with CR and spaces

I have a model called MSG and it has a content:string in it.
In the MSG form I have a
<%= f.text_area :content %>
for the user to input the message.
However, when I display :content using <%= Msg.content %> the text has no "enter"/CR or spaces, tabs etcs and it is just a long line of text.
How do I force displaying the CR that that user put?
example:
content:
Hello,
buy milk.
instead I get
Hello, buy milk.
thanks
You can use simple_format to properly display line breaks:
<%= simple_format(Msg.content) %>

How do you pass an array to an erb template in ruby and have it iterated over?

I need some help with erb templates, I can't seem to get my head around passing an array and then iterating over it.
My problem is this.
I want to pass a few arrays:
`
device => ["eth0", "br0"],
ipaddr => ["192.168.12.166", "192.168.12.199"],
netmask => ["255.255.255.0", "255.255.255.0"],
hwaddr => '',
network => '',
gateway => ["192.168.12.254", "192.168.12.204"],
To a template that iterates over each item in the array and prints it out:
auto <%= device %> inet static
address <%= ipaddr %>
netmask <%= netmask %>
broadcast <%= broadcast %>
gateway <%= gateway %>
As far as I can get so far is figuring out that I need to do something with device.each |device| puts device, but I don't know what the syntax is supposed to look like.
I believe you can tell what I'm trying to do from these snippets, and then you might understand that the entries need to be seperate, and not interpolated.
Any help you can offer would be appreciated. I know I should be trying things out in irb and figuring them out from there, which is what I'm reading up on now.
Thanks!
the basic syntax for using each in ruby is something like this:
array.each do |item_from_array| BLOCK
so if you only had one array then you could just do something like this:
(I would use a different name inside the vertical bars for clarity)
<% device.each do |dev| %>
auto <%= dev %> inet static
<% end %>
However that would iterate over all of your devices first, before moving on to your ipaddr array. I'm guessing you want them each in turn auto, address, netmask, etc. In that case you'd be better off using a more 'traditional' index and looping through N times, like this:
<% for idx in (0..1) %>
auto <%= device[idx] %> inet static
address <%= address[idx] %>
netmask <%= netmask[idx] %>
broadcast <%= broadcast[idx] %>
<% end %>
Of course you need to think about what your maximum size of array is, and what to do if an array contains less entries than the others. You can find the maximum size of all the arrays by doing something like this: [device,address,netmask,broadcast].map{|a| a.length}.max
and you can skip over a particular array like this: <% if idx < address.length %> address <%= address[idx] %><% end %>
ERB Templates for Dummies
Basic code:
require 'erb'
ERB.new(template).result binding_for_template
What are template and binding_for_template?
Template
Just the template content. You can read it from a file just with a File.read(path).
Binding for template
A binding
encapsulate the execution context at some particular place in the code and retain this context for future use.
How do you use it? Easy:
def binding_for_my_template
devices = ["eth0", "br0"]
ipaddrs = ["192.168.12.166", "192.168.12.199"]
netmasks = ["255.255.255.0", "255.255.255.0"]
hwaddrs = ['']
networks = ['']
gateways = ["192.168.12.254", "192.168.12.204"]
binding
end
That will return a binding with all six arrays (I changed hwaddr and network to arrays for consistency.
Iterating over arrays
The usual way is using the each method, just like this:
<%- devices.each do |d| %>
auto <%= d %> inet static
<%- end %>
but there are other methods if you wanna put all in one line, for example devices.join ' ' will join all the strings with a space in between.
You should read the docs. Quoting:
# Managed by Class['ntp']
<% servers_real.each do |server| -%>
server <%= server %>
<% end -%>
# ...
This snippet will iterate over each entry in the array and print it
after a server statement, so, for example, the string generated from
the Debian template will end up with a block like this:
# Managed by Class['ntp']
server 0.debian.pool.ntp.org iburst
server 1.debian.pool.ntp.org iburst
server 2.debian.pool.ntp.org iburst
server 3.debian.pool.ntp.org iburst

Resources