bloc referral

If you are interested in learning to code with a mentor, try bloc and get $100 off.
Showing posts with label html. Show all posts
Showing posts with label html. Show all posts

Friday, January 30, 2015

Marked HAML

I spent quite some time in my developing learning avoiding HAML, SLIM, and all other template alternatives, but recently I took on the task of working on a project that was exclusively written in HAML.

HAML is a template language that is meant to make writing html less of a drag. It was very common for myself to forget closing brackets and div tags which always annoyed me greatly, but I avoided the simplicity of HAML because I wanted to force myself out of that bad habit. HAML does not require any brackets and works with spacing and indentation to convert the valid html.

I initially reaction when started the project was to convert the existing HAML into/erb right away, but I then read an article from Hashrocket and found they used HAML exclusively and it got me thinking, maybe it's time for start learning this.

Here is an example of html converted to haml:

What annoyed me the most was the errors received due to the sensitivity of spacing. If your indentation is off HAML will tell you. If you write invalid code, HAML will tell you and there is less flexibility with how much Ruby logic you can place in your template.

Learning Ruby/Rails first introduced me to erb and I love it and understand it, which is another reason why I avoided HAML.

I have since stopped work on this project but did walk away with some more experience in this templating language. I can't say I would go for HAML at the start of a project but I don't think I need to avoid it anymore. I will definitely give it another shot on one for my smaller projects, especially since I am pretty sure this is not the last I will see of it.

If you come across HAML somewhere I recommend learning it and use tools like hamle2erb or htmltohaml

Wednesday, November 5, 2014

Invalidating text as html with Nokogiri

I am finding a lot of my issues with writing code at work is due to the specific use case of our industry.

I was given the task to add an input box to a form that will allow HTML snippets; these snippets can be a variety of types but of course exclude script tags. I wrote a line of code that validates HTML but it failed QA testing since plain text was allowed to be added to the form. 








This check_html method validates the HTML provided, but it was still allowing plain text, which is a functionality I had to circumvent. I finally came across a hacky solution which was to parse the content using an XML call. After 90 minutes of deliberation I came to this solution:












*note: I was checking for image tags because the XML call specifically disregards the "<img>" tags and requires "<image>", but this worked because it disallowed plain text to be input. I was later informed by my peer that not all XML was valid html and there would not stand the chance of time, I did say this was a hacky solution.

Not proud of this, I submitted my code for peer review and was quickly informed that my attempt was noted but plain text is valid html... Not taking any formal "HTML" course in college I had not known of this fact; All that work to only eventually find out that I was trying to invalidate something that was technically valid.

I was then provided this solution thanks to code review:



Nokogiri parse the HTML and separates each element tag into children and nodes. This code checks to see if thre are any children that have nodes with plain text and finally invalidates those. Like the intro to Star Trek: Enterprise, "It's been a long road".

I hope sharing this will help anyone else in the trying to validate html in put forms, this have proven to be a great learning experience.