1. Go to this page and download the library: Download keepsuit/liquid library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
keepsuit / liquid example snippets
$environment = \Keepsuit\Liquid\EnvironmentFactory::new()
// enable strict variables mode (disabled by default)
->setStrictVariables(true)
// enable strict filters mode (disabled by default)
->setStrictFilters(true)
// rethrow exceptions instead of rendering them (disabled by default)
->setRethrowErrors(true)
// disable lazy parsing (enabled by default)
->setLazyParsing(false)
// replace the default error handler
->setErrorHandler(new \Keepsuit\Liquid\ErrorHandlers\DefaultErrorHandler())
// set filesystem used to load templates
->setFilesystem(new \Keepsuit\Liquid\FileSystems\LocalFileSystem(__DIR__ . '/views'))
// set the resource limits
->setResourceLimits(new \Keepsuit\Liquid\ResourceLimits())
// register a custom extension
->addExtension(new CustomExtension())
// register a custom tag
->registerTag(CustomTag::class)
// register a custom filters provider
->registerFilters(CustomFilters::class)
// build the environment
->build();
/** @var \Keepsuit\Liquid\Environment $environment */
// Parse from string
$template = $environment->parseString('Hello {{ name }}!');
// Parse from template (loaded from filesystem)
$template = $environment->parseTemplate('index');
/** @var \Keepsuit\Liquid\Environment $environment */
/** @var \Keepsuit\Liquid\Template $template */
// Create the render context
$context = $environment->newRenderContext(
// Data available only in the current context
data: [
'name' => 'John',
],
// Data shared with all sub-contexts
staticData: []
)
$view = $template->render($context);
// $view = 'Hello John!';
/** @var \Keepsuit\Liquid\Template $template */
/** @var \Keepsuit\Liquid\Render\RenderContext $context */
$stream = $template->stream($context);
// $stream is a Generator<string>
use Keepsuit\Liquid\Drop;
class ProductDrop extends Drop {
public function __construct(private Product $product) {}
public function title(): string {
return $this->product->title;
}
public function price(): float {
return round($this->product->price, 2);
}
#[\Keepsuit\Liquid\Attributes\Cache]
public function expensiveOperation(){
// complex operation
}
#[\Keepsuit\Liquid\Attributes\Hidden]
public function buy(){
// Do something
}
}
use Keepsuit\Liquid\Contracts\MapsToLiquid;
class Product implements MapsToLiquid {
public function __construct(public string $title, public float $price) {}
public function toLiquid(): ProductDrop {
return new ProductDrop($this);
}
}
use Keepsuit\Liquid\Parse\TagParseContext;
use Keepsuit\Liquid\Render\RenderContext;
use Keepsuit\Liquid\Tag;
class CustomTag extends Tag
{
public static function tagName(): string
{
return 'custom';
}
public function render(RenderContext $context): string
{
return '';
}
public function parse(TagParseContext $context): static
{
return $this;
}
}
// register when building the environment
$environment = \Keepsuit\Liquid\EnvironmentFactory::new()
->registerTag(CustomTag::class)
->build();
// or directly in the environment
$environment->tagRegistry->register(CustomTag::class);
use Keepsuit\Liquid\Filters\FiltersProvider;
class CustomFilters extends FiltersProvider
{
public function customFilter(string $value): string
{
return 'custom '.$value;
}
#[\Keepsuit\Liquid\Attributes\Hidden]
public function notAFilter(string $value): string
{
return 'hidden '.$value;
}
}
// register when building the environment
$environment = \Keepsuit\Liquid\EnvironmentFactory::new()
->registerFilters(CustomFilters::class)
->build();
// or directly in the environment
$environment->filterRegistry->register(CustomFilters::class);
class CustomExtension extends \Keepsuit\Liquid\Extensions\Extension
{
public function getTags() : array{
return [
CustomTag::class,
];
}
public function getFiltersProviders() : array{
return [
CustomFilters::class,
];
}
// custom registers passed to render context
public function getRegisters() : array {
return [
'custom' => fn() => 'custom value',
];
}
}
// register when building the environment
$environment = \Keepsuit\Liquid\EnvironmentFactory::new()
->addExtension(new CustomExtension())
->build();
// or directly in the environment
$environment->addExtension(new CustomExtension());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.