Global Variables with Sage 9 & Blade

I’m currently converting a personal project I’ve been working on to Sage 9.

I have a couple classes that I initialize and then global their variables in each template that I need them in, but using Blade I feel that there’s a better way to accomplish it.

Using Google, I came across https://stackoverflow.com/questions/29715813/laravel-5-global-blade-view-variable-available-in-all-templates which would let me use my variable across all blade’s without manually globaling them, but I’m curious if there’s a way with the current implementation to do something like this? I understand what they are saying and have done it before inside of Laravel, but I’m unsure how to accomplish it with Sage 9’s use of Blade.

Any insight on this would be greatly appreciated as I’d like to make everything as clean as possible.

Thanks!

sage('blade')->share( ... )

Just make sure it’s after Blade is instantiated, i.e., after_setup_theme.

1 Like

Thanks for the quick reply.

I’m currently trying to do a messy quick implementation just to get it working.

https://hastebin.com/nigiheqaka.php

I’m assuming I don’t have the proper namespaces?

I tried using Roots\Sage\Template\Blade, etc. but am getting https://log1x.com/screenshots/2016-12-30_18-53-12_BTUL6.png

I’m assuming I’m making an obvious mistake.

Appears to work using init instead of after_setup_theme.

It looks like a decent amount of variables aren’t accessible inside of blades like sidebar.blade.php such as $post, $shortcode_tags, etc. so I’m guessing we just add them using sage('blade')->share()?

I also couldn’t figure out a way to get my comment template to render using blade inside of a Walker_Comment callback.

I tried \App\template(locate_template('templates/partials/comments-single-reviews.blade.php')); but since it’s not using include I’m unable to pass the variables inside of html5_comment. I tried putting them into an array and passing it with $data but they still weren’t accessible.

Example: https://hastebin.com/laxamowayo.php

Any way to make this work or is it not sane since to do?

Edit init would be fine-- but it’s too soon to be able to get $post.

Edit 2 after_setup_theme works fine. I just had it in an include that seemed to of fired it before blade was initialized. Still not able to get $post though.

I’m also unsure how to get global variables such as $_POST inside of a blade view.

Sorry for being a nuisance, but I can imagine these will be common questions in the future.

Perhaps we need Illuminate\Http\Request for it to work as seen here?

Unless I’m missing an alternative method that is already implemented.

It can of course be done using sage('blade')->share('_POST', $_POST); but that will result in quite the mess without something more native.

While blade syntax and all possibilities it gives is great - wouldn’t switch back to plain old PHP on corner cases like Yours would be better time-wise?

I’m not on a time constraint with this project and am sucker for the clean code that is possible with Blade. :slight_smile:

Most of the issues above have been solved other than the inability to fetch $post without globaling it (i.e. $GLOBALS['post'], but that is not a major issue by any means.

For records sake, to properly include:

\App\template(locate_template('templates/partials/comments-single-reviews.blade.php'));

in something such as a Comment_Walker you would instead do:

include \App\template_path(locate_template('templates/partials/comments-single-reviews.blade.php'));

to allow the passing of all variables within’ the class.

Also, after_setup_theme worked just fine when using sage('blade')->share() within’ setup.php.

An example would be:

/**
 * Share variables to Blade views
 */
sage('blade')->share('options', $GLOBALS['options']);
sage('blade')->share('review', $GLOBALS['review']);
sage('blade')->share('shortcodes', $GLOBALS['shortcode_tags']);

which would then allow you to simply use $options, $review and $shortcodes within’ your blades without globaling.

Otherwise, everything is working great and Blade is a treat to use. :smile:

2 Likes

I know this is old, can you expand on how you solved this? For example, I have a variable that sets the template width and would like to print that out on all my containers. But, since I am pulling in nav, top, bottom, etc. seperate blade files, they don’t get the variable unless I place it individually within each file.

The proper way to do this now would be to use a Controller.

<?php
namespace App;
use Sober\Controller\Controller;

class App extends Controller
{
    public function width()
    {
        $width = '??';
        return $width;
    }
}

and then use {{ $width }} in your blades.

1 Like

Hey, working on the same issue if I understood correctly. But this doesnt say anything about getting
global $post in the controller?
For example this:

return wp_list_pages(array(
        'child_of' => $post->post_parent, 
        'depth'    => 1,
        'echo'     => false
    ));

$post in here is not available even though using

sage('blade')->share('post', $GLOBALS['post']);

in setup.php in after_theme_setup hook as you suggested.

Any tips appreciated @Log1x :slight_smile:

Edit: this works if you of course add
global $post; too before using $post variable.

Seems It does not even need sage('blade')->share()