reading-notes

Flexbox reading notes

Templating with Mustache.js

Mustache is a logic-less template syntax used for HTML, config files, source code..etc * it works by expanding tags in a template using values provided in a hash or object

logic-less means there are no IF statements, ELSE clauses or FOR loops only tags

mustache.js is an implementation of the mustache template system in JavaScript that is considered the base for JS templating. It supports various languages and doesn’t need a seperate templating system on the server side.

Example of Mustache syntax:

Mustache.render(“Hello, ”, { name: “Sherlynn” }); // returns: Hello, Sherlynn

Mustache is NOT a template engine It is a specification for a templating language

Mustache-Express

Use this if you want to use Mustache with Node and Express

This is how you install it in the terminal

$ npm install mustache --save

Configure mustache-express in your server.js/app.js/index.js file

var mustacheExpress = require('mustache-express'); app.engine('html', mustacheExpress()); app.set('view engine', 'html'); app.set('views, _dirname + '/src/views');

Create a views folder and add some html view templates:

views

Add the HTML markup

<h1>Hello </h1>

Then in the router configuration, use

res.render(TEMPLATE_NAME, JSON_DATA).

Example:

res.render('hello', {"name": "Sherlynn"})

  1. the first parameter ‘hello’ refers to the hello.html file
    • no need to include the extension as it has been previously set as html
  2. The second parameter is the JSON data

example

var nameObject = {"name": "Sherlynn"} res.render('hello', nameObject)

A Complet Guide to Flexbox

Background

flexbox - a more efficient way to layout, align and distribute space among items in a container even when the size is unknown or dynamic

* A flex container expands items to fill available free space or shrinks them to prevent overflow.

Regular layouts are

Block - vertically based

Inline - horizontally based

Flexbox is direction-agnostic

display - defines a flex container; inline or block depending on the given value. It enables a flex content for all its direct children * CSS columns have no effect on a flex container

flex-direction - establishes the main-axis and defines the direction flex items are placed in the flex container. * Flexbox is a single direction layout concept * They layout either in horizontal rows or vertical columns

  1. row - (default) left to right
  2. row-reverse - right to left
  3. column - same as row but top to bottom
  4. column-reverse - bottom to top

.container {

flex-direction: row;

}

flex-wrap - allow items to wrap as needed * by default flex items will all try to fit on one line

  1. nowrap - (default) all flex items will be on one line
  2. wrap - flex items will wrap onto multiple lines from top to bottom
  3. wrap-reverse - flex items will wrap onto multiple lines from bottom to top.

.container {

flex-wrap: nowrap;

}

flex-flow* - shorthand for the flex-direction and flex-wrap properties which together define the flex containers main and cross axes

row nowrap - default

.container {

flex-flow: row nowrap;

}

justify-content - defines the alignment along the main axis and helps distribute extra free space leftover when: * all the flex items on a line are inflexible OR * are flexible but have reached their maximum size * also controls the alignment of items when they overflow the line.

flex-start - (default) items are packed toward the start of the flex-direction.

flex-end - items are packed toward the end of the flex-direction

start - items are packed toward the start of the writing-mode direction

end - items are packed toward the end of the writing-mode direction

left - items are packed toward left edge of the container

* unless that doesn't make sense with the **flex-direction** 
*then it behaves like **start** *right* - items are packed toward right edge of the container

* unless that doesn't make sense with the **flex-direction** 
*then it behaves like **start** *center* - items are centered along the line

space-between - items are evenly distributed in the line

* first item is on the start line
* last item  is on the end line *space-around* - items are evenly distributed in the line with equal space around them

* the spaces aren't equal since all the items have equal space on both sides
1. first item will have one unit of space against the container edge
1. the next item will have 2 units because it has its own spacing *space-evenly* - items are distributed so that the spacing between any two items (space to the edges is equal).

.container {

justify-content: flex-start;

}

order - controls the order in which they appear in the flex container * by default flex items are laid out in the source order .item { order: 5;

}

flex-grow - defines the ability for a flex item to grow if necessary 1. accepts a unitless value that serves as a proportion and dictates what amount of the available space inside the flex container the item should take up * If all items have flex-grow set to 1 the remaining space in the container will be distributed equally to all children. * If one of the children has a value of 2, the remaining space would take up twice as much space as the others. *Negative numbers are invalid

.item { flex-grow: 4; ```}``

flex-shrink - defines the ability for a flex item to shrink if necessary

.item { flex-shrink: 3; }

*Negative numbers are invalid

flex-basis - defines the default size of an element before the remaining space is distributed * it can be a length(20%, 5rem,) or a keyword * the auto keyword means width or height property * the contentsize based on the item’s content

.item { flex-basis: |auto; }

if set to 0 the extra space around content isnt factored in if set to auto, the extra space is distributed based on it’s flex-grow value

flex - shorthand for flex-grow, *flex-shrink, flex-basis combined * the default is 0 1 auto,

** It is recommended that you use this shorthand property rather than set the individual properties. The shorthand sets the other values intelligently.**

*align-self - allows the default alignment (or the one specified by align-items) to be overridden for individual flex items.

https://deannaj401.github.io/reading-notes/