Shopify/liquid — an open-source project — sits at 11.9k GitHub stars. Liquid markup language. Safe, customer facing template language for flexible web apps.
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Liquid is a template engine which was written with very specific requirements:
It has to have beautiful and simple markup. Template engines which don't produce good looking markup are no fun to use.
It needs to be non evaling and secure. Liquid templates are made so that users can edit them. You don't want to run code on your server which your users wrote.
It has to be stateless. Compile and render steps have to be separate so that the expensive parsing and compiling can be done once and later on you can just render it passing in a hash with local variables and objects.
Why you should use Liquid
You want to allow your users to edit the appearance of your application but don't want them to run insecure code on your server.
You want to render templates directly from the database.
You like smarty (PHP) style template engines.
You need a template engine which does HTML just as well as emails.
You don't like the markup of your current templating engine.
What does it look like?
<ul id="products">
{% for product in products %}
<li>
<h2>{{ product.name }}</h2>
Only {{ product.price | price }}
{{ product.description | prettyprint | paragraph }}
</li>
{% endfor %}
</ul>
How to use Liquid
Install Liquid by adding gem 'liquid' to your gemfile.
Liquid supports a very simple API based around the Liquid::Template class.
For standard use you can just pass it the content of a file and call render with a parameters hash.
@template = Liquid::Template.parse("hi {{name}}") # Parses and compiles the template
@template.render('name' => 'tobi') # => "hi tobi"
Concept of Environments
In Liquid, a "Environment" is a scoped environment that encapsulates custom tags, filters, and other configurations. This allows you to define and isolate different sets of functionality for different contexts, avoiding global overrides that can lead to conflicts and unexpected behavior.
By using environments, you can:
Encapsulate Logic: Keep the logic for different parts of your application separate.
Avoid Conflicts: Prevent custom tags and filters from clashing with each other.
Improve Maintainability: Make it easier to manage and understand the scope of customizations.
Enhance Security: Limit the availability of certain tags and filters to specific contexts.
We encourage the use of Environments over globally overriding things because it promotes better software design principles such as modularity, encapsulation, and separation of concerns.
Here's an example of how you can define and use Environments in Liquid:
user_environment = Liquid::Environment.build do |environment|
environment.register_tag("renderobj", RenderObjTag)
end
Liquid::Template.parse(<<~LIQUID, environment: user_environment)
{% renderobj src: "path/to/model.obj" %}
LIQUID
In this example, RenderObjTag is a custom tag that is only available within the user_environment.
Similarly, you can define another environment for a different context, such as email templates:
email_environment = Liquid::Environment.build do |environment|
environment.register_tag("unsubscribe_footer", UnsubscribeFooter)
end
Liquid::Template.parse(<<~LIQUID, environment: email_environment)
{% unsubscribe_footer %}
LIQUID
By using Environments, you ensure that custom tags and filters are only available in the contexts where they are needed, making your Liquid templates more robust and easier to manage. For smaller projects, a global environment is available via Liquid::Environment.default.
Error Modes
Setting the error mode of Liquid lets you specify how strictly you want your templates to be interpreted.
Normally the parser is very lax and will accept almost anything without error. Unfortunately this can make
it very hard to debug and can lead to unexpected behaviour.
Liquid also comes with different parsers that can be used when editing templates to give better error messages
when templates are invalid. You can enable this new parser like this:
Liquid::Environment.default.error_mode = :strict2 # Raises a SyntaxError when invalid syntax is used in all tags
Liquid::Environment.default.error_mode = :strict # Raises a SyntaxError when invalid syntax is used in some tags
Liquid::Environment.default.error_mode = :warn # Adds strict errors to template.errors but continues as normal
Liquid::Environment.default.error_mode = :lax # The default mode, accepts almost anything.
If you want to set the error mode only on specific templates you can pass :error_mode as an option to parse:
This is useful for doing things like enabling strict mode only in the theme editor.
It is recommended that you enable :strict or :warn mode on new apps to stop invalid templates from being created.
It is also recommended that you use it in the template editors of existing apps to give editors better error messages.
Undefined variables and filters
By default, the renderer doesn't raise or in any other way notify you if some variables or filters are missing, i.e. not passed to the render method.
You can improve this situation by passing strict_variables: true and/or strict_filters: true options to the render method.
When one of these options is set to true, all errors about undefined variables and undefined filters will be stored in errors array of a Liquid::Template instance.
Here are some examples:
template = Liquid::Template.parse("{{x | filter1 | upcase}}")
template.render({ 'x' => 'foo' }, { strict_filters: true })
#=> '' # when at least one filter in the filter chain is undefined, a whole expression is rendered as nil
template.errors
#=> [#<Liquid::UndefinedFilter: Liquid error: undefined filter filter1>]
If you want to raise on a first exception instead of pushing all of them in errors, you can use render! method:
render_score_limit and cumulative_render_score_limit account for each item visited by
integer-range for and tablerow loops, including loops with empty bodies. This bounds range
iteration work when a score limit is configured; render_length_limit only bounds generated
output and does not by itself limit CPU work for output-free loops.
Usage tracking
To help track usages of a feature or code path in production, we have released opt-in usage tracking. To enable this, we provide an empty Liquid:: Usage.increment method which you can customize to your needs. The feature is well suited to https://github.com/Shopify/statsd-instrument. However, the choice of implementation is up to you.
Once you have enabled usage tracking, we recommend reporting any events through Github Issues that your system may be logging. It is highly likely this event has been added to consider deprecating or improving code specific to this event, so please raise any concerns.
The most recent commit recorded on Shopify/liquid was 23 days ago, based on the GitHub push timestamp. The repository has 1.6k forks — one of the better signals of community interest.
How many stars does Shopify/liquid have?
Shopify/liquid has 11.9k GitHub stars — refresh the page for the live number, or check github.com/Shopify/liquid. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is Shopify/liquid open source?
Yes — Shopify/liquid ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/Shopify/liquid.
What is Shopify/liquid?
Shopify/liquid (Shopify/liquid) is a Ruby project on GitHub. From the project's own README: Liquid markup language. Safe, customer facing template language for flexible web apps.
What language is Shopify/liquid written in?
Shopify/liquid is written primarily in Ruby. GitHub's language field is based on the largest share of bytes in the default branch.
What license does Shopify/liquid use?
Shopify/liquid is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where do I read more about Shopify/liquid?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/Shopify/liquid is the definitive source.
Read full README in the tab above.
Still deciding about liquid?
One click hands the question to an AI along with this page — see what it says about liquid.