Simpla-paths maps Simpla content paths to the DOM, using HTML attributes. This allows you to easily structure data declaratively in code.
Installation & usage
Simpla-paths is available on NPM/Yarn, Bower, and the Unpkg CDN
$ npm install simpla-paths --save
$ bower install simpla-paths --save
Simpla-paths is distributed as both an HTML import (
simpla-paths.min.js). You can include either in your project, but make sure you only include one of them.
<!-- As HTML import -->
Once simpla-paths is included it will begin observing IDs and constructing paths automatically (except for inside Shadow DOM - see Observing shadow roots).
Simpla-paths creates paths by stringing together IDs used in new HTML attributes. For example, this markup
Creates the path
/page/section/element for the ``` element.
Simpla-paths exposes two new HTML attributes:
sid: Scoped ID
gid: Global ID
Every element with either of these attributes will get a
path property set on it by simpla-paths.
Scoped IDs (the
sid attribute) are the building blocks of structured content. They are namespaced to any parent element with a path ID. To created nested paths, just nest HTML elements with
Global IDs (the
gid attribute) always create new paths regardless of where they are used. When applied to Simpla elements, they are equivelant to specifying
They are useful for creating reusable chunks of content that always have consistent data, regardless of where they appear on your site.
<!-- path = /page/title --><!-- path = /footer/company -->
Dynamically reloading paths
When you change any ID in a chain of IDs, the whole path is recalculated. This means you can easily fetch and reload whole sections of content dynamically by changing a single attribute.
<!-- simpla-text path = /page/section/title --><!-- simpla-text path = /about/section/title -->
Read more about structuring data with simpla-paths
Observing shadow roots
Simpla-paths automatically observes IDs and constructs paths in the main document. To use
gid attributes in Shadow DOM you will need to tell simpla-paths to observe your shadow root manually.
Do this with the
observe method on the
SimplaPaths global. It takes two arguments, the shadow tree to observe, and an optional base path (defaults to
||Scoped ID, appended to parent IDs to create nested paths|
||Global ID, creates a new root path|
Events are fired on elements that
gid attributes set
||Fired whenever an element's path changes|
If you find any issues with simpla-paths please report them! If you'd like to see a new feature in supported file an issue or let us know in Simpla's public Slack group. We also happily accept PRs.
MIT © Simpla