NAME

WWW::Shopify::Liquid - Fully featured liquid preprocessor with shopify tags & filters added in.

DESCRIPTION

A concise and clear liquid processor. Runs a superset of what Shopify can do. For a strict shopify implementation see Template::Liquid for one that emulates all the quirks and ridiculousness of the real thing, but without the tags. (Meaning no actual arithemtic is literal tags without filters, insanity on acutal number processing and conversion, insane array handling, no real optimization, or partial AST reduction, etc.., etc..).

Combines a lexer, parser, optimizer and a renderer. Can be invoked in any number of ways. Simplest is to use the sub this module exports, liquid_render_file.

use WWW::Shopify::Liquid qw/liquid_render_file/;

$contents = liquid_render_file({ collection => { handle => "test" } }, "myfile.liquid");
print $contents;

This is the simplest method. There are auxiliary methods which provide a lot of flexibility over how you render your liquid (see below), and an OO interface.

This method represents the whole pipeline, so to get an overview of this module, we'll describe it here. Fundamentally, what liquid_render_file does, is it slurps the whole file into a string, and then passes that string to the lexer. This then generates a stream of tokens. These tokens are then transformed into an abstract syntax tree, by the the parser if the syntax is valid. This AST represents the canonical form of the file, and can, from here, either transformed back into almost the same file, statically optimized to remove any unecessary calls, or partially optimized to remove branches of the tree for which you have variables to fill at this time, though both these steps are optional.

Finally, these tokens are passed to the renderer, which interprets the tokens and then produces a string representing the final content that can be printed.

Has better error handling than Shopify's liquid processor, so if you use this to validate your liquid, you should get better errors than if you're simply submitting them. This module is integrated into the Padre::Plugin::Shopify module, so if you use Padre as your Shopify IDE, you can automatically check the liquid of the file you're currently looking at with the click of a button.

You can invoke each stage individually if you like.

use WWW::Shopify::Liquid;
my $text = ...
my $liquid = WWW::Shopify::Liquid->new;
my @tokens = $liquid->lexer->parse_text($text);
my $ast = $liquid->parser->parse_tokens(@tokens);

# Here we have a partial tree optimization. Meaning, if you have some of your
# variables, but not all of them, you can simplify the template.
$ast = $liquid->optimizer->optimize({ a => 2 }, $ast);

# Finally, you can render.
$result = $liquid->renderer->render({ b => 3 }, $ast);

If you're simply looking to check whether a liquid file is valid, you can do the following:

use WWW::Shopify::Liquid qw/liquid_verify_file/;
liquid_verify_file("my-snippet.liquid");

If sucessful, it'll return nothing, if it fails, it'll throw an exception, detailing the fault's location and description.

STATUS

This module is currently in beta. That means that while it is able to parse and validate liquid documents from Shopify, it may be missing a few tags. In addition to this, the optimizer is not yet fully complete; it does not do advanced optimizations such as loop unrolling. However, it does do partial tree rendering. Essentially what's missing is the ability to generate liquid from syntax trees.

This is close to complete, but not quite there yet. When done, this will be extremely beneficial to application proxies, as it will allow the use of custom liquid syntax, with partial evaluation, before passing the remaining liquid back to Shopify for full evaluation. This will allow you to do things like have custom tags that a user can customize which will be filled with your data, yet still allow Shopify to evaluate stuff like asset_urls, includes, and whatnot.

SEE ALSO

WWW::Shopify, WWW::Shoipfy::Tools::Themer, Padre::Plugin::Shopify

AUTHOR

Adam Harrison (adamdharrison@gmail.com)

LICENSE

Copyright (C) 2016 Adam Harrison

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.