Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

spacedog4's avatar

How can I user Mustache (or another option) with blade templates?

I have this example template file

// example.template

My name is {{ $name }} // I want to keep this braces

Company {{ $company }} // But this one I want to replace later

mustache file

$mustache = new Mustache_Engine;

$mustache->render('example.template', [
    'company' => 'My Company'
]);

output

My name is

Company My Company

I don't want to do things like '$name' => '{{ $name }} cause I have so many routes with braces in my file that it would be terrible to write

0 likes
17 replies
Cronix's avatar

I don't think it's possible to use both blade and mustache in the same file. How would any parser know {{ }} this one belongs to blade, but {{ }} this other one should be rendered by mustache

spacedog4's avatar

@Nash I use blade for my views, but I want to use mustache generage views, so I can change page name, routes, but mantain the blade variables

spacedog4's avatar

@tykus But I want to mustache ignore, exist an option like that for mustache?

Nash's avatar

I user blade for my views, but I want to use mustache generage views, so I can change page name, routes, but mantain the blade variables

@spacedog4 What is it that you're trying to achieve with Mustache that can't be done with Blade?

click's avatar

So you are using the php variant of mustache? What are you exactly trying to achieve? I think you are making it yourself quite complicated.

spacedog4's avatar

@click I'm creating a command to generate a view based on a model, so It reads a template, search the columns from the table for that model and place it on the template

spacedog4's avatar

I think I'll need to create a helper class, I created this one


class MustacheBlade {

    private $context;

    public function __construct($context = [])
    {
        $this->context = $context;
    }

    public function __isset($name)
    {
        return true;
    }

    public function __get($name)
    {
        if (!array_key_exists($name, $this->context)) {
            if (substr($name, 0, 1) === '@') {
                $str = ltrim($name, '@');
                $str = '{{ ' . $str . ' }}';
                return $str;
            } else {
                return false;
            }
        }

        return $name;
    }
}

and calls this way


$mustache->render("Teste {{ foo }} {{ @bar }} {{ foobar }}", new MustacheBlade([
      'foo' => 'hello'
]));

output


Test hello {{ bar }}

click's avatar
click
Best Answer
Level 35

Ok. So if I understand you correctly. You are generating *.blade.php files based on a stub file or something. Inside this generated blade.php file you want to have {{ $name }} variables. But you also want to enter some strings in your generated blade.php files. Am I seeing this correct?

If that is the case there is no need for a second parser I think.

If your stub files looks like this:

<div> 
    This is an auto generated blade file for model {{ $modelClass }} on {{ $createdOn }}<br />
        The id of this model is @{{ $model->id }}
</div>

If you now create from this stub file a real blade file with pseudo code like:

$bladeContent = view()->make('path.to.stub', [
    'modelClass' => \App\User::class,
    'createdOn' => date()->format('Y-m-d H:i:s'),
])->render(); 

// if your stub is not in the view directory I think you can also use file() instead of make()

// save the generated content in your blade file on the location you want

At this moment your generated blade file should look like this:

<div> 
    This is an auto generated blade file for model \App\User on 2018-08-02 19:19:00<br />
        The id of this model is {{ $model->id }}
</div>
1 like
spacedog4's avatar

@click hmmmm very interesting, I don't need to use mustache, but I got this error Call to undefined method Illuminate\View\Factory::render()

And Thank you so much for you time

my file

$content = view()->render("test.stub", [
    'foo' => 'hello'
])->toHtml();

dd($content);
Cronix's avatar

think it would be

$bladeContent = view('path/to/stub/', [
    'modelClass' => \App\User::class,
    'createdOn' => date()->format('Y-m-d H:i:s'),
])->render(); 

basically everything that was in render() should go in view(). Don't think you need ->toHtml(), since the rendered file should return html.

click's avatar

Typo, I didn't test my code. This should the syntax be. I will update my answer above

$html = view()->make('view', [ ])->render();
1 like
spacedog4's avatar

Perfect, I create a namespace in my AppService provider, so I can keep my template in my command folder

view()->addNamespace('template', app_path().'/console/commands/templates/views/admin/');
$content = view()->make('template::test', [
    'foo' => 'hello'
])->render();

THANK YOU SO MUCH

Please or to participate in this conversation.