Skip to content

Instantly share code, notes, and snippets.

@linkdd
Last active March 17, 2021 13:32
Show Gist options
  • Save linkdd/5fd915e3d80906921c6c48d5bd34ed2a to your computer and use it in GitHub Desktop.
Save linkdd/5fd915e3d80906921c6c48d5bd34ed2a to your computer and use it in GitHub Desktop.
Standard Web Components with Hugo
baseURL = "https://example.com"
languageCode = "en-us"
title = "Example of Standard Web Component"
title star_wars
Example of Standard Web Component
Attack of the Clones

{{< star-wars >}}

<!DOCTYPE html>
<html>
<head>
{{ partial "metadata.html" . }}
{{ partial "css.html" . }}
{{ partial "scripts.html" . }}
</head>
<body>
{{ partial "templates.html" . }}
{{ .Content }}
</body>
</html>
<template id="star-wars-tmpl">
<h1>{{ .Params.star_wars }}</h1>
<p class="obiwan"></p>
<p class="grievous"></p>
</template>
<script
type="application/javascript"
src="/js/components/star-wars.js">
</script>
<!-- Include CSS here -->
<meta charset="utf-8" />
<title>Hugo and Standard Web Components</title>
<script
type="application/javascript"
src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous">
</script>
<script
type="application/javascript"
src="/js/components/index.js">
</script>
{{ partial "components/star-wars.html" . }}
<!-- include other templates here -->
window.BaseWebComponent = {
extends({ tag, template, render }) {
customElements.define(tag, class extends HTMLElement {
connectedCallback() {
const fragment = document.getElementById(template)
const node = document.importNode(fragment.content, true)
const instance = $('<div/>')
instance.append(node)
render(instance)
$(this).append(instance)
}
})
}
}
BaseWebComponent.extends({
tag: 'star-wars',
template: 'star-wars-tmpl',
render(instance) {
instance.find('.obiwan').text('hello there')
instance.find('.grievous').text('general kenobi')
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment