Skip to content

Helper reference

Marc Apfelbaum edited this page Jun 3, 2018 · 1 revision

Here is a list of all available helpers in Leafpub. This documentation is a little raw — I'm still working on organizing it better. Thanks for your patience!

Theme Helpers

authors

Get all available authors with post count or do a wildcard search

_`{{#authors query="admin"}}`_  
  {{#each authors}}  
    Name: {{name}}, Posts written: {{post_count}}  
  {{/each}}  
`_{{/authors}}_`

author

Switch to an author context. If no author is specified, the helper will try to use this.author as an array then this.author as a slug.

{{#author}}
    Name: {{name}} Email: {{email}}
{{/author}}

{{#author 'bobmarley'}}
    Name: {{name}} Email: {{email}}    
{{/author}}

{{#author 'bobmarley'}}
    Author {{name}} exists
{{else}}
    Author doesn't exist
{{/author}}

body_class

Outputs classes that apply to the current template.

<body class="{{body_class}}"> ==> <body class="post-template homepage">

content

Outputs the post’s content. This helper must appear once in page.hbs and post.hbs with the editable attribute set to true. To use this helper more than once per page, omit the editable attribute.

{{content editable="true"}} ==> will be editable in the editor
{{content}} ==> will not be editable in the editor

bio, description

These “helpers” exist to map {{helper}} to {{{helper}}} since they output HTML. These are essentially convenience helpers.

navigation

Changes context to the website navigation. Use {{#each items}} to iterate over each menu item.

{{#navigation}}
    {{#each items}}
        <a class="nav-{{slug}}{{#if current}} nav-current{{/if}}" href="{{link}}">
            {{label}}
        </a>
    {{/each}}
{{/navigation}}

next_post, previous_post

Changes context to an adjacent post. If no post is specified, the helper tries to use this.post then this.slug. You can use {{else}} for when no next/previous post can be found.

{{next_post}}
    Title: {{title}}
    Content: {{content}}
{{/next_post}}

{{previous_post}}
    Title: {{title}}
    Content: {{content}}
{{/previous_post}}

posts

Search for posts with different options.

{{#posts author="bob" query="kitten" query="25" sort="oldest" tag"news"}}

post

Switches to a post context. If no post is specified, the helper will try to use this.post.

{{#post}}
    Title: {{title}}
    Content: {{content}}
{{/post}}

{{#post 'my-post-slug'}}
    Title: {{title}}
    Content: {{content}}
{{else}}
    Post doesn't exist
{{/post}}

post_class

Outputs classes that apply to the current post or page.

{{post_class}} ==> post post-featured tag-example tag-another

leafpub_foot

This helper is required once just before the </body> section of every page. Outputs important code for the editor and custom footer code for your theme.

<body>
    <!-- your HTML above -->
    {{leafpub_foot}}
</body>

leafpub_head

This helper is required once inside the <head> section of every page. Outputs important code for the editor and custom header code for your theme.

<head>
    <!-- your head code above -->
    {{leafpub_head}}
</head>

reading_time

Outputs the estimated reading time in minutes for the specified string. If no string is specified, the helper will try to use this.content.

This helper calculates the average read time using the number of words divided by words_per_minute. By default, this value is 225.

{{reading_time}} ==> 5
{{reading_time words_per_minute="250"}} ==> 4
{{reading_time "This is some random content..."}} ==> 1

recent_posts

Gets recent posts. Use {{#each posts}} to iterate over the results.

_{{#recent_posts}}_  
    {{#each posts}}  
        Title: {{title}}  
    {{/each}}  
_{{/recent_posts}}_

To limit results to a specific author or tag, set the appropriate attribute(s):

_{{#recent_posts author="bob" tag="favorites" count="5"}}  
_    {{#each posts}}  
        Title: {{title}}  
    {{/each}}    _  
{{/recent_posts}}_

suggested_posts

Gets suggested posts based on the specified post. Use {{#each posts}} to iterate over the results.

{{#suggested_posts}}
    {{#each posts}}
        Title: {{title}}
    {{/each}}
{{/suggested_posts}}

To limit results to a specific author or tag, set the appropriate attribute(s).

{{#suggested_posts author="bob" tag="favorites"}}
    {{#each posts}}
        Title: {{title}}
    {{/each}}
{{/suggested_posts}}

tag

Switch to a tag context. If no tag is specified, the helper will try to use this.tag as a collection then this.slug.

{{#tag}}
    Name: {{name}}
    Description: {{description}}
{{/tag}}

{{#tag 'my-post'}}
    Name: {{name}}
    Description: {{description}}
{{else}}
    Tag doesn't exist
{{/tag}}

tags

Outputs a formatted list of tags. Tags can be specified as an array or a comma-separated list of tag slugs. If no tags are specified, the helper tries to use this.tags.

Default output:

{{tags}} ==> <a href="#">Tag 1</a>, <a href="#">Tag 2</a>, <a href="#">Tag 3</a>

Example with all possible options:

{{#tags before="on " and=", and " after="." separator=", " links="false"}} ==> on Tag 1, Tag 2, and Tag 3.

CSV example:

_`{{tags 'tag-1, tag-2, tag-3'}}` _

title

Outputs the post’s title. This helper must appear once in page.hbs and post.hbs with the editable attribute set to true. To use this helper more than once per page, omit the editable attribute.

{{title editable="true"}} ==> will be editable in the editor{{title}} ==> will not be editable in the editor

tag_cloud

Returns a all available tags.

_`{{#tag_cloud}}`_  
_`  {{#each tags}}`_  
_`    {{name}}`_  
_`  {{/each}}`_  
_`{{/tag_cloud}}`_

img

The image helper generates a link to the specified image with specified options.
Thanks to Cory LaViska's GD wrapper class SimpleImage we're able to process images like a charm. The images are generated on the fly and are cached. It's possible to add some filters( blur, brighten, sepia and emboss ) to the image.

{{img 'path/to/the/image/image.jpg'        width="123" blur="20" brighten="20"        grayscale="true" sepia="true" emboss="true" }}

URL Helpers

admin_url

Outputs the admin URL. You can also specify a path if desired.

{{admin_url}}
{{admin_url 'path/to/file.ext'}}

author_url

Outputs an author URL. If an author is not specified, the helper will try to use this.author then this.slug.

{{author_url}}
{{author_url 'bobmarley'}}

blog_url

Outputs the URL to the blog index. You can specify a page if desired.

{{blog_url}}
{{blog_url page="2"}}

feed_url

Outputs the URL to the blog’s RSS feed. You can specify an author and/or tag if desired.

{{feed_url}}
{{feed_url author="bobmarley" tag="favorites"}}

post_url

Outputs a post URL. If a post is not specified, the helper will try to use this.slug.

{{post_url}}
{{post_url 'your-post'}}

search_url

Outputs a search URL. You can specify a query and page if desired.

{{search_url}}
{{search_url query="your-query"}}
{{search_url query="your-query" page="2"}}

tag_url

Outputs a tag URL. If a tag is not specified, the helper will try to use this.slug.

_`{{tag_url}}
{{tag_url 'your-tag'}}` _

theme_url

Outputs the current theme’s URL. You can specify a path if desired.

_`{{theme_url}}
{{theme_url 'css/styles.css'}}` _

url

Outputs the base URL of the website. You can also specify a path if desired.

_`{{url}}
{{url 'path/to/file.ext'}}` _

Utility Helpers

concat

Concatenates any number of strings using an optional delimiter.

{{concat "a" "b" "c"}} ==> abc
{{concat "a" "b" "c" delimiter=","}} ==> a,b,c

date

Outputs a formatted date. If no date is specified, the helper will try to use this.date then the current date/time.

If no format is specified, %Y-%m-%d will be used. A list of available placeholders is available in PHP’s strftime() documentation.

{{date}} ==> 2016-04-27 (current date)
{{date format="%B %e, %Y"}} ==> April 27, 2016 (current date with formatting)

{{date your_date}} ==> 2015-06-10 (variable date)
{{date your_date format="%B %e, %Y"}} ==> June 10, 2016 (variable date with formatting)

date_compare

Compares two dates using the specified operator. Requires three arguments: a date, an operator, and another date.

Date arguments can be any valid date/time format. The comparison operator can be one of <, >, <=, >=, or =. This helper can have an  {{else} block that will execute if the comparison fails.

_{{#date_compare pub_date ">" "now"}}_  
    Date is in the future  
_{{date_compare}}_

each

Iterates over a list of items. Inside the block, you can use this to reference the element being iterated over.

_{{#each items}}_  
    
  • {{this}}
  • _{{/each}}_

    either

    Given any number of arguments, this helper uses the first truthy argument available.

    For example, the following will try to use the post’s image first then the blog’s cover image. If neither are set, the {{else}} block will be executed.

    {{#either image @settings.cover}}
        <img src="{{this}}">
    {{else}}
        No image
    {{/either}}
    

    encode

    URL-encodes a string.

    {{encode "This is a string"}} ==> This%20is%20a%20string
    

    excerpt

    Given any text or HTML, outputs a plain-text excerpt of the specified length. If no string is specified, the helper will try to use this.content. You can specify the max number of characters or words to return if desired.

    {{excerpt}}
    {{excerpt chars="140"}}
    {{excerpt words="20"}}
    {{excerpt "Your string here" chars="11"}} ==> Your string
    

    if

    Compares a property for a truthy value and renders the appropriate block. You can use the optional {{else}} block as a fallback.

    _{{#if image}}_  
          
    _{{/if}}_  
    
    _{{#if cover}}_  
          
    _{{else}}_  
        No cover photo  
    _{{/if}}_

    is

    Compares one or two values using an optional operator.

    Example comparing one value (same as if):

    {{#is var1}}
        var1 is truthy
    {{else}}
        var1 is not truthy
    {{/is}}
    

    Example comparing two values (same as ==):

    {{#is var1 var2}}
        var1 and var2 are equal
    {{else}}
        var1 and var2 are not equal
    {{/is}}
    

    Example comparing two values with an operator:

    {{#is var1 ">" var2}}
        var1 is greater than var2
    {{else}}
        var1 is not greater than var2
    {{/is}}
    

    Available operators:

    • ==
    • > - greater than
    • >= - greater than or equal to
    • < - less than
    • <= - less than or equal to
    • === - equal (strict)
    • and, && - both values are truthy
    • or, || - one value is truthy
    • xor - either value is truthy, but not both
    • not, != - not equal
    • !== - not equal (strict)
    • in - check if a value is in a collection or a comma-separated string
    • not in - check if a value is not in a collection or a comma-separated string
    • type - check if a value is of the specified type (supported types: collection, string)

    json_encode

    Encodes data as a JSON string. Example:

    {{json_encode data}} ==> {"key":"value"}
    

    L

    Outputs localized terms using the current language pack.

    {{L "login"}} ==> Login
    {{L "{n} hours ago" n="4"}} ==> 4 hours ago
    

    log

    Logs the specified context to the screen. If no context is specified, the current context will be used.

    {{log}}
    {{log @settings}}
    

    markdown

    Converts markdown to HTML. Useful if you’re working with raw markdown data and need to output it as HTML.

    {{markdown '# Howdy'}} ==> <h1>Howdy</h1>
    

    match

    Compares two values using a regular expression. The regex format follows that of PHP’s preg_match().

    {{#match "test string" "/^test/"}}
        Matches
    {{else}}
        Does not match
    {{/match}}
    

    math

    Perform math operations. Basic arithmetic:

    • + - Addition

    • - - Subtraction

    • * - Multiplication

    • / - Division

    • % - Modulus

    • ^ - Exponentiation

      {{math 4 '+' 3}} ==> 7

    Mathematical functions:

    • abs - Absolute value

    • ceil - Round up

    • floor - Round down

    • round - Round

    • sqrt - Square root

      {{math 'round' 4.2}} ==> 4 {{math 'sqrt' 9}} ==> 3

    number

    Formats a number. You can specify places, decimal, and thousands if desired.

    {{number 1000}} ==> 1,000
    {{number 1000 places="2" decimal="." thousands=","}} ==> 1,000.00
    

    plural

    Outputs one of three strings based on a numeric value.

    _`{{plural your_number none="No posts" singular="One post" plural="% posts"}}` _

    post_count

    Outputs the number of posts based on the specified options. See Post::count() for all available options.

    _`{{post_count}}
    {{post_count tag="some-tag"}}
    {{post_count author="some-author"}}
    {{post_count status='draft,published'}}`_

    text

    Converts HTML to plain text.

    {{text "<strong>Hello!</strong>"}} ==> Hello!
    

    unless

    The unless helper is the inverse of the {{if}} helper. The block will be rendered if the property returns a falsy value.

    _{{#unless image}}_  
        No image  
    _{{/unless}}_  
    
    _{{#unless cover}}_  
        No cover  
    _{{else}}_  
          
    _{{/unless}}_

    with

    Switch to a specific context.

    _{{#with author}}_  
        Written by {{name}}  
    _{{/with}}_