Jigsaw: Passing $page variable to @component

Created on 27 Mar 2018  路  2Comments  路  Source: tighten/jigsaw

My current build relies heavily on blade components & slots and within some of those there are references to the $page variable which is currently undefined when called inside @component template.

Currently I have to pass the variable every time I use a component.

Basic example:

Component

<!-- /source/_components/sharer.blade.php -->
<div class="container">{{ $slot }}</div>
<ul>
    <li>
        <a href="https://www.facebook.com/sharer/sharer.php?u={{ urlencode($page->getUrl()) }}">Share on Facebook</a>
    </li>
    <li>
        <a href="https://twitter.com/home?status={{ urlencode($page->getUrl()) }}">Share on Twitter</a>
    </li>
</ul>

Usage

<!-- /source/index.blade.php -->
@component('_components.sharer', ['page' => $page])
    <p>Some content here</p>
@endcomponent

Would be good if this was accessible without having to pass it as an attribute.

Thanks.

Most helpful comment

@raniesantos Yes it is possible, as of about a week ago, from PR https://github.com/tightenco/jigsaw/pull/204, thought hasn't been documented yet.

Add a blade.php file to your project, which will have access to a $bladeCompiler variable from which you can add component aliases, include aliases, and custom directives. For a component alias:

$bladeCompiler->component('_components.alert');

...would give you an @alert alias.

All 2 comments

Blade component-related question...

Is it possible to use Blade component aliases in Jigsaw or does it lack the Laravel components that enable this?

Reference:

https://sebastiandedeyne.com/posts/2018/blade-component-aliases-in-laravel-56

@raniesantos Yes it is possible, as of about a week ago, from PR https://github.com/tightenco/jigsaw/pull/204, thought hasn't been documented yet.

Add a blade.php file to your project, which will have access to a $bladeCompiler variable from which you can add component aliases, include aliases, and custom directives. For a component alias:

$bladeCompiler->component('_components.alert');

...would give you an @alert alias.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

royvanv picture royvanv  路  10Comments

mattstauffer picture mattstauffer  路  3Comments

slavarazum picture slavarazum  路  6Comments

Log1x picture Log1x  路  8Comments

dgursh picture dgursh  路  4Comments