Skip to content

Instantly share code, notes, and snippets.

@anthonyalvarez
Last active November 4, 2018 17:52
Show Gist options
  • Select an option

  • Save anthonyalvarez/a529d20e7d85824b4b08b7cee7926f58 to your computer and use it in GitHub Desktop.

Select an option

Save anthonyalvarez/a529d20e7d85824b4b08b7cee7926f58 to your computer and use it in GitHub Desktop.
HTML / JavaScript Snippet Pattern

HTML / JavaScript Snippet Pattern

Useful for Progressive Web Apps (PWA) using App-shell architecture. Where each section and/or div in HTML is a template generated by a JavaScript function. The actual HTML web page contains very little HTML markup, styles informaiton or data.

Web pages only contains data listed below.

  • HTML5 landmark tags
  • section
  • header / footer
  • Navigation
  • main and etc.
  • link tag
  • meta tags
  • ttile tag
  • head tag
  • script tags
  • aria-label
  • aria role
  • CSS ID
  • CSS Classname

HTML tags, CSS styles and data are inserted dynamically by JavaScript instead of being hard coded into web page.

createSectionHTML(data object, variable or none) {
  
  // Create Parent Tag
  const PARENT-TAG-NAME =  document.createElement('tag-name');
   
   // set Parent Tag properties
   name.innerHTML = argument;
   
  // create child HTML tags
  const CHILD-TAG-NAME =  document.createElement('tag-name');
  
   // Set child tag properties

 // create images
 const image = document.getElementById('SECTION-ID');
  
 // set image prooerties
 image.className = 'restaurant-img';

  // return parent HTML
  return PARENT-TAG-NAME;

Separating HTML tags, CSS styles, scripts and data enables a dynamically generated website using JavaScript templates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment