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
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…
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