-
Notifications
You must be signed in to change notification settings - Fork 59
Value and object on assign #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I've been thinking about how it could be implemented. If an item is an array, it could check for an item in the array with the same key. E.g.
Then What do you think? As a quick fix in my code at the moment, I'm explicitly checking for template in the So far I haven't been able to figure out how to check if the key is an array?
|
That calls for an |
You can do this fairly easily using a Drop instead of an array. Here's quick example of what that might look like. $assigns = [
'template' => new TemplateDrop('templates/product.alternate.liquid')
]; <?php
use Illuminate\Support\Str;
use Liquid\Drop;
class TemplateDrop extends Drop
{
public function __construct(string $templatePath)
{
$templatePath = preg_replace('/\.liquid$/', '', $templatePath);
$this->liquid = [
'name' => basename($templatePath),
'type' => Str::before($templatePath, '.'),
'suffix' => null,
'directory' => Str::contains($templatePath, '/') ? dirname($templatePath) : null,
];
if (Str::contains($this->liquid['name'], '.')) {
$this->liquid['suffix'] = Str::after($this->liquid['name'], '.');
}
}
protected function beforeMethod($method)
{
return $this->liquid[$method] ?? null;
}
public function __toString()
{
return $this->liquid['name'];
}
} |
Here's an example of a dynamic drop similar to Shopify linklists drop.
$assigns = [
'linklists' => new LinkListsDrop
]; class LinkListsDrop extends \Liquid\Drop
{
public function invokeDrop($handle)
{
if ($linklist = \App\Models\LinkList::where('handle', $handle)->first()) {
return $linklist->toArray();
}
}
} |
I'm not able to access elements in the elements in template.
Here are the assigns:
|
Ya sorry left this out. You'll need to overwrite the beforeMethod method like so. class TemplateDrop extends \Liquid\Drop {
protected function beforeMethod($method) {
return $this->liquid[$method] ?? null;
}
} |
Thanks, that has fixed it. 👍 I've created a Drop class to get the search types which in Shopify returns an array but also a string. https://help.shopify.com/en/themes/liquid/objects/search#search-types I can't get it to work with numeric arrays. This works:
This does not work:
What am I doing wrong here?
The assigns:
|
If you want to iterate (ie. foreach) the drop you'll need to make it iterable. For example: class SearchTypesDrop extends \Liquid\Drop implements \IteratorAggregate
{
protected $types = [];
protected $validTypes = ['article', 'document', 'page'];
public function __construct (string $types)
{
$types = explode(',', $types);
$types = array_map('trim', $types);
$this->types = array_intersect($this->validTypes, $types);
if (count($this->types) === 0) {
$this->types = $this->validTypes;
}
}
public function getIterator()
{
return new \ArrayIterator($this->types);
}
public function __toString()
{
return implode('', $this->types);
}
} |
Shopify is using a value and an object/array on their
template
object. Any idea how I can achieve something similar?https://help.shopify.com/en/themes/liquid/objects/template
If you're on the product.alternate.liquid template:
{{ template }}
will outputproduct.alternate
{{ template.name }}
will outputproduct
The text was updated successfully, but these errors were encountered: