Create custom Blade directive

Hi,

Can’t find a way to add a custom directive. I’d like to add something like @dump($var) (“debug()” doesn’t work at all, seems like a reserved word) to be a shortcut for PC::debug($var) using PHP console: https://github.com/barbushin/php-console

Tried half a dozen ways to add it, no luck! Even tried to duplicate this piece of code: https://github.com/roots/sage/blob/8691074b29c4c09dae40f0f0c33dc2b51d9292e2/app/setup.php#L125

Any idea? Thanks.

Are you getting any errors? Blade keeps a cache in your uploads/cache folder with compiled templates, so if you previously had a problem with your directive, it might not get updated when you change your directive definition. I’d suggest emptying that directory as a start and trying again.

I haven’t used PHP Console so I don’t know if there are extra requirements but here’s a quick directive I got working by adding the following to app/setup.php inside the after_setup_theme hook:

sage('blade')->compiler()->directive('dump', function ($var) {
        return "<pre>DUMP RESULT:\n\n<?php print_r({$var}); ?></pre>";
});

Then in your Blade file you can do something like:

@dump($myVar)

You just have to remember when creating a directive that you’re printing out actual PHP code that will be executed later, which is a bit strange at first…

Your code works, thanks:

sage('blade')->compiler()->directive('debug', function ($var) {
    return "<?php PC::debug({$var}); ?>";
});

To be used like this:

@debug($var)

I’m trying to get this piece of code out of setup.php to keep it safe from future updates of Sage itself. How could I access sage() in another file?

I’m also struggling with this≥ .

Added directives.php, included in functions,.php (where setup, helpers, filters are added etc) . but putting them there, nothing happens, my directives in templates are printed straight to the screen.

I have included the same namespace and use statements from setup.php but no dice.

In order to work correctly, you need to wrap your directive creator in an add_action() for after_setup_theme (the action it uses in setup.php). I managed to get it working by doing the following:

// app/debug.php
// This is a file I created.

<?php
namespace App; // Only the namespace is required; don't need to `use` anything.

add_action('after_setup_theme', function () {
    // @debug is already used by soberwp/controller
    sage('blade')->compiler()->directive('debugtest', function ($var) {
        return "<?php var_dump({$var}); ?>";
    });
});
// functions.php

array_map(function ($file) use ($sage_error) {
    $file = "../app/{$file}.php";
    if (!locate_template($file, true, true)) {
        $sage_error(sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file), 'File not found');
    }
}, ['helpers', 'setup', 'debug', 'filters', 'admin']); // add my new `debug` file
// views/layouts/app.php
@debugtest(get_option('siteurl'))

// string(15) "http://sand.box" 
3 Likes

I have them wrapped maybe having the use statements is the issue?

Can you post your code? The use statements shouldn’t cause the problem you’re describing.

just figured it out, had a typo in the action :confused: .

Works great! Thanks. Here’s my final code:

namespace App;

add_action('after_setup_theme', function () {
  sage('blade')->compiler()->directive('debug', function ($var) {
    return "<?php if(class_exists('PC')) { PC::debug({$var}); } ?>";
  });
});

Using this WP plugin: https://wordpress.org/plugins/wp-php-console/