Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                class Template {
  #embeddedTemplates = new Map()

  constructor(source, ...values) {
    this.element = document.createElement('template')
    // pre-process the template string values
    const updatedValues = this.#prepareValues(values)
    this.source = String.raw({ raw: source }, ...updatedValues)
    this.element.innerHTML = this.source
  }

  #prepareValues(values) {
    let index = 0

    const prepareValue = v => {
      index += 1
      const i = String(index)

      if (typeof v === 'boolean' || v === null || v === undefined) {
        return ''
      }

      if (v instanceof Template) {
        // if we find a template, store it in our Map
        this.#embeddedTemplates.set(i, v)
        // substitute a small custom element including the index in the Map for this embedded template
        // our template content must be a string, so we are using this little custom element to stand in for our non-string Template object
        return `<t- data-i="${i}"></t->`
      }

      return String(v)
    }

    return values.map(v => {
      // just in case someone interpolated an array, then let's map over it
      if (Array.isArray(v)) {
        return v.map(prepareValue).join('')
      }

      return prepareValue(v)
    })
  }

  // we'll just always imagine it's true
  cloneNode() {
    const node = this.element.content.cloneNode(true)
    
    // find all those custom elements that are standing in for embedded template objects
    const embeddedTemplates = node.querySelectorAll('t-')

    // for each custom element, replace it with the cloned fragment of the template object it's standing in for
    embeddedTemplates.forEach(t => {
      const i = t.dataset.i
      const template = this.#embeddedTemplates.get(i)
      t.replaceWith(template.cloneNode())
    })

    return node
  }
}

function html(strings, ...args) {
	return new Template(strings, ...args)
}

const groceryItems = ['milk', 'eggs', 'eggos']

const template = html`
  <ul>
    ${groceryItems.map(item => html`
      <li>${item}</li>
    `)}
  </ul>
`

document.body.append(template.cloneNode())
              
            
!
999px

Console