.Net : Parse html containing variables - html

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>

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 %>

If statement in underscore to check a condition?

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 %>
<% } %>

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

converting html from external site to excel in ruby (rails)?

I am trying to retrieve data/tables from this site: http://haydarov.net/mest.htm
and convert it into excel file and make it downloadable from my site.
tried nokogiri, writeexcel and some other libraries. I was able to download it as .cvs file with code below.
doc = Nokogiri::HTML(open("http://haydarov.net/mest.htm"))
<% csv = CSV.open("output#{Date.today}.csv", 'w') %>
<% #doc.xpath('//td[#class = "base"]').each do |row| %>
<% tarray = [] #temporary array %>
<% row.xpath('//td[#class = "base"]').each do |cell| %>
<% tarray << cell.text #Build array of that row of data. %>
<% end %>
<% csv << tarray #Write that row out to csv file %>
<% end %>
<% csv.close %>
what i need is to do kind of thing to export this data as excel file.
I have actually have poor code that might work with editing. from the url below, I do not know how to put it into excel file. I am using writeexcel gem/lib.
<% (open("http://haydarov.net/mest.htm") do |f| %>
<% workbook = WriteExcel.new('simple.xls') %>
<% worksheet = workbook.add_worksheet %>
# Row and column are zero indexed
<% row = 0 %>
<% f.read.each do |line| %>
<% col = 0 %>
<% line.chomp.split("\t").each do |token| %>
<% worksheet.write(row, col, token) %>
<% col += 1 %>
<% end %>
<% row += 1 %>
<% end %>
<% workbook.close %>
<% end %>
thanks for the help..