Tom
Tom

Reputation: 3664

Laravel 4 - extending blade, problems with command without brackets

I want to extend blade in laravel 4 to have some custom commands and I want to call them without brackets. For example I want to call:

@test

So I've created Blade::extend function like so:

Blade::extend(function($view, $compiler)
{
    $pattern = $compiler->createMatcher('test');

    return preg_replace($pattern, '$1<?php echo "test"; ?>', $view);
});

It works fine when I call it with:

@test()

But fails when I call it with just:

@test

How can I achieve this?

Upvotes: 1

Views: 407

Answers (1)

Tom
Tom

Reputation: 3664

The thing was with preg_replace pattern. Diving into BladeCompiler I found out that it has more that one createMatcher method.

To call

@test

without brackets, just change

$pattern = $compiler->createMatcher('test');

to:

$pattern = $compiler->createPlainMatcher('test');

which creates proper pattern in that case.

Upvotes: 2

Related Questions