-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
New when() helper. #52665
New when() helper. #52665
Conversation
Shouldn't escaping be on by default? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it! Nice work @danmatthews.
Have made some suggestions for a few small fixes 🙂
Love it! +1 This looks much cleaner compared to having all these inline |
I see that this would bring benefits, but I believe it would be interesting to consider the following:
<div @when($true, ['wire:poll.5s' => 'myMethodName'])>
<div @unless($false, ['wire:poll.5s'])> Also, the example: <input name="phone" @when($isRequired, 'required') /> ... would probably be discarded since the framework already has |
I believe the framework already has unless, but it's a block scoped statement:
And i'm not sure you can have both a block scoped statement AND a single tag directive with the same name. Good point with required though, always forget about |
@taylorotwell yeah maybe people would expect I wouldn't be against just having the <div {{ when($someCondition, 'wire:poll') }}> Or even possibly change |
@taylorotwell @joshhanley what about setting a new blade 'paradigm' with Edit: Paradigm is the wrong word, but including "print" in the directive name would mean that we might have to enforce this going forward on any directives that do 'echo' or 'print' things, and if that's at odds with current ones that do print things without this prefix. Edit 2: For simplicity, i'm actually down with @joshhanley 's suggestion of the helper, then the print logic using |
@joshhanley I would maybe start with just the |
|
How about adding a default value, instead of defaulting to As this is now a helper, I guess the return type-hint could be dropped, so it won't cast to a string when used in a context other than a blade template. Also, I would drop the function when($condition, $value, $default = null)
{
if ($condition) {
return value($value, $condition);
}
return value($default, $condition);
} This way, one could do something like this: {{ when($user, fn ($user) => $user->preference('theme'), 'light') }} And simple usage would still work: {{ when($user->meetsSomeCriteria(), 'wire:poll.5s="myMethodName"') }} |
@rodrigopedra I like it, i've got a refactor that matches this on the go, but would love to hear from @joshhanley and/or @taylorotwell what they think about
|
It is silly how annoyed I get whenever I have to do the Dig this. |
@danmatthews PR's looking good! I'm with @timacdonald it really annoys me having to do that too haha 😂 Re the default, while I see it's benefit, I also think that if you need a default, then just revert back to using the ternary operator @rodrigopedra is right about the return type, I don't think it should be restricted to just a string. As I will use So keen to be able to use this 😁 |
Cool, i've removed the default, but kept the
Currently it returns |
Thanks! |
Based on a little twitter discussion with Josh Cirre and @joshhanley over on twitter - it was his idea really!
Adds a new
when()
helper method to make this work, but this could either be exposed as a helper, not exposed as a helper, renamed to something else likeprintWhen
orechoWhen
if it keeps things less cluttered.Use Case Example
Useful for attributes and more, the example we explored in the thread was for conditionally printing a livewire binding:
But could also be useful for conditionally printing things in general: