npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@escaladesports/sanity-template-engine

v1.0.3

Published

A template engine with user-friendly syntax for Sanity schemas

Downloads

3

Readme

sanity-template-engine

An engine for parsing a user-friendly template syntax. To be used with a Sanity schema.


Usage

1. Have the Sanity schema ready

The engine requires an array of Sanity schema types. Get this data by any means available; it should be the same as the array you would pass to schemaTypes.concat() (see Sanity docs).

Note: This engine only cares about the name, title, type, and fields properties of each type (each field within fields array should have this information as well). Anything else is unnecessary and doesn't need to be fetched/assembled.

2. Instantiate the template engine

Create an instance of the template engine:

import TemplateEngine from '@escaladesports/sanity-template-engine'

// Your own implementation here
const schema = getSchema()
const data = getData()

// 'new' keyword not required
const engine = TemplateEngine({ schema, data })

The main TemplateEngine function takes an object with these required properties:

  • schema – the Sanity schema
  • data – a Sanity document to use as the source of values to replace template variables

Important Notes:

  • The data property above should be formatted like a proper Sanity query response (i.e. an object with _id, _type, and other properties). The engine will not work unless the _type property is present on the document at the top level.
  • The engine works with reference field types in the schema, but reference values in the data should be fully resolved (i.e. having a _ref property is not enough)

3. Use the template engine

// To replace template variables in a full content excerpt
const excerpt = `Hello. My name is {{Person Info:First Name}} {{Person Info:Last Name}}`
const parsedExcerpt = engine.parse(excerpt)

// To get the value for a single variable
const value = engine.resolveProperty(`Person Info:Full Name`)

The template syntax uses double curly braces to place variables in text. Within the double curly braces, the title for a field in Sanity should be used (spaces are allowed). This is to make things more user-friendly, as users can refer to the input labels they're actually able to see in the UI.

Objects

Subfields of an object type are accessed with a colon, so something like Person Info:First Name may translate to something like info.firstName depending on the schema.

Arrays

Members of an array type are accessed with a colon followed by the index (starting at 1 to be intuitive for non-developers). For example, something like Profile:Images:1 might translate to something like profile.images[0] depending on the schema.

Provided Functions

Instances of the engine offer these 2 functions:

  • parse() – Takes an excerpt containing variables in curly braces & returns the excerpt with values substituted in
  • resolveProperty() – Gets the value for a single property (without curly braces)

Both of the above functions take an optional 2nd argument for a custom data source. By default, they use the data property passed to the engine when it was instantiated.