Chef Template Nested Loops - erb

I have a small problem with my chef cookbook, i'm trying todo a nested loop (loop in loop). I have a array with multiple levels and i would like to loop all levels in a chef template. Right now i'm down at the following:
Template:
<% #countries.each do |country| %>
<% #countries[#country].each do |key, val| %>
<Publication <%= #country #key #val %> />
<% end %>
<% end %>
Attribute:
default['countries']['IE'] = {'57'=>'val2','58'=>'val2','61'=>'val','63'=>'val'}
default['countries']['DE'] = {'110'=>'val2','113'=>'val2','115'=>'val2'}
default['countries']['BE'] = {'134'=>'val2','138'=>'val2','139'=>'val2'}
Recipe:
template "conf.xml" do
action :create
variables ({
:countries => node['countries']
})
end
So first i would like too loop the countries level, then loop each level in that array by using the key and val of the array. I hope someone can help me out with this one.
Best,
- Thijs

You can do something like this,
<% #countries.each do |country, country_details| -%>
<% country_details.to_hash.each do |value| -%>
<Publication <%= "#{country} #{value[0]} #{value[1]}" %> />
<% end -%>
<% end -%>
The country_details should ideally be a hash, however, Chef changes that to something called an ImmutableMash which needs to be converted to a Hash.
However, on conversion the value received after iteration is an array instead of a hash so needs to be written in the array format i.e. value[0] and value[1].
The above code returns the following output, not sure if you're looking for this,
<Publication IE 57 val2 />
<Publication IE 58 val2 />
<Publication IE 61 val />
<Publication IE 63 val />
<Publication DE 110 val2 />
<Publication DE 113 val2 />
<Publication DE 115 val2 />
<Publication BE 134 val2 />
<Publication BE 138 val2 />
<Publication BE 139 val2 />

Related

Scraping multiple similar lines with python

Using a simple request I'm trying to get from this html page some information stored in "alt". The problem is that, within each instance, the information is separated in multiple lines that start with "img", and when I try to access it, I can only read the first instance of "img" and not the rest, but I'm not sure how to do it. Here's the HTML text:
<div class="archetype-tile-description-wrapper">
<div class="archetype-tile-description">
<h2>
<span class="deck-price-online">
Golgari Midrange
</span>
<span class="deck-price-paper">
Golgari Midrange
</span>
</h2>
<div class="manacost-container">
<span class="manacost">
<img alt="b" class="common-manaCost-manaSymbol sprite-mana_symbols_b" src="//assets1.mtggoldfish.com/assets/s-d69cbc552cfe8de4931deb191dd349a881ff4448ed3251571e0bacd0257519b1.gif" />
<img alt="g" class="common-manaCost-manaSymbol sprite-mana_symbols_g" src="//assets1.mtggoldfish.com/assets/s-d69cbc552cfe8de4931deb191dd349a881ff4448ed3251571e0bacd0257519b1.gif" />
</span>
</div>
<ul>
<li>Jadelight Ranger</li>
<li>Merfolk Branchwalker</li>
<li>Vraska's Contempt</li>
</ul>
</div>
</div>
Having said that, what I'm looking to get from this is both "b" and "g" and store them in a single variable.
You can probably grab those <img> elements with the class "common-manaCost-manaSymbol" like this:
imgs = soup.find_all("img",{"class":"common-manaCost-manaSymbol"})
and then you can iterate over each <img> and grab the alt property of it.
alts = []
for i in imgs:
alts.append(i['alt'])
or with a list comprehension
alts = [i['alt'] for i in imgs]

kentico ascx transformation, every nth

Here's my transformation:
<%# IsFirst() ? "<div class='row'>" : "" %>
<div class='col-xs-12 col-md-3 col-bdlt'>
<div class='bdlt lt'>
<asp:PlaceHolder runat="server" Visible='<%# IfEmpty( Eval("Headshot"), false, true ) %>'>
<img src="<%# Eval("Headshot") %>" alt="<%# Eval("Name") %>" class='img-responsive'>
</asp:PlaceHolder>
<p class='name'><%# Eval("Name") %><%# IfEmpty( Eval("Accolades"),"</p>",",</p> <p class='accolades'>" + Eval("Accolades") + "</p>" ) %>
<p><%# Eval("Copy") %></p>
</div>
</div>
<%# IsLast() ? "</div>" : "" %>
I'm running Bootstrap, so i'm wrapping these elements in a row. Things are working, but depending on the amount of copy, it's not looking as it should. What i'd like do is have a row for each group of 4.
So like this
Row
item
item
item
item
Row
item
etc.
So after every 4th, close the row div, and start a new one. I think my first line is good, it's the last where i think the logic is needed.
To start the row, you want to make sure the first record starts a row:
<%# ( (DataItemIndex % 4 == 0) ? "<div class=\"row\">" : "" ) %> <!-- Start Row -->
To close the row, you want to make sure the last record ends the current row.
<%# (DataItemIndex % 4 == 3 || DataItemIndex == DataRowView.DataView.Count - 1 ? "</div>" : "") %> <!-- Close Row -->
You want to utilize the DataItemIndex value, and do something like this, coupled with the Modulo
You compare the DataItemIndex %4 = 3 because DataItemIndex is 0 based, so the 4th, 8th, 12th rows have index of 3, 7, 11 which modulo 4 are all 3.

Scriplets in Liferay

What can I do with this scriptlet
<%
ResultRow row = (ResultRow) request.getAttribute(BookAndAuthor.SEARCH_CONTAINER_RESULT_ROW);
Book book = (Book) row.getObject();
%>
If I want to use value="${book.BookId} in this code?
<liferay-ui:icon-menu>
<portlet:actionURL name="deleteBook" var="deleteBookVar">
<portlet:param name="bookId" value="<%=String.valueOf(book.getBookId())%>" />
</portlet:actionURL>
</liferay-ui:icon-menu>
This is a little tricky but I thing it should work.
Use a variable to hold the value of the constant SEARCH_CONTAINER‌​_RESULT‌​_ROW
<c:set var="resultRowConstant" value="<%=BookAndAuthor.SEARCH_CONTAINER‌​_RESULT‌​_ROW%>" />
than to read the attribute you you should be able to use bracket notation to access 'dynamic' properties:
<c:set var="row" value="${[resultRowConstant]}"/> and that will hold the ResultRow object, than you access your book object through
${row.object}

How can I delay the closing tag in a Jade template?

I am having an issue with Jade and Bootstrap. I am trying to layout some HTML in this format:
<bootstrap row>
<bootstrap col-sm-6>
<bootstrap col-sm-6>
</bootstrap row>
<bootstrap row>
<bootstrap col-sm-6>
<bootstrap col-sm-6>
</bootstrap row>
etc...
I need this to happen inside an each statement however I cannot get it to work quite how I'd like it. It keeps closing the row after each iteration so I get:
<bootstrap row>
<bootstrap col-sm-6>
</bootstrap row>
<bootstrap row>
<bootstrap col-sm-6>
</bootstrap row>
This is the Jade template I currently have:
div.col-sm-12
#items
- each item, x in items
div.article.col-sm-6
div.title
h3= item.name + " (" + item.cost + ")"
p= item.stats
p= item.recipe
p= item.ability
p= "Purchased from " + item.category
So, for each 2 iterations, I need to wrap the two col-sm-6 items in a row.
Any help would be appreciated.
Thank you.
One way to do it is to only render item when the index is even, and within that condition render the next item. Something like:
.col-sm-12
#items
- var items = ['a', 'b', 'c', 'd']
- each item, index in items
if 0 === index % 2
.row
- var nextItem = items[index + 1]
.col-sm-6= item
.col-sm-6= nextItem
The output:
<div class="col-sm-12">
<div id="items">
<div class="row">
<div class="col-sm-6">a</div>
<div class="col-sm-6">b</div>
</div>
<div class="row">
<div class="col-sm-6">c</div>
<div class="col-sm-6">d</div>
</div>
</div>
</div>
Of course, since your item and nextItem isn't as simple as this example you may need to use a partial to render each with the appropriate markup for its values.

Include new line character in value attribute of Liferay search container

I have the following line of code of search container.
I want to include a new line between the two values that I want to display..
<liferay-ui:search-container-column-text name='Employee Name'
value='<%=String.valueOf(search.getEmpFname()) + String.valueOf(search.getEmpLname()) +"\n" + String.valueOf(search.getEmpTitle()) %>'
href="" >
The reason I want it this way is that I want all these values in one box each row.
So how should I format the above code so that I have:
String.valueOf(search.getEmpFname()) + String.valueOf(search.getEmpLname())
on one line and
String.valueOf(search.getEmpTitle())
on the next line of the same row.
Converting my comment as an answer:
You can try using <br> tag instead of "\n" like this:
String.valueOf(search.getEmpLname()) + "<br>" + String.valueOf(search.getEmpTitle()`
or you can use <liferay-ui:search-container-column-jsp tag instead of <liferay-ui:search-container-column-text tag
or else use the tag as following:
<liferay-ui:search-container-column-text name='Employee Name' href="">
<%=String.valueOf(search.getEmpFname()) + String.valueOf(search.getEmpLname()) %>
<br>
<%= String.valueOf(search.getEmpTitle()) %>
</liferay-ui:search-container-column-text>*emphasized text**emphasized text*

Resources