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

@brizy/content-placeholder

v1.0.6

Published

Brizy content placeholders used to put dynamic data in compiled page (PREVIEW)

Downloads

39

Readme

Brizy content placeholders sdk

Let's assume we have an email template that must be sent for many users and this template must contain the username and some other dynamic info.

Email template:

Hi {{username}}
I wanted to personally welcome you to {{company-name}}
If you have any questions, you can always email us to {{our-email}}

Best Regards.

As you can see the template above contains three placeholders username, company-name and our-email.

This can easily achieved by replacing the strings with str_replace but what if you have 100 placeholders and some of them must get info from resources like a DB or an API.

Architecture

Few words about the classes you will work with

Registry Class

A class that manage the palceholders. You can register or obtain placeholders. See the examples blow.

Placeholder Interface

All placeholders must implement this interface.

The getValue method must return the string that will replace the placeholder. This method receive a context and the content placeholder object (An object that contain all the info about the placeholder found in the original content)

The support method must return true if the class can handle the placeholder.

Context Interface

There are cases when the placeholder will need some specific info like the current page or current request, session, etc.. all these objects must be passed in a context object.

Replacer Class

The class has only one method: replacePlaceholders. Self explanatory :).


Get Started

First you must create your own design of placeholder. You can extend the AbstractPlaceholder

import { AbstractPlaceholder } from "@brizy/content-placeholder"
import type { Attr } from "@brizy/content-placeholder"

export class Placeholder extends AbstractPlaceholder {
  protected label: string 
  protected placeholder: string
  protected value: string
  protected attributes: Attr | undefined
  constructor(label: string, placeholder: string, value: string, attrs?: Attr) {
    super()
    this.label = label
    this.placeholder = placeholder
    this.value = value
    if (attrs) {
      this.setAttributes(attrs)
    }
  }

  public setAttributes(attrs: Attr) {
    this.attributes = attrs
  }

  public getAttributes(): Attr | undefined {
    return this.attributes
  }

  public getLabel(): string {
    return this.label
  }

  public setLabel(label: string) {
    this.label = label
  }

  public setPlaceholder(placeholder: string) {
    this.placeholder = placeholder
  }

  public getPlaceholder(): string {
    return this.placeholder
  }

  public support(placeholderName: string): boolean {
    return placeholderName === this.placeholder
  }

  public getValue(): string {
    return this.value
  }

  public getConfigStructure() {
    return {
      id: this.getUid(),
      label: this.getLabel(),
      name: this.getPlaceholder(),
      placeholder: this.buildPlaceholder(),
      attr: this.getAttributes(),
      varyAttr: this.getVaryAttributes(),
    }
  }
}

Register you placeholder

import { Registry } from "@brizy/content-placeholder"
// Initialize a register of placeholder
const registry = new Registry();

// Create placeholders:
const titlePlaceholder = new Placeholder("label", "title","HomePage");
//...

// Add created placeholder to the registry
registry.registerPlaceholder(titlePlaceholder);

Replace placeholder with their value

import { EmptyContext, Replacer } from "@brizy/content-placeholder"

//Initialize replacer with the registry of placeholders
const replacer = new Replacer(registry);

// and then you can use this to replace placeholdes with your values

const htmlWithPlaceholders = `
<div>
    <h1>{{title}}</h1>
    <article>
        //... rest of page
    </article>
</div>
`;

const finalHTML = replacer.replacePlaceholders(htmlWithPlaceholders, new EmptyContext());

// and the final HTML will be
console.log(finalHTML); // return the html with replaced values.

And the finalHTML value is :

<div>
    <h1>HomePage</h1>
    <article>
    //... rest of page
    </article>
</div>