Skip to content

HTML and Handlebars

Juozas Rastenis edited this page Mar 31, 2019 · 2 revisions

HTML

Table of Contents

Syntax
HTML5 doctype
Language attribute
IE compatibility mode
Character encoding
CSS and JavaScript includes
Attribute order
Boolean attributes
BEM in HTML
Handlebars

Syntax

  • We use soft tabs with four spaces.
  • Nested elements should be indented once (four spaces).
  • For HTML, always use double quotes, never single quotes.
  • Don't include a trailing slash in self-closing elements — the HTML5 spec says they're optional.
  • N.B. there will be scenarios where it makes sense with two spaces, for instance Drupal. Coordinate with your team.
<!DOCTYPE html>
<html>
    <head>
        <title>Page title</title>
    </head>
    <body>
        <img src="images/company-logo.png" alt="Company">
        <h1 class="header">Hello, world!</h1>
    </body>
</html>

HTML5 doctype

Enforce standards mode and more consistent rendering in every browser possible with this simple doctype at the beginning of every HTML page.

<!DOCTYPE html>
<html>
    <head>
    </head>
</html>

Language attribute

From the HTML5 spec:

Authors are encouraged to specify a lang attribute on the root html element, giving the document's language. This aids speech synthesis tools to determine what pronunciations to use, translation tools to determine what rules to use, and so forth.

Read more about the lang attribute in the spec. Head to Sitepoint for a list of language codes.

<html lang="da-dk">
    <!-- ... -->
</html>

IE compatibility mode

Internet Explorer supports the use of a document compatibility <meta> tag to specify what version of IE the page should be rendered as. Unless circumstances require otherwise, it's most useful to instruct IE to use the latest supported mode with edge mode. For more information, read read this awesome Stack Overflow article.

<meta http-equiv="X-UA-Compatible" content="IE=Edge">

Character encoding

Quickly and easily ensure proper rendering of your content by declaring an explicit character encoding. When doing so, you may avoid using character entities in your HTML, provided their encoding matches that of the document (generally UTF-8).

<head>
    <meta charset="UTF-8">
</head>

CSS and JavaScript includes

Per HTML5 spec, typically there is no need to specify a type when including CSS and JavaScript files as text/css and text/javascript are their respective defaults. HTML5 spec links

<!-- External CSS -->
<link rel="stylesheet" href="master.bundle.css">

<!-- In-document CSS -->
<style>
    /* ... */
</style>

<!-- JavaScript -->
<script src="master.bundle.js"></script>

Attribute order

HTML attributes should come in this particular order for easier reading of code.

  • class
  • id, name
  • data-*
  • src,for,type,href,value
  • title,alt
  • role, aria-* Classes make for great reusable components, so they come first. Ids are more specific and should be used sparingly (e.g., for in-page bookmarks), so they come second.
<a class="..." id="..." data-toggle="modal" href="#">
    Example link
</a>

<input class="form-control" type="text">

<img src="..." alt="...">

Boolean attributes

A boolean attribute is one that needs no declared value. XHTML required you to declare a value, but HTML5 has no such requirement. For further reading, consult the WhatWG section on boolean attributes:

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If you must include the attribute's value, and you don't need to, follow this WhatWG guideline:

If the attribute is present, its value must either be the empty string or [...] the attribute's canonical name, with no leading or trailing whitespace.

In short, don't add a value.

<input type="text" disabled>

<input type="checkbox" value="1" checked>

<select>
    <option value="1" selected>1</option>
</select>

BEM in HTML

BEM stand for Block, Element, Modifier

They are used as follows:

  • Block: The main building block for your component - It's allowed to have blocks inside blocks
  • Element: Elements inside the block. Notated by __
  • Modifier: If your block or your element needs a modifier. Notated by -- In the breadcrumb example, the component is the breadcrumb. Each like is an element, and the currently active page, has a modifier attached to it.
<!-- Block -->
<ul class="list"> 
    <!-- Block -->
    <li class="list-item">
        <!-- Element -->
        <a class="list-item__link" href="/#"> 
            Item 1
        </a>        
    </li>
    <!-- Block & Modifier -->
    <li class="list-item list-item--summer"> 
        <!-- Element -->
        <a class="list-item__link" href="/#"> 
            <!-- Element -->
            <img class="list-item__image" src="#" alt="#"> 
            Item 2
        </a>  
    </li>
    <!-- Block -->
    <li class="list-item"> 
        <!-- Element & Modifier & Modifier --> 
        <a class="list-item__link list-item__link--big list-item__link--danger" href="/#"> 
            Item 3
            <!--Block & Modifier-->
            <span class="tooltip tooltip--small"> 
                Tooltip
            </span>
        </a>        
    </li>
</ul>

<!-- Block -->
<div class="page-header"> 
    <!-- Element -->
    <h1 class="page-header__headline"> H1 </h1> 
    <!-- Element -->
    <div class="page-header__wrapper"> 
        <!-- Element -->
        <h2 class="page-header__subheadline"> H2 </h2>
        <!-- Element -->
        <p class="page-haeder__text"> Text </p> 
        <!-- Element & Block & Modifier & Modifier -->
        <button class="page-header__btn btn btn--large btn--warning"> Button </button> 
    </div>
</div>

Handlebars

We've added som extra functionallity to the handlbars parser. Here's the helpers we've added:

Helpers

Times

Times helper is an equivelant of a for loop, this example will give 10x span. {{this}} refers to the index of the loop.

{{#times 10}}
    <span>{{this}}</span>
{{/times}}

Compare

Compare helper is for comparing values with different operators. Available operators: "==", "===", "!=", "<", ">", "<=", ">=", "typeof"

{{#compare @index 5 operator="<="}}
  <span>lower than/equals 5</span>
{{else}}
  <span>higher than 5</span>
{{/compare}}

Math

Math helper provides operators for addition, subtraction, dividing, multiplying and modulus with two values.

{{math @index "+" 1}}
Clone this wiki locally