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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ctrl/golang-template

v2.0.0

Published

Basic golang template parsing in js

Downloads

19,722

Readme

golang-template npm

TypeScript implementation of a subset of the Go template syntax.

Install

npm install @ctrl/golang-template

Usage

import { compile, parse } from '@ctrl/golang-template';

// One-shot
parse('{{ if .keywords }}{{ .keywords }}!!{{ else }}nothing{{ end }}', { keywords: '123' });
// '123!!'

// Compile once, render many times (better for hot paths)
const tmpl = compile('{{ if .name }}Hello {{ .name }}!{{ end }}');
tmpl.render({ name: 'World' }); // 'Hello World!'
tmpl.render({ name: 'Alice' }); // 'Hello Alice!'

Supported syntax

Variables

parse('{{ .foo }}', { foo: 'bar' });
// 'bar'

parse('{{ .user.name }}', { user: { name: 'Alice' } });
// 'Alice'

if / else

parse('{{ if .keywords }}{{ .keywords }}{{ else }}nothing{{ end }}', { keywords: 'swag' });
// 'swag'

if with and / or / not

parse('{{ if and .a .b }}both{{ else }}no{{ end }}', { a: 'x', b: 'y' });
// 'both'

parse('{{ if or .a .b }}either{{ else }}neither{{ end }}', { a: '', b: 'y' });
// 'either'

parse('{{ if not .disabled }}enabled{{ end }}', { disabled: false });
// 'enabled'

with

Sets . to the value for the duration of the block; skips the block if the value is falsy.

parse('{{ with .user }}Hello {{ . }}!{{ end }}', { user: 'World' });
// 'Hello World!'

parse('{{ with .user }}Hello {{ . }}{{ else }}nobody{{ end }}', { user: '' });
// 'nobody'

range

const categories = ['a', 'b', 'c'];
parse('{{ range .categories }}{{ . }};{{ end }}', { categories });
// 'a;b;c;'

const items = [{ name: 'alice' }, { name: 'bob' }];
parse('{{ range .items }}{{ .name }};{{ end }}', { items });
// 'alice;bob;'

Inside range, {{ .Field }} resolves against the current item only. {{ $.RootVar }} is still unsupported.

index

const data = {
  object: { value: 'foo' },
  array: ['bar'],
};
parse('{{ index .object "value" }}', data); // 'foo'
parse('{{ index .array 0 }}', data); // 'bar'

join †

const categories = ['a', 'b', 'c'];
parse('{{ join .categories "," }}', { categories });
// 'a,b,c'

re_replace †

parse('{{ re_replace .category "[^a-zA-Z0-9]+" "%" }}', { category: '123$special' });
// '123%special'

† Non-standard extension, not available in Go's text/template.

Compatibility

| Feature | Supported | | -------------------------------------------------------------- | --------- | | {{ .Variable }} | ✅ | | {{ .nested.path }} | ✅ | | {{ if }} / {{ else }} / {{ end }} | ✅ | | {{ if and .a .b }} / {{ if or .a .b }} / {{ if not .a }} | ✅ | | {{ with }} | ✅ | | {{ range }} with {{ . }} | ✅ | | {{ index .arr 0 }} / {{ index .obj "key" }} | ✅ | | {{ join }} (extension) | ✅ | | {{ re_replace }} (extension) | ✅ | | {{ range }} with {{ .Field }} on items | ✅ | | {{ if eq .a .b }} / {{ if gt .a .b }} etc. | ❌ | | {{ $var := .val }} variable assignment | ❌ | | {{ $.RootVar }} from inside range/with | ❌ | | {{ len .val }} | ❌ | | {{ printf }} / {{ html }} / {{ js }} | ❌ | | {{ template }} / {{ block }} | ❌ |

Warnings

This library is not safe for untrusted user input. The re_replace tag compiles user-provided regex patterns at render time, making it vulnerable to ReDoS if patterns are sourced from user data.