Skip to content
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

[5.2] Allow for @has in blade #12866

Merged
merged 7 commits into from
Mar 28, 2016
Merged

[5.2] Allow for @has in blade #12866

merged 7 commits into from
Mar 28, 2016

Conversation

alexbowers
Copy link
Contributor

Currently, it is difficult to determine whether something can be @yield'd

Code usage is the same as @if.

Example usage:

image

Currently, it is difficult to determine whether something can be @yield'd

Code usage is the same as @if.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@alexbowers
Copy link
Contributor Author

Okay. styleci makes things annoying. If necessary I can squash all commits down.

@alexbowers
Copy link
Contributor Author

I also can't find where blade gets tested.

@GrahamCampbell GrahamCampbell changed the title Allow for @has in blade [5.2] Allow for @has in blade Mar 25, 2016
*/
protected function compileHas($expression)
{
return "<?php if(!empty(trim(\$__env->yieldContent{$expression}))): ?>";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may use $__env->hasSection to determine whether a section exists.

Actually, yieldContent does check the section is set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should this be changed? To me this seems correct. I want to see whether there is any content to be yielded.

@mark86092
Copy link
Contributor

IMHO, you should define the @has, is it "has section" or "is yielding content nothing" ?

For example, if a section foo

@section("foo", "0 ");

, the @has("foo") results in false. (empty(trim("0 ") --> true)

@alexbowers
Copy link
Contributor Author

Ah yes. Perhaps the fix is to check length > 0, rather than empty?

@alexbowers
Copy link
Contributor Author

And should it perhaps be called hasYield? Though to me that is not as clean

@Cyrille37
Copy link
Contributor

Intuitively, I think @has is to test if a view variable exists ...
I really prefer @hasYield

@themsaid
Copy link
Member

I have a custom blade directive in one of my projects that works like this:

@display('title', 'Site Name', '% - Site Name')

It yields the section content as it is if exists, if not then the second argument will be displayed. If the third argument has a value it'll yield the section content based on the format.

Maybe such directive can be used to provide the same functionality instead of a condition, otherwise I believe @hasSection should end with @endhassection just like @endcan. Or maybe make it @ifHasSection so that it may end with @endif.

@mark86092
Copy link
Contributor

I would like @hassection with @endsection or @hasyield with @endhasyield.
Although the word is long, it is more clear

@alexbowers
Copy link
Contributor Author

I agree that the endif is strange, and can easily have a @endhas added (or equivalent).

Before I make any changes on this then, I'd like to get @taylorotwell input on this. Which direction do you think this should go (if any?).

@taylorotwell
Copy link
Member

I believe you can also already do @yield('title', 'Site Name');

Does that solve your problem?

@mark86092
Copy link
Contributor

@taylorotwell, the problem is something must show if title section exist, not your example something must show if title doesn't exist

@alexbowers
Copy link
Contributor Author

Yeah, the use case here for me is a conditional one.

@taylorotwell taylorotwell merged commit cb3187f into laravel:5.2 Mar 28, 2016
@taylorotwell
Copy link
Member

Renamed to @hasSection

@vedmant
Copy link

vedmant commented Mar 28, 2016

Great! Today I actually was searching for something like this, in my case I needed to put default HTML content for section, using second argument for @yield doesn't go easily with more complex HTML or if it has blade directives.

Btw, if somebody interested, I used this approach:

     @if (View::hasSection('profile-image'))
        @yield('profile-image')
     @else
        <div class="my-html-stuff">{{ $andMyData }}</div>
     @endif

@alexbowers
Copy link
Contributor Author

@taylorotwell Do you want a PR with @endhassection in it too? Or has that been added by yourself already / you're happy with @endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants