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

hbs-wp-helpers

v2.0.4

Published

Handlebars helpers for responsive Wordpress API images

Downloads

23

Readme

hbs-wp-helpers

Handlebars helpers for rendering responsive images in posts while using Wordpress API V2.

Installation

npm install hbs-wp-helpers --save

Usage

Just declare it. It registers helpers to handlebars.

hbsWpHelpers  = require('hbs-wp-helpers');

Helpers

srcSet

Generates the html attribute srcset to an img by parsing the "featured_media" object from a post. Input:

{{srcSet postMediaObject}}

Example:

<img src="/images/if-no-image.jpg" srcset="{{{srcSet post._embedded.wp:featuredmedia.0. }}}">

Output:

<img
    src="http://your-wordpress-site.com/wp-content/uploads/sites/2/2017/03/My-Image-1.jpg"
    srcset="
        http://your-wordpress-site.com/wp-content/uploads/sites/2/2017/03/My-Image-1-150x150.jpg 150w,
        http://your-wordpress-site.com/wp-content/uploads/sites/2/2017/03/My-Image-1-300x225.jpg 250w,
        http://your-wordpress-site.com/wp-content/uploads/sites/2/2017/03/My-Image-1-600x600.jpg 200w,
        http://your-wordpress-site.com/wp-content/uploads/sites/2/2017/03/My-Image-1-1280x720.jpg 320w ">

srcSetByHeight

Same as srcSet but uses height values.

srcMediaFitsHeight / srcMediaFitsWidth

Takes the closest image that covers the height/width passed as param:

{{srcMediaFitsWidth 200 post._embedded.wp:featuredmedia.0.}}

Output:

http://your-wordpress-site.com/wp-content/uploads/sites/2/2017/03/My-Image-1-300x225.jpg

srcMediaFitsHeightAcf / srcMediaFitsWidthAcf

The same as srcMediaFitsHeight / srcMediaFitsWidth but passing custom field's (acf) media as param.

parsePost (post)

Embeds post object as using Handlebars builtin helper #with with some useful new variables:

media

Contains the post media reference post._embedded.wp:featuredmedia.0. for a smarter code when calling image helpers:

    {{parsePost post}}
        {{title}}
        {{#if media}}
            <img 
                src="{{srcMediaFitsWidth 200 media}}" 
                src-set="{{srcSet media}}">
        {{/if}}
    {{/parsePost}}

instead of

    <h1>{{post.title}}</h1>  
    {{#if post._embedded.wp:featuredmedia.0.source}}
        <img 
            src="{{srcMediaFitsWidth 200 post._embedded.wp:featuredmedia.0.}}" 
            src-set="{{srcSet post._embedded.wp:featuredmedia.0.}}">
    {{/if}}

###toArray Converts passed params to array:

{{toArray param1 param2 param3}}

Output:

[param1, param2, param3]

parseLinksInText

Parses urls from text and converts them to tag.

{{parseLinksInText 'My url is http:example.com' 'myClass'}}

Output:

 My url is <a class="myClass" href="http:example.com">http:example.com</a>

pluralize

Renders singular or plural word depending on the count passed as param.

{{pluralize count, singular, plural}}

Example:

<span>{{pluralize 5, 'banana', 'bananas'}}</span>

Output:

<span>bananas</span>

pagination

Sets the variables needed for the pagination nav.

Example:

{{#pagination 7 20 5}}
    <ul>
        {{#unless startFromFirstPage}}
            <li>
                <a href="/news/page/{{firstPage}}">first page</a>
            </li>
            <li>
            <a href="/news/page/{{previous}}">previous</a>
            </li>
        {{/unless}}

        {{#each pages}}
            {{#if isCurrent}}
                <li class="active">
                    <a>{{page}}</a>
                </li>
            {{/if}}
            {{#unless isCurrent}}
                <li>
                    <a href="/news/page/page/{{page}}">{{page}}</a>
                </li>
            {{/unless}}
        {{/each}}

        {{#unless endAtLastPage}}
            <li>
                <a href="/news/page/{{next}}">next page</a>
            </li>
            <li>
                <a href="/news/page/{{lastPage}}">last page</a>
            </li>
        {{/unless}}
    </ul>
{{/pagination}}

Output:

<ul>
<li><a href="/news/page/1">first page</a></li>
<li><a href="/news/page/6">last</a></li>
<li><a href="/news/page/5">5</a></li>
<li><a href="/news/page/6">6</a></li>
<li class="active">     <a>7</a></li>
<li><a href="/news/page/6">6</a></li>
<li><a href="/news/page/7">7</a></li>
<li><a href="/news/page/8">next</a></li>
<li><a href="/news/page/20">last</a></li>
</ul>

Dependencies

  • express-hbs: Express 3 handlebars template engine complete with multiple layouts, partials and blocks.

License

ISC