pug-php / pug

Pug template engine for PHP
https://www.phug-lang.com
MIT License
387 stars 42 forks source link

Compatibility problem with laracast #234

Closed MindFlare56 closed 4 years ago

MindFlare56 commented 4 years ago

Hello,

I encountered an issue with the following code:

include phpvars
    script(type='text/javascript').
      $(document).ready(function () {
        console.log('laracast Javascript::put example: ' + phpvars.age);
      })

I expected it to generate the html in order to make laracast work like in this exemple: http://bonstutorial.com/laravel-transform-variable-from-controller-to-javascript/

@include('phpvars') 
<script type="text/javascript">   
    $(document).ready(function(){     
        console.log('laracast Javascript::put example: ' + phpvars.age);
    })
</script>

But I actually the include doesn't seem to do the same... So I was wondering if there was an existing solution for this or else if it was possible to include a blade file inside pug in order to fix this problem

Thanks!

kylekatarnls commented 4 years ago

Hello,

You script has no reason to be nested inside the include if it's only PHP vars as it sounds to be.

include phpvars
script(type='text/javascript').
  $(document).ready(function () {
    console.log('laracast Javascript::put example: ' + phpvars.age);
  });

Then laravel-pug engine is the recommended way to use Pug inside Laravel: https://github.com/BKWLD/laravel-pug

And finally I need you to precise "But I actually the include doesn't seem to do the same..." with the exact output you get in order to help further. And it may also depends on the content of phpvars, but I can't guess it as I don't know what it is.

Thanks.

MindFlare56 commented 4 years ago

It was a copy paste error it is not nested into it

In fact it may be a laracast compatibility issue rather than pug https://github.com/laracasts/PHP-Vars-To-Js-Transformer

when the file is included with blade laracast generate the following to be able to pass value from php to javascript. The phpvars file is empty but it is needed for the generation.

<script>window.phpvars = window.phpvars || {};phpvars.foo = "bar";phpvars.age = 29;</script>
kylekatarnls commented 4 years ago

This sounds like an easy job for View::share() with no need for extra package:

// In service or controller:
View::share('phpvars', [
  'age' => 22,
]);
script(type='text/javascript').
  window.phpvars = !{json_encode(phpvars)};

  $(document).ready(function () {
    console.log('laracast Javascript::put example: ' + phpvars.age);
  });

Is there a particular reason to use this package?

MindFlare56 commented 4 years ago

Oof... That works perfectly! Thanks a lot!