jonassiewertsen / statamic-livewire

A Laravel Livewire integration for Statamics antlers engine
85 stars 15 forks source link

Computed Properties #11

Closed aerni closed 1 year ago

aerni commented 3 years ago

Is there a way to use computed properties with Antlers? https://laravel-livewire.com/docs/2.x/properties#computed-properties

jonassiewertsen commented 3 years ago

I haven't tried it with Antlers yet 🤷‍♂️

Does it simply not work? Are you getting any error message?

aerni commented 3 years ago

In Blade you would access a computed property like this {{ $this->post->title }}. There's no way to access $this in Antlers.

mkriegeskorte commented 2 years ago

I’ve solved it by explicitly passing the data in the render method like this:

public function render()
{
  return view('livewire.template', [
    'property' => $this->property,
  ]);
}
jonassiewertsen commented 2 years ago

Computed properties are not supported at the moment.

Hopefully the workaround from @mkriegeskorte does help.

sheldonkotyk commented 7 months ago

Just wanted to note here that the workaround here works in Livewire 3.

marcorieser commented 7 months ago

Just wanted to note here that the workaround here works in Livewire 3.

Just wanted to note here that the workaround here works in Livewire 3.

Are computed properties in v3 not accessable like normal properties? Wondering if the workaround is needed.

sheldonkotyk commented 7 months ago

Are computed properties in v3 not accessable like normal properties? Wondering if the workaround is needed.

They still require the $this->propertyname.

Must use $this in your template

Unlike normal properties, computed properties aren't directly available inside your component's template. Instead, you must access them on the $this object. For example, a computed property named posts() must be accessed via $this->posts inside your template.
marcorieser commented 7 months ago

Are computed properties in v3 not accessable like normal properties? Wondering if the workaround is needed.

They still require the $this->propertyname.

Must use $this in your template

Unlike normal properties, computed properties aren't directly available inside your component's template. Instead, you must access them on the $this object. For example, a computed property named posts() must be accessed via $this->posts inside your template.

You are right.

aerni commented 7 months ago

You could probably use Antlers' PHP functionality to make it work like {{? $this->property ?}}.

marcorieser commented 6 months ago

You could probably use Antlers' PHP functionality to make it work like {{? $this->property ?}}.

FYI: Looks like that does not work. $this refers to Antlers NodeProcessor.

Call to undefined method Statamic\View\Antlers\Language\Runtime\NodeProcessor::posts()