How can I get Ractive.js to cooperate with Slim templates? - slim-lang

The Ractive tutorial uses this:
<th class='sortable {{ sortColumn === "name" ? "sorted" : "" }}'
on-tap='sort:name'>
Superhero name
</th>
The project I am working on uses Slim. Using html2slim, I am provided with this syntax:
th.sortable.sortColumn.: class=("{{ === \"name\" ? \"sorted\" \"\" }}") on-tap="sort:name"
Superhero name
I don't know if that is valid Slim syntax; I cannot find anything in the Slim documentation to guide me. So I'm lost as to how this should be formatted in Slim, to render properly for Ractive.
The above syntax results in:
syntax error, unexpected tIDENTIFIER, expecting keyword_end
I have searched for gems, SO answers, and broad Googling but cannot find any clues. Has anyone here successfully done something like this?

In slim, use a double equal == to disable escaping in the attribute, see https://github.com/slim-template/slim#quoted-attributes
I believe you can also mix ' and "" to avoid back-slashing, and I think you need to include the data ref in the mustache. The : inline a child element, so you don't want that either. Lastly, I'm not sure you can mix .classname notation with an explicit attribute. So I think it ends up being:
th class=="sortable {{ sortColumn === 'name' ? 'sorted' : '' }}" on-tap="sort:name"
Superhero name

Related

Replace a string in Thymeleaf

My problem is when I use the character ', Thymeleaf converts it to '.
I need to show the apostophes instead.
My string is saved in SQL like this:
"body" : "L'' autorizzazione di EUR [[${ #numbers.formatDecimal(#strings.replace(amount,'','',''.''),1,''POINT'',2, ''COMMA'')}]] in [[${date}]] ore [[${time}]] c/o presso [[${merchant}]] รจ stata negata. [[${ #strings.replace(refuseMessage,'',/'/g)}]]"
I tried string.replace but it doesn't work. Can somebody help me please?
Are you creating HTML? Then ' is correct, and you don't need to replace it.
If you are not creating HTML, then you need to make sure your template resolver is set to an appropriate template mode, for example, TEXT:
templateResolver.setTemplateMode(TemplateMode.TEXT);

Accessing Ruby Hash value using a string

I have a ruby array like below
tomcats = [
'sandbox',
'sandbox_acserver',
'sandbox_vgw'
]
I need to pass the string as a hash index like below
tomcats.each do |tomcat_name|
obi_tomcat '#{tomcat_name}' do
Chef::Log::info("Creating tomcat instance - #{tomcat_name}")
Chef::Log::info("#{node['obi']['tomcat']['sandbox'][:name]}") // works
Chef::Log::info("#{node['obi']['tomcat']['#{tomcat_name}'][:name]}") // doesn't work
end
end
The last log throws an error since the access with #{tomcat_name} is nil. I'm new to ruby. How do I access with key as the tomcat_name ?
In normal code, you'd write:
node['obi']['tomcat'][tomcat_name][:name]
In a string interpolation (useless here, because it's the only thing in the string in this case), it is completely the same:
"#{node['obi']['tomcat'][tomcat_name][:name]}"
#{} only works in double quote, as "#{tomcat_name}".
But you don't need the syntax here, just use [tomcat_name] directly.
When I saw this question, I'm thinking whether ruby placeholder could be put inside other placeholder in string interpolation. And I found that ruby actually support it, and most interesting thing is that you don't need to escape the " inside the string.
Although it is not very useful in this case, it still works if you write as below:
Chef::Log::info("#{node['obi']['tomcat']["#{tomcat_name}"][:name]}")
Below is an simple example of placeholder inside other placeholder:
tomcats = [
'sandbox',
'sandbox_acserver',
'sandbox_vgw'
]
node = {
'sandbox_name' => "sandbox name",
'sandbox_acserver_name' => "sandbox_acserver name",
'sandbox_vgw_name' => "sandbox_vgw name",
}
tomcats.each do | tomcat |
puts "This is tomcat : #{node["#{tomcat}_name"]}"
end

Scala Parser, set reserved words

I am writing a simple proggramming language with scala parser. So far no trouble, but im worrying about the relation function name / variable name against reserved words.
I'va already addded some special functions like "floor" ~ gexp or "top" ~ gexp and i dont want anybody using this language being able to name a function or a variable like them. I have not found yet a way to check this.
in Ruby i would write something like
rule varname
lowerid &{ |id| id[0].is_not_reserved } <VarNameNode>
but i dont know how would i write this in scala
def varName : Parser[StringValue] = lowerid
You can use the ^? operator:
def varName: Parser[StringValue] = lowerid ^? ({
case id if !isReserved(id) => id
}, { id => s"Error: $id is reserved." })

Capybara - assert_selector("tr#1234") doesn't work, but find_by_id(1234) does

What are possible reasons that doing page.find_by_id(id) works, but doing page.assert_selector("tr##{id}") returns aCapybara::ElementNotFound`?
For background, I am using the Poltergeist driver for Capybara.
I have HTML that is structured like so:
<tbody>
<tr id="1234">
<td>Rico Jones</td>
<td>Price Request</td>
</tr>
<tr id="2345">
<td>Rico Jones</td>
<td>Price Request</td>
</tr>
</tbody>
I have confirmed that my HTML is coming out as expected by using the page.driver.debug feature of Poltergeist and looking at the actual HTML generated by the test.
When I put something like this in my tests, I get a Capybara::Poltergeist::InvalidSelector error with the message The browser raised a syntax error while trying to evaluate the selector.
lead = Lead.first
assert_selector "tr##{lead.id}"
I also get an error when doing this:
lead = Lead.first
within "tr##{lead.id}" do
click_on "Price Request"
end
However, using find_by_id works:
lead = Lead.first
find_by_id(lead.id).click_on("Price Request")
Based on my understanding of Capybara, this shouldn't be the case. Am I doing something wrong?
This is because ID's should not begin with numbers, as shown here.
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".").

Looking for guide line about Razor syntax in asp.net mvc

i am learning asp.net mvc just going through online tutorial
1) just see <span>#model.Message</span> and #Html.Raw(model.Message)
suppose if "Hello Word" is stored in Message then "Hello Word" should display if i write statement like
<span>#model.Message</span> but i just could not understand what is the special purpose about #Html.Raw(model.Message).
what #Html.Raw() will render ?
please discuss with few more example to understand the difference well.
2) just see the below two snippet
#if (foo) {
<text>Plain Text</text>
}
#if (foo) {
#:Plain Text is #bar
}
in which version of html the tag called was introduce. is it equivalent to or what ? what is the purpose of
this tag ?
just tell me about this #:Plain Text is #bar
what is the special meaning of #: ?
if our intention is to mixing text with expression then can't we write like Plain Text is #bar
3) <span>ISBN#(isbnNumber)</span>
what it will print ? if 2000 is stored in isbnNumber variable then it may print <span>ISBN2000</span>. am i right ?
so tell me what is the special meaning of #(variable-name) why bracket along with # symbol ?
4) just see
<span>In Razor, you use the
##foo to display the value
of foo</span>
if foo has value called god then what this ##foo will print ?
5 ) see this and guide me about few more syntax given below point wise
a) #(MyClass.MyMethod<AType>())
b)
#{
Func<dynamic, object> b =
#<strong>#item</strong>;
}
#b("Bold this")
c) <div class="#className foo bar"></div>
6) see this
#functions
{
string SayWithFunction(string message)
{
return message;
}
}
#helper SayWithHelper(string message)
{
Text: #message
}
#SayWithFunction("Hello, world!")
#SayWithHelper("Hello, world!")
what they are trying to declare ? function ?
what kind of syntax it is ?
it seems that two function has been declare in two different way ? please explain this points with more sample. thanks
Few More question
7)
#{
Func<dynamic, object> b = #<strong>#item</strong>;
}
<span>This sentence is #b("In Bold").</span>
what the meaning of above line ? is it anonymous delegate?
when some one will call #b("In Bold") then what will happen ?
8)
#{
var items = new[] { "one", "two", "three" };
}
<ul>
#items.List(#<li>#item</li>)
</ul>
tell me something about List() function and from where the item variable come ?
9)
#{
var comics = new[] {
new ComicBook {Title = "Groo", Publisher = "Dark Horse Comics"},
new ComicBook {Title = "Spiderman", Publisher = "Marvel"}
};
}
<table>
#comics.List(
#<tr>
<td>#item.Title</td>
<td>#item.Publisher</td>
</tr>)
</table>
please explain briefly the above code. thanks
1) Any kind of #Variable output makes MVC automatically encode the value. That is to say if foo = "Joe & Dave", then #foo becomes Joe & Dave automatically. To escape this behavior you have #Html.Raw.
2) <text></text> is there to help you when the parser is having trouble. You have to keep in mind Razor goes in and out of HTML/Code using the semantics of the languages. that is to say, it knows it's in HTML using the XML parser, and when it's in C#/VB by its syntax (like braces or Then..End respectively). When you want to stray from this format, you can use <text>. e.g.
<ul>
<li>
#foreach (var item in items) {
#item.Description
<text></li><li></text>
}
</li>
</ul>
Here you're messing with the parser because it no longer conforms to "standard" HTML blocks. The </li> would through razor for a loop, but because it's wrapped in <text></text> it has a more definitive way of knowing where code ends and HTML begins.
3) Yes, the parenthesis are there to help give the parser an explicit definition of what should be executed. Razor makes its best attempt to understand what you're trying to output, but sometimes it's off. The parenthesis solve this. e.g.
#Foo.bar
If you only had #Foo defined as a string, Razor would inevitably try to look for a bar property because it follows C#'s naming convention (this would be a very valid notation in C#, but not our intent). So, to avoid it from continuing on we can use parenthesis:
#(Foo).bar
A notable exception to this is when there is a single trailing period. e.g.
Hello, #name.
The Razor parser realizes nothing valid (in terms of the language) follows, so it just outputs name and a period thereafter.
4) ## is the escape method for razor when you need to actually print #. So, in your example, you'd see #foo on the page in plain text. This is useful when outputting email addresses directly on the page, e.g.
bchristie##contoso.com
Now razor won't look for a contoso.com variable.
5) You're seeing various shortcuts and usages of how you bounce between valid C# code and HTML. Remember that you can go between, and the HTML you're seeing is really just a compiled IHtmlString that is finally output to the buffer.
1.
By default, Razor automatically html-encodes your output values (<div> becomes <div>). #Html.Raw should be used when you explicitly want to output the value as-is without any encoding (very common for outputting JSON strings in the middle of a <script>).
2.
The purpose of <text> and #: is to escape the regular Razor syntax flow and output literal text values. for example:
// i just want to print "Haz It" if some condition is true
#if (Model.HasSomething) { Haz It } // syntax error
#if (Model.HasSomething) { <text>Haz It</text> } // just fine
As of #:, it begins a text literal until the next line-feed (enter), so:
#if (Model.HasSomething) { #:Haz It } // syntax error, no closing '}' encountered
// just fine
#if (Model.HasSomething)
{
#:Haz It
}
3.
By default, if your # is inside a quote/double-quotes (<tr id="row#item.Id"), Razor interprets it as a literal and will not try to parse it as expression (for obvious reasons), but sometimes you do want it to, then you simply write <tr id="row#(item.Id").
4.
The purpose of ## is simply to escape '#'. when you want to output '#' and don't want Razor to interpret is as an expression. then in your case ##foo would print '#foo'.
5.
a. #(MyClass.MyMethod<AType>()) would simply output the return value of the method (using ToString() if necessary).
b. Yes, Razor does let you define some kind of inline functions, but usually you better use Html Helpers / Functions / DisplayTemplates (as follows).
c. See above.
6.
As of Razor Helpers, see http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx

Resources