nunomaduro / termwind

🍃 In short, it's like Tailwind CSS, but for the PHP command-line applications.
MIT License
2.29k stars 78 forks source link

Injecting variable when rendering a template #124

Closed dib258 closed 2 years ago

dib258 commented 2 years ago

Hi,

I was recently trying to use Termwind with Laravel Zero to make a small cli/php app.

After trying to print the first layout with Termwind, I can't get my head around the possibility to inject a variable in the template before rendering it.

Either I miss something, either I'm in a specific case (Laravel Zero, which has striped a lot of functionality of Laravel).

  1. First attempt is with a nowdoc and trying different way to print write a variable but with no luck
use function Termwind\render;

$test = 'hello';
render(<<<'HTML'
        <div>
            <div class="px-1 bg-green-600">test : </div>
            <div class="ml-1">
                {$test}<br>
                $test<br>
                ${test}<br>
                {{ $test }}<br>
                <?php $test; <br>
            </div>
        </div>
HTML);

With this as an output :

 test : 
 {$test}
$test
${test}
{{ $test }}
  1. Second attempt : I tried to use the view helper with a blade file. Since in Laravel Zero there's no resources folder with blade files, I created thoses folder resources/views and the file termwind.blade.php with the template with the previous template. I found out that the view helper is present but there was no ViewServiceProvider.
    'providers' => [
        App\Providers\AppServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,
    ],

This was a good try but Laravel Zero just have a subset of laravel/framework. Just the Foundation folder not the View one. I could try to pull the whole laravel/framework but I think it's not the way to go. Here was the code I wanted to try :

use function Termwind\render;

$test = 'hello';
render(view('termwind', compact('test')));
  1. The last attempt : I've seen another way to build the HTML template with chained methods :
use function Termwind\div;
use function Termwind\render;

$test = 'hello';
div([
    div('test', 'px-1 bg-green-600'),
    div($test, 'ml-1'),
])->render();

I get the following error :

 Error

  Call to undefined function Termwind\div()

I am actually now out of idea to try this. Any help appreciated! I'm using :

Thanks for reading me and I'm hoping this can help others that want to use this wonderful tool you created !

jdreesen commented 2 years ago

Did you try it like this (Heredoc instead of Nowdoc)?

use function Termwind\render;

$test = 'hello';
render(<<<HTML   # <-- note the missing single quotes!
        <div>
            <div class="px-1 bg-green-600">test : </div>
            <div class="ml-1">
                {$test}<br>
                $test<br>
                ${test}<br>
                {{ $test }}<br>
                <?php $test; <br>
            </div>
        </div>
HTML);
dib258 commented 2 years ago

Oh my god thank you, what a sneaky nowdoc !

 test
 hello
hello
hello
{{ hello }}

Thank you for that fast answer, I should have searched more on that nowdoc notation.