Strange Datamapper's behaviour - sinatra

I have this code in my controller:
#cats = DirCat.all
And this in a view:
%ul#menu
= #cats.each do |item|
%li
= link_to item.title, "/catalog/#{item.id}/"
And get strange output:
<ul id='menu'>
<li>
hello
</li>
<li>
hello 1
</li>
#<DataMapper::Collection:0x85a9d00>
</ul>
In the irb console:
irb(main):002:0> c.each { |item| puts item.title }
hello
hello 1
=> [#<DirCat #id=4 #parent_id=1 #title="hello">, #<DirCat #id=5 #parent_id=1 #title="hello 1">]
How can I get out #<DataMapper::Collection:0x85a9d00> from my html?

In HAML, = is used to display the output of a thing in your markup. #<DataMapper::Collection:0x85a9d00> is the return value of #cats.each do |item|. You want to use -, which executes code (but does not print the return value):
- #cats.each do |item|
%li
= link_to item.title, "/catalog/#{item.id}/"

Related

how do i solve 'Cannot read properties of undefined' in ejs

i am using ejs as the template language in 11ty. in trying to use 11ty collections, i came up with the following ejs code.
---
layout: layouts/base.ejs
title: a list of post
---
<ul>
<% const posts = collections.post;
for (let a_post in posts) { %>
<li> <%- a_post.data.title %> </li>
<% } %>
</ul>
this gives the error:
Cannot read properties of undefined (reading 'title')
the nunjucks code works however,
ul>
{%- for post in collections.post -%}
<li>{{ post.data.title }}</li>
{%- endfor -%}
</ul>
how can i write the above in ejs
i really have no idea of what i am doing wrong
The collections.post is what i would refer to as an objects of objects. The for/in loop iterates over properties of an obeject. there are two ways to address this
Use a for/of loop: this iterates over an iterable object
<ul>
<% const posts = collections.post;
for (let a_post of posts) { %>
<li> <%- a_post.data.title %> </li>
<% } %>
</ul>
Change code to the following
<ul>
<% const posts = collections.post;
for (let a_post in posts) { %>
<li> <%- (posts)[a_post].data.title %> </li>
<% } %>
</ul>

Scala parameter value does not compare correctly - checking for empty string value with if statement in view

I have a view using Scala:
#(user: User = null, scripts: Html = Html(""), isLoggedIn: String = "", currentEmail: String=controllers.helpers.AccessMiddleware.getSessionEmail())(content: Html)
The currentEmail string is what I am interested in. It is empty or "" when a user is not logged into the system and contains an email value when logged in.
I can see that the value is empty or "" when opening the URL/application, but the if statement inside the view continually goes to the else part of the statement, display an empty string and not "Login":
<nav class="navmenu center">
<ul>
<li class="scroll_btn">Home</li>
<li class="scroll_btn">Register</li>
<li class="scroll_btn">Admin</li>
#if(currentEmail == "") {
<li class="scroll_btn">Login</li>
} else {
<li class="scroll_btn">#currentEmail</li>
<li class="scroll_btn">Logout</li>
}
</ul>
</nav>
It always determines that there is a value even though it is empty. Here is the page source:
<nav class="navmenu center">
<ul>
<li class="scroll_btn">Home</li>
<li class="scroll_btn">Register</li>
<li class="scroll_btn">Admin</li>
<li class="scroll_btn"></li>
<li class="scroll_btn">Logout</li>
</ul>
Is there another way to compare to an empty string? Also, how do you make the currentEmail value lower case? .toLowerCase() does not work...
I appreciate the help!
Use Option
Option with forall will handle the case where currentEmail is null
Option(currentEmail).forall(_.isEmpty)
Your code becomes
#if(Option(currentEmail).forall(_.isEmpty)) {
<li class="scroll_btn">Login</li>
} else {
<li class="scroll_btn">#currentEmail</li>
<li class="scroll_btn">Logout</li>
}

HTML attribute in Scala Template

I am a Java developer who recently started to learn about the Play Framework. I have been trying to get the below template working but cant seem to get it. I have got the following in my Scala template
#navItem(label: String, link1: String) = {
#{if (Application.isAuthenticated()) {
<li class="active">
label
</li>
}
else {
<li class="disabled">
{label}
</li>
}
}
}
I am calling this later in my template like so
<ul class="nav">
#navItem("Search Documents", "/search")
</ul>
The generated link has href as localhost:9000/#link1 instead of localhost:9000/search. I am not sure whats going on.
PS: If I change my template as below it works fine. But I want to understand why the above template wont work.
#navItem(label: String, link1: String) = {
<li class="#(if (Application.isAuthenticated()) "active" else "disabled")">
#label
</li>
}
Not quite sure about this, but my guess would be the following: The #{ ... } indicates the beginning of a dynamic statement and all of its content is treated as Scala code. Thus, it is a normal if-condition with two strings as a result, both of which are simply returned in the template.
Why are you marking it as a multi-line code block anyway? Have you tried it like this? (note the missing curly braces after the 2nd # sign):
#navItem(label: String, link1: String) = {
#if(Application.isAuthenticated()) {
<li class="active">
#label
</li>
} else {
<li class="disabled">
#label
</li>
}
}

Parsing HTML In iOS Using TFHpple?

Iam New To iOS .
i Wanna Parse HTML Page Like This :
<div class="main_menu">
<ul class="sf-menu">
<li class="current">Holiday Tours
<ul>
<li>About Us</li>
<li>Mohammed El Decken</li>
<li>Yehia El Decken</li>
</ul>
</li>
</li>
<li>Vacation Packages
<ul>
<li>Slov2Spain</li>
<li>Swiss Alps</li>
<li>Fall Hike</li>
<li>Nile Valley</li>
<li>Sahlala</li>
<li>Safar El Layaly</li>
<li>Cruising Sinai</li>
<li>Siwa Siwa</li>
<li>Silk-Road</li>
<li>French Alps</li>
</ul>
</li>
-> <li>Weekend Trips<ul>
-> <li>Hiking</li>
-> <li>Sandboarding</li>
-> <li>Horse Riding</li>
-> <li>GoCycle</li>
-> <li>Islamic Tour</li>
</ul>
</li>
<li>Reservation </li>
</ul>
</div>
</div>
i wanna Access Weekend trips Where The Arrows And I Cannot Fine XPathQuery To Get Them ?
Any Help Please ?
NSArray *data = [xpath searchWithXPathQuery:#"//li//a//child::text()"];
to search data
for (int i = 0; i < data.count; i++) {
TFHppleElement *element = [data objectAtIndex:i];
NSLog(#"%#", [element content]);
}
to look data inside and you can check how to get you want.
And here is a xPath syntax tutorial page:
http://www.w3schools.com/xpath/xpath_syntax.asp

How to conditionally assign variable value in template of play framework 2.0?

I want to create a variable called style and assign its value
based on the value of the input parameter #filter
I read the play's documentation, but the current solution I can get
it works is something like: which the list template part is duplicated...
#(filter: String = "error")(body: (String) => Html)
#filter match {
case "HOT" => {
<ul class="list">
<li class="icon-hot">this is a list item..</li>
<li class="icon-hot">this is a list item..</li>
</ul>
}
case "NEW" => {
<ul class="list">
<li class="icon-new">this is a list item..</li>
<li class="icon-new">this is a list item..</li>
</ul>
}
}
How do I assign "icon-new" & "icon-hot" to a variable #style and use it latter when writing the list template like this?
<ul class="list">
<li class="#style">this is a list item..</li>
<li class="#style">this is a list item..</li>
</ul>
This seems to work...
in style.scala.html
#(filter: String = "error")(body: (String) => Html)
#filter match {
case "OPEN" => {
#body("icon-plus")
}
case "FOLLOWING" => {
#body("icon-comments")
}
case "HOT" => {
#body("icon-fire")
}
}
And in list template
#(filter: String = "HOT")
#style(filter) { style =>
<ul class="list">
<li class="#style">this is a list item..</li>
<li class="#style">this is a list item..</li>
</ul>
}
but I'm still not very clear how this works...especially the following part:
(body: (String) => Html)
#body("icon-plus")