Sinatra app using HAML not preserving indentation - sinatra

I am developing a basic Sinatra app. I implemented all the views using HAML, but have noticed that the output does not preserve the indentation.
For example, the following:
%html
%head
%script{ :src => '/main.js' }
is rendered in the browser as
<html>
<head>
<script src='/main.js'></script>
</head>
</html>
It's been a while since I used HAML for anything, but my understanding is that preserving indentation is one of the points of HAML as a tool. So this result strikes me as particularly unusual.
How do I fix this?
Ruby 2.7.1p8, HAML 5.2.0, Sinatra 2.1.0.

Related

How to prevent Perl CGI->start_html() from printing DOCTYPE header

when using Perl CGI, it always print out the: <"DOCTYPE html ".... > header before printing out the rest of the html body, which messes up the proper display of the web page. I guess the cause is the DOCTYPE still references a very old version of HTML. This first line get generated regardless when I use $query->start_html(...);. So the question is, how to prevent that <!DOCTYPE...> line from being generated? When I delete that DOCTYPE line, my web page runs properly after that. Any insight is greatly appreciated. Thanks!
Browsers will use the DOCTYPE definition to decide whether or not to interpret an HTML (or XHTML) document in Quirks mode. I assume that's what you mean by "messes up the proper display of the web page" - you're carefully creating valid HTML5 but the browser goes into Quirks mode and the page doesn't look right.
What you want is the standard HTML DOCTYPE definition.
<!DOCTYPE html>
Unfortunately for you, the HTML generation functions in CGI.pm are unmaintained and don't have support for HTML5. So you can't get the module to generate this DOCTYPE.
Honestly, your best approach is to stop using all of the HTML generation functions and switch to a template-based approach instead. But I can see how that would be a big job so, in the short term, perhaps you can just stop using start_html(). It shouldn't be too hard to replace the call with a heredoc that contains the text that you want.
Update: The comment from Quentin below got me thinking. When I said:
you're carefully creating valid HTML5 but the browser goes into Quirks mode and the page doesn't look right
I was completely wrong. If you get the browser behaviour you want without a DOCTYPE, then you want to put the browser into Quirks mode. And that's far easier. Using the information from the Wikipedia article I linked to above, you can see that HTML 4.01 Transitional is the most recent DOCTYPE that will put all modern browsers into Quirks mode. So you can use that DOCTYPE.
$ perl -MCGI=:html -E'say start_html(-dtd => "-//W3C//DTD HTML 4.01 Transitional//EN")'
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Untitled Document</title>
</head>
<body>
And that should get the browsers working as you want them.
But relying on Quirks mode is fragile. I recommend that you consider fixing your HTML and CSS so that they work as you want when browsers are in Standards mode.

HTML reuse and maintenance with Play! framework

We have been having discussions in our product dev team regarding html maintainability and reuse. To set the context, we started with HTML5/CSS3 front end with plain JS under Play MVC, which in turn uses RESTful backend. Then we thought of adding AngularJS to the spin and to adopting a hybrid approach only to realize that two strong MVC frameworks don't necessarily work together and you have to pick one. So for the performance and type-safety among other issues, we decided to go with using Play framework and Scala based templates.
Here's the challenge: We would like to create reusable web components just like Apache Tiles, so that common elements such as header, menus, footer, etc. can be reused. These components are ready to go in Play to which dynamic content could be added to serve the entire page.
Can this be done? If yes, how?
Secondly, play templates seem to take you back in the time since they don't allow the separation of concern in html. Therefore for re-designing or improving html content, the html developer will have to deal with the template or merging new html with existing templates. How to make this process easier?
I'm don't know exactly how Apache Tiles works, but if I properly understand, it offers a way to create pages using smaller components (like header, menu, footer, etc) and some sort of include mechanism to glue these components together and then compose the page.
That said, you can achieve the same thing using Twirl. You just need to declare reusable blocks that can be used inside the same page, or you can have something like Rails partials that can be reused across different pages.
Let's see an example. Consider that you have the following files:
File app/views/partials/header.scala.html:
<header>
<h1>My Header</h1>
</header>
File app/views/partials/navigation.scala.html:
<nav>
<ul>
<li>Home</li>
<li>Profile</li>
<li>FAQ</li>
</ul>
</nav>
File app/views/partials/footer.scala.html:
<footer>
Some copyright info
</footer>
File app/views/main.scala.html:
#(title: String)(content: Html)
<!DOCTYPE html>
<html lang="en">
<head>
<title>#title</title>
<link rel="stylesheet" media="screen" href="#routes.Assets.versioned("stylesheets/main.css")">
<script src="#routes.Assets.versioned("javascripts/hello.js")" type="text/javascript"></script>
</head>
<body>
#partials.header()
#partials.navigation()
#content
#partials.footer()
</body>
</html>
The files above defines not only some reusable partial templates (header, navigation and footer), but also a common layout (main) that all the pages of your application can reuse. Now, lets see a page that uses the structure above:
File app/views/users/profile.scala.html:
#(user: models.User)
#main(title = "User Profile Page") {
<h2>This is the profile page of #user.username</h2>
}
And there is more: since views are compiled to Scala code, you can import code written in Scala/Java and call it directly from your views, something like Rails view helpers:
File app/views/helpers/DateHelpers.scala:
package views.helpers
object DateHelpers {
def formatToISO8601(date: Date) = {
??? // format the date
}
}
Let's use this helper in our app/views/users/profile.scala.html page:
#(user: models.User)
#import controllers.DateHelpers._
#main(title = "User Profile Page") {
<h2>This is the profile page of #user.username.</h2>
<p>User since #formatToISO8601(user.createdAt)</p>
}
And there are other approaches to consider:
Ping-Play: Big Pipe Streaming for the Play Framework
You can create a Play module that integrate with Apache Tiles. I'm pretty sure this is possible.
A quick answer would be the following. If you are comfortable with Yeoman, you can keep most of the UI part in existing HTML, while rendering some pages with Scala templates. I would recommend Play-yeoman, which may help such that you can--with minimum effort--reuse UI components.
For instance, you may easily convert a NodeJS+Angular app into Play+Angular. The Play-yeoman plugin helps a lot. But it is not so flexible as it does not support any arbitrary Yeoman configuration, just Angular.

Handlebars formatting in NetBeans

I'm using NetBeans as my IDE for a Ember.js project. When I create handlebars templates in my app like below the code highlighting doesn't work correctly.
<script type="text/x-handlebars">
<div>
</div>
</script>
Normally, when I'd select the first div, it and its matching end tag would highlight yellow, but this doesn't work. Since its inside the handlebars script tag both are highlighted red as errors and don't match together. This makes writing complex templates kinda annoying as it can be difficult to pinpoint syntax errors.
Is there anyway to get NetBeans to highlight inside the handlebars tag as if its regular html?
One option, until Netbeans implements this enhancement, is to add the following script tag in index.html immediately after your reference to jQuery:
<script src="js/libs/jquery.js"></script>
<!-- use following line to change script type to 'text/x-handlebars' -->
<script>jQuery('script[type="text/html"]').attr('type', 'text/x-handlebars');</script>
This is a variation of the answer provided by GCoda.
I had the same problem and tried various non satisfying fixes.
In the end I figured the best solution is simply to change the script's type attribute to text/html:
<script type="text/html">
<div>
</div>
</script>
I got same problem. And i just used a some kind of postprocessing, i am using node.js, so i did res.send(data.replace(/type="text\/html"/g,'type="text/x-handlebars"')); on my / page.
I think you can do something similar in you language, and ofcource this is not a fix, just an ugly trick to make developing more easy. Dont keep it in production.

Incorrect MIME type for GET requests

I've been using the Lift Web Framework as a REST only service for quite a while, but I need to use it as a stand alone tool now.
<lift:surround with="default" at="content">
<head>
<script data-lift="with-resource-id" src="/test.js" type="text/javascript"></script>
</head>
<h2>Welcome to your project!</h2>
<p><lift:helloWorld.howdy /></p>
</lift:surround>
I have the above very basic Lift template. The problem is when I view it in the browser something is adding an <?xml> DOCTYPE and the browser defaults to interpreting the resource as XML instead of plain HTML.
How do I tell Jetty/Lift that my static file is HTML?
Sounds like you may be using the XHTML doctype. In your Boot.scala file, you may want to try adding:
LiftRules.htmlProperties.default.set((r: Req) =>
new Html5Properties(r.userAgent))
That should set your application to use HTML5, and should turn off adding the <?xml... encoding header.
Also, as #VasyaNovikov mentioned, the lift: prefixed tags are an older construct (even though a lot of documentation still mentions them). They still work but will have some issues with HTML5. It is recommended to use either of the equivalent forms:
Original:
<lift:surround with="default" at="content">...</lift:surround>
HTML5:
<span data-lift="surround?with=default;at=content"></span>
<span class="lift:surround?with=default;at=content"></span>
If you want to use the lift: variety, the biggest issue you'll find is that in HTML5 the tags and attributes are converted to lowercase, so <lift:helloWorld.howdy /> will be interpreted as <lift:helloworld.howdy />, and Lift will not find the snippet. Using <span data-lift="helloWorld.howdy"></span> should allow you to work around that.
Maybe adding the header will help?
<html>
<head>...
Example:
https://github.com/lift/lift_25_sbt/blob/master/scala_29/lift_basic/src/main/webapp/index.html
In general, you use a very old approach to templates, with custom tags <lift:surround>, <lift:helloWorld> and such. Where did you get them? I suggest to use the new template style like in the link I posted.

simple coffeescript web page

I have seen coffescript tutorials that show how to use coffeescript with rails, nodejs, or even coffeescript REPL for learning it.
How can I create a web project where I can just write a cofeescript script inside an HTML page, I mean something like
<script type ="text/coffeescript">
//some coffeescript code
</script>
How to use coffeescript in developing web-sites? just tells to include output javascript and test it. But, I do not want to do that (I feel its just clumsy).
I also saw coffeescript web site, it has small amount of direction regarding this, it says-
I tried it, I included a those scripts along with jquery. But my page is remains blank. Has anyone does it before, can you provide a sample code?
Thanks.
Just include coffee-script.js in the usual manner and then add <script> elements like this:
<script type="text/coffeescript">
alert 'pancakes!'
</script>​
or this
<script type="text/coffeescript">
eggs = 'gotta have some'
document.write "It really is CoffeeScript: #{eggs}"
</script>
Demo: http://jsfiddle.net/ambiguous/DmuHh/