{"id":13395152,"url":"https://github.com/Shopify/liquid","last_synced_at":"2025-03-13T20:31:53.620Z","repository":{"id":397344,"uuid":"15435","full_name":"Shopify/liquid","owner":"Shopify","description":"Liquid markup language. Safe, customer facing template language for flexible web apps. ","archived":false,"fork":false,"pushed_at":"2025-03-07T18:00:00.000Z","size":5975,"stargazers_count":11277,"open_issues_count":383,"forks_count":1429,"subscribers_count":784,"default_branch":"main","last_synced_at":"2025-03-09T21:16:19.646Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://shopify.github.io/liquid/","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Shopify.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2008-05-08T15:27:26.000Z","updated_at":"2025-03-09T16:50:35.000Z","dependencies_parsed_at":"2023-07-05T14:47:02.612Z","dependency_job_id":"a3c2d016-c2c9-4a43-90f5-035a3f674e16","html_url":"https://github.com/Shopify/liquid","commit_stats":{"total_commits":1270,"total_committers":185,"mean_commits":6.864864864864865,"dds":0.8196850393700787,"last_synced_commit":"b4667adadff0b648b33678d5db958e59b59c7f80"},"previous_names":[],"tags_count":59,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Fliquid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Fliquid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Fliquid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Fliquid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shopify","download_url":"https://codeload.github.com/Shopify/liquid/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242776225,"owners_count":20183359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T17:01:44.265Z","updated_at":"2025-03-13T20:31:53.602Z","avatar_url":"https://github.com/Shopify.png","language":"Ruby","readme":"[![Build status](https://github.com/Shopify/liquid/actions/workflows/liquid.yml/badge.svg)](https://github.com/Shopify/liquid/actions/workflows/liquid.yml)\n[![Inline docs](http://inch-ci.org/github/Shopify/liquid.svg?branch=master)](http://inch-ci.org/github/Shopify/liquid)\n\n# Liquid template engine\n\n* [Contributing guidelines](CONTRIBUTING.md)\n* [Version history](History.md)\n* [Liquid documentation from Shopify](https://shopify.dev/docs/api/liquid)\n* [Liquid Wiki at GitHub](https://github.com/Shopify/liquid/wiki)\n* [Website](http://liquidmarkup.org/)\n\n## Introduction\n\nLiquid is a template engine which was written with very specific requirements:\n\n* It has to have beautiful and simple markup. Template engines which don't produce good looking markup are no fun to use.\n* 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.\n* 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.\n\n## Why you should use Liquid\n\n* 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**.\n* You want to render templates directly from the database.\n* You like smarty (PHP) style template engines.\n* You need a template engine which does HTML just as well as emails.\n* You don't like the markup of your current templating engine.\n\n## What does it look like?\n\n```html\n\u003cul id=\"products\"\u003e\n  {% for product in products %}\n    \u003cli\u003e\n      \u003ch2\u003e{{ product.name }}\u003c/h2\u003e\n      Only {{ product.price | price }}\n\n      {{ product.description | prettyprint | paragraph }}\n    \u003c/li\u003e\n  {% endfor %}\n\u003c/ul\u003e\n```\n\n## How to use Liquid\n\nInstall Liquid by adding `gem 'liquid'` to your gemfile.\n\nLiquid supports a very simple API based around the Liquid::Template class.\nFor standard use you can just pass it the content of a file and call render with a parameters hash.\n\n```ruby\n@template = Liquid::Template.parse(\"hi {{name}}\") # Parses and compiles the template\n@template.render('name' =\u003e 'tobi')                # =\u003e \"hi tobi\"\n```\n\n### Concept of Environments\n\nIn 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.\n\nBy using environments, you can:\n\n1. **Encapsulate Logic**: Keep the logic for different parts of your application separate.\n2. **Avoid Conflicts**: Prevent custom tags and filters from clashing with each other.\n3. **Improve Maintainability**: Make it easier to manage and understand the scope of customizations.\n4. **Enhance Security**: Limit the availability of certain tags and filters to specific contexts.\n\nWe encourage the use of Environments over globally overriding things because it promotes better software design principles such as modularity, encapsulation, and separation of concerns.\n\nHere's an example of how you can define and use Environments in Liquid:\n\n```ruby\nuser_environment = Liquid::Environment.build do |environment|\n  environment.register_tag(\"renderobj\", RenderObjTag)\nend\n\nLiquid::Template.parse(\u003c\u003c~LIQUID, environment: user_environment)\n  {% renderobj src: \"path/to/model.obj\" %}\nLIQUID\n```\n\nIn this example, `RenderObjTag` is a custom tag that is only available within the `user_environment`.\n\nSimilarly, you can define another environment for a different context, such as email templates:\n\n```ruby\nemail_environment = Liquid::Environment.build do |environment|\n  environment.register_tag(\"unsubscribe_footer\", UnsubscribeFooter)\nend\n\nLiquid::Template.parse(\u003c\u003c~LIQUID, environment: email_environment)\n  {% unsubscribe_footer %}\nLIQUID\n```\n\nBy 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`.\n\n### Error Modes\n\nSetting the error mode of Liquid lets you specify how strictly you want your templates to be interpreted.\nNormally the parser is very lax and will accept almost anything without error. Unfortunately this can make\nit very hard to debug and can lead to unexpected behaviour.\n\nLiquid also comes with a stricter parser that can be used when editing templates to give better error messages\nwhen templates are invalid. You can enable this new parser like this:\n\n```ruby\nLiquid::Environment.default.error_mode = :strict\nLiquid::Environment.default.error_mode = :strict # Raises a SyntaxError when invalid syntax is used\nLiquid::Environment.default.error_mode = :warn # Adds strict errors to template.errors but continues as normal\nLiquid::Environment.default.error_mode = :lax # The default mode, accepts almost anything.\n```\n\nIf you want to set the error mode only on specific templates you can pass `:error_mode` as an option to `parse`:\n```ruby\nLiquid::Template.parse(source, error_mode: :strict)\n```\nThis is useful for doing things like enabling strict mode only in the theme editor.\n\nIt is recommended that you enable `:strict` or `:warn` mode on new apps to stop invalid templates from being created.\nIt is also recommended that you use it in the template editors of existing apps to give editors better error messages.\n\n### Undefined variables and filters\n\nBy 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.\nYou can improve this situation by passing `strict_variables: true` and/or `strict_filters: true` options to the `render` method.\nWhen 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.\nHere are some examples:\n\n```ruby\ntemplate = Liquid::Template.parse(\"{{x}} {{y}} {{z.a}} {{z.b}}\")\ntemplate.render({ 'x' =\u003e 1, 'z' =\u003e { 'a' =\u003e 2 } }, { strict_variables: true })\n#=\u003e '1  2 ' # when a variable is undefined, it's rendered as nil\ntemplate.errors\n#=\u003e [#\u003cLiquid::UndefinedVariable: Liquid error: undefined variable y\u003e, #\u003cLiquid::UndefinedVariable: Liquid error: undefined variable b\u003e]\n```\n\n```ruby\ntemplate = Liquid::Template.parse(\"{{x | filter1 | upcase}}\")\ntemplate.render({ 'x' =\u003e 'foo' }, { strict_filters: true })\n#=\u003e '' # when at least one filter in the filter chain is undefined, a whole expression is rendered as nil\ntemplate.errors\n#=\u003e [#\u003cLiquid::UndefinedFilter: Liquid error: undefined filter filter1\u003e]\n```\n\nIf you want to raise on a first exception instead of pushing all of them in `errors`, you can use `render!` method:\n\n```ruby\ntemplate = Liquid::Template.parse(\"{{x}} {{y}}\")\ntemplate.render!({ 'x' =\u003e 1}, { strict_variables: true })\n#=\u003e Liquid::UndefinedVariable: Liquid error: undefined variable y\n```\n\n### Usage tracking\n\nTo 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.\n\nOnce 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.\n","funding_links":[],"categories":["HTML \u0026 Markup","Ruby","Template Engine","Liquid Template Language","others"],"sub_categories":["Template Engines"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FShopify%2Fliquid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FShopify%2Fliquid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FShopify%2Fliquid/lists"}