statamic-livewire icon indicating copy to clipboard operation
statamic-livewire copied to clipboard

Computed Properties

Open aerni opened this issue 4 years ago • 4 comments

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

aerni avatar May 14 '21 07:05 aerni

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

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

jonassiewertsen avatar May 15 '21 15:05 jonassiewertsen

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

aerni avatar May 17 '21 06:05 aerni

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,
  ]);
}

mkriegeskorte avatar May 18 '22 09:05 mkriegeskorte

Computed properties are not supported at the moment.

Hopefully the workaround from @mkriegeskorte does help.

jonassiewertsen avatar Aug 26 '22 10:08 jonassiewertsen

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

sheldonkotyk avatar Feb 09 '24 03:02 sheldonkotyk

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.

marcorieser avatar Feb 09 '24 04:02 marcorieser

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.

sheldonkotyk avatar Feb 09 '24 04:02 sheldonkotyk

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.

marcorieser avatar Feb 09 '24 05:02 marcorieser

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

aerni avatar Feb 09 '24 13:02 aerni

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()

marcorieser avatar Mar 07 '24 14:03 marcorieser