This gem includes list of useful Liquid blocks, tags and filters which can be all embedded in both Wagon and the LocomotiveCMS engine. Behind the scene, it uses Solid to write reliable and consistant liquid code.
For now, it only works best with the edge versions of Wagon and the engine (master branch).
The json filter escapes quotes. It can also work on arrays and collections.
Escape quotes of the name of the current site:
{{ site.name | json }}
Output a single attribute of a collection of content entries:
{{ projects.all | json: name }}
"Project #1","Project #2",...,"Project #3"
Output only the name and the description of the list of projects
{{ projects.all | json: name, description }}
{"name":"Project #1","description":"Lorem ipsum"},...,{"name":"Project #n","description":"Lorem ipsum"}
Formats a number into a currency string (e.g., $13.65). You can customize the format in the options.
{{ "42.00" | money }}
{{ product.price | money: "precision:2, unit:'€', separator:',', delimiter:'', format:'%n %u'" }}
Formats a number as a percentage string (e.g., 65%). You can customize the format in the options
{{ "100" | percentage }}
{{ a_percentage | percentage: "precision:2, separator:',', delimiter:'', significant:true" }}
It behaves exactly like the default liquid for but with the exception of having an extra property named "join".
{% for project in projects join: ", " %}{{ project.name }}{% enfor %}
Project #1, Project #2, Project #3
Send an email directly from a liquid template. Although, it does break the MVC pattern, it is reasonable to use it within a LocomotiveCMS site for simple needs.
The emails are sent by Pony. It is disabled in Wagon, only the logs are output.
By default, email sending is html-formatted. You can disable this behaviour by setting the html attribute to false.
The code inside {% sendemail %} and {% endsendemail %} serves as the body of the email. You can access the whole context of your page within the body.
{% if post? and params.email != '' %}
<p>Email sent to {{ params.email }}</p>
{% send_email to: params.email, from: '[email protected]', subject: 'Hello world', page_handle: 'email-template', smtp_address: 'smtp.example.com', smtp_user_name: 'user', smtp_password: 'password' %}
Hello {{ send_email.to }},
Lorem ipsum....
{% endsend_email %}
{% else %}
....
{% endif}
Note: In order to set up the smtp server in Pony, use the smtp_**** attributes.
If you want to use a page as the template for the email, use the page_handle property which is the handle of your page.
{% send_email to: params.email, from: '[email protected]', subject: 'Hello world', page_handle: 'email-template', smtp_address: 'smtp.example.com', smtp_user_name: 'user', smtp_password: 'password' %}{% endsend_email %}
It is also possible to attach a file to the email.
You can pass directly some text:
{% send_email to: params.email, from: '[email protected]', subject: 'Hello world', page_handle: 'email-template', smtp_address: 'smtp.example.com', smtp_user_name: 'user', smtp_password: 'password', attachment_name: 'my_file.txt', attachment_value: 'hello world' %}{% endsend_email %}
Or an url, or even, a path to a local file:
{% send_email to: params.email, from: '[email protected]', subject: 'Hello world', page_handle: 'email-template', smtp_address: 'smtp.example.com', smtp_user_name: 'user', smtp_password: 'password', attachment_name: 'my_file.txt', attachment_value: '/somewhere/test.txt' %}{% endsend_email %}
Note: An attachment's mime-type is set based on the filename (as dictated by the ruby gem mime-types). So 'foo.pdf' has a mime-type of 'application/pdf'
Display the posts of a Facebook account. The Liquid code inside the facebook_posts tag will be used as the template to display a single Facebook post.
A liquid variable named facebook_post is available inside. It contains the following attributes representing a Facebook post.
- name
- message
- picture
- link
- created_time
<ul>
{% facebook_posts account: 'my_account', access_token: 'my access token', limit: 10 %}
<li>
<a href="{{ facebook_post.link }}">{{ facebook_post.name }}</a>
<br/>
Posted on {{ facebook_post.created_time | date: "%a, %d %b %Y" }}
</li>
{% endfacebook_posts %}
</ul>
Create a new entry for any content types of the current site. Again, it does break the MVC pattern, so use it wisely.
{% create 'projects', title: 'Your new title' %}
<p>The project {{ project.title }} was created with success</p>
{% else %}
<p>Arrggh, there was an error.</p>
{% endupdate %}
Note: The whole context of the page is available in the 2 blocks.
Update the attributes of a content entry. Again, it does break the MVC pattern, so use it wisely.
{% update project, title: 'Your new title' %}
<p>The project gets updated with success</p>
{% else %}
<p>Arrggh, there was an error.</p>
{% endupdate %}
Note: The whole context of the page is available in the 2 blocks.
In your Wagon site, add the following line to your Gemfile:
group :misc do
gem 'locomotivecms_liquid_extensions', github: 'locomotivecms/liquid_extensions'
end
In the Gemfile of your engine, add the following line:
gem 'locomotivecms_liquid_extensions', github: 'locomotivecms/liquid_extensions', branch: 'hosting'
First, get the source
$ git clone git://github.com/locomotivecms/liquid_extensions.git
$ cd liquid_extensions
Note: Again, if you want to contribute, you may consider to fork it instead
$ bundle install
Run the tests suite
$ bundle exec rake spec
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Feel free to contact me at did at locomotivecms dot com.
Copyright (c) 2014 NoCoffee, released under the MIT license