-
Notifications
You must be signed in to change notification settings - Fork 74
Helper reference
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!
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}}_`
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}}
Outputs classes that apply to the current template.
<body class="{{body_class}}"> ==> <body class="post-template homepage">
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
These “helpers” exist to map {{helper}}
to {{{helper}}}
since they output HTML. These are essentially convenience helpers.
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}}
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}}
Search for posts with different options.
{{#posts author="bob" query="kitten" query="25" sort="oldest" tag"news"}}
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}}
Outputs classes that apply to the current post or page.
{{post_class}} ==> post post-featured tag-example tag-another
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>
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>
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
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}}_
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}}
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}}
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'}}` _
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
Returns a all available tags.
_`{{#tag_cloud}}`_ _` {{#each tags}}`_ _` {{name}}`_ _` {{/each}}`_ _`{{/tag_cloud}}`_
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" }}
Outputs the admin URL. You can also specify a path if desired.
{{admin_url}}
{{admin_url 'path/to/file.ext'}}
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'}}
Outputs the URL to the blog index. You can specify a page if desired.
{{blog_url}}
{{blog_url page="2"}}
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"}}
Outputs a post URL. If a post is not specified, the helper will try to use this.slug
.
{{post_url}}
{{post_url 'your-post'}}
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"}}
Outputs a tag URL. If a tag is not specified, the helper will try to use this.slug
.
_`{{tag_url}} {{tag_url 'your-tag'}}` _
Outputs the current theme’s URL. You can specify a path if desired.
_`{{theme_url}} {{theme_url 'css/styles.css'}}` _
Outputs the base URL of the website. You can also specify a path if desired.
_`{{url}} {{url 'path/to/file.ext'}}` _
Concatenates any number of strings using an optional delimiter.
{{concat "a" "b" "c"}} ==> abc
{{concat "a" "b" "c" delimiter=","}} ==> a,b,c
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)
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}}_
Iterates over a list of items. Inside the block, you can use this
to reference the element being iterated over.
_{{#each items}}_
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}}
URL-encodes a string.
{{encode "This is a string"}} ==> This%20is%20a%20string
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
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}}_
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
)
Encodes data as a JSON string. Example:
{{json_encode data}} ==> {"key":"value"}
Outputs localized terms using the current language pack.
{{L "login"}} ==> Login
{{L "{n} hours ago" n="4"}} ==> 4 hours ago
Logs the specified context to the screen. If no context is specified, the current context will be used.
{{log}}
{{log @settings}}
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>
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}}
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
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
Outputs one of three strings based on a numeric value.
_`{{plural your_number none="No posts" singular="One post" plural="% posts"}}` _
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'}}`_
Converts HTML to plain text.
{{text "<strong>Hello!</strong>"}} ==> Hello!
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}}_
Switch to a specific context.
_{{#with author}}_ Written by {{name}} _{{/with}}_