Skip to content

[5.6] Add Blade::include for include aliases - #23172

Merged
taylorotwell merged 3 commits into
laravel:5.6from
sebastiandedeyne:blade-include-aliases
Feb 19, 2018
Merged

taylorotwell merged 3 commits into
laravel:5.6from
sebastiandedeyne:blade-include-aliases

Conversation

@sebastiandedeyne

Copy link
Copy Markdown
Contributor

This does the same as #22796, but for @includes. Useful when you have bits of html that don't have a "slot" (self-closing parts).

<input type="{{ $type ?? 'text' }}">
Blade::include('includes.input');
@input(['type' => 'email'])

@sebastiandedeyne sebastiandedeyne changed the title Add Blade::include for include aliases [5.6] Add Blade::include for include aliases Feb 15, 2018
@jmarcher

Copy link
Copy Markdown
Contributor

How dos this behaves if I make something like Blade::include('something.include') or any other blade reserved word?

@sebastiandedeyne

sebastiandedeyne commented Feb 15, 2018 •

Copy link
Copy Markdown
Contributor Author

Gonna need to dig in the compiler code for an answer on that question, I think custom directives get priority.

protected function compileStatement($match)
{
if (Str::contains($match[1], '@')) {
$match[0] = isset($match[3]) ? $match[1].$match[3] : $match[1];
} elseif (isset($this->customDirectives[$match[1]])) {
$match[0] = $this->callCustomDirective($match[1], Arr::get($match, 3));
} elseif (method_exists($this, $method = 'compile'.ucfirst($match[1]))) {
$match[0] = $this->$method(Arr::get($match, 3));
}
return isset($match[3]) ? $match[0] : $match[0].$match[2];
}

Note that this question shouldn't really affect this PR, since you can already register any custom directive using a reserved word.

@jmarcher

Copy link
Copy Markdown
Contributor

If that is the case that custom directives get priority then this should not affect anything. I was thinking about some undesirable behaviour like Blade::include('something.foreach')
@foreach('some text') and then Laravel expecting an @Endforeach

@taylorotwell

Copy link
Copy Markdown
Member

Pretty cool. Would be interested to know the answer to @jmarcher's question.

@sebastiandedeyne

Copy link
Copy Markdown
Contributor Author

Pushed some tests that confirm that behaviour.

@garygreen

Copy link
Copy Markdown
Contributor

Meh. Probably a bad example but I find the idea of loading micro pieces of HTML like that to be a pretty bad code smell.

Just imagine it.

@body
   @div(['class' => 'form-group'])
       @label(['text' => 'Name'])
       @input(['type' =>'name'])
    @enddiv
@endbody

Just an idea - would it be possible "inlining" some code from view files? So this:

Blade::inlineView('input', 'elements.input');
@input(['type' => 'email'])

Is compiled to be inline:

<?php extract(['type' => 'email']); ?>
<input type="{{ $type ?? 'text' }}">

@sebastiandedeyne

Copy link
Copy Markdown
Contributor Author

@garygreen Push any style to the extreme and it won't be productive anymore.

Regarding your second commend, I don't really see the difference between that an the includes we already have?

@garygreen

Copy link
Copy Markdown
Contributor

I'm just thinking performance - imagine outputting hundreds of inputs on a page, each one rendering a view for that one tiny piece of HTML. Would it not make more sense to "inline" those snippets into the original compiled view, somehow?

@taylorotwell
taylorotwell merged commit 3ea25dd into laravel:5.6 Feb 19, 2018
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.

4 participants