12

My home page had some inline javascript that was mixed up with some blade syntax e.g.

    <script type="text/javascript">
        @if(Auth::user())
            if(path.indexOf('/user/' + {{Auth::user()->id}} ) != -1) {
                    $( "#tabs" ).tabs();
            };
        @endif
    </script>

It worked until I wanted to move the javascript to an external file.js. I got error whenever blade syntax was added. Is there a way I can fuse blade syntax in my javascript files.js? I tried renaming to file.blade.js with no luck...

4
  • Uncaught SyntaxError: Unexpected token ILLEGAL => @if Commented Aug 6, 2013 at 8:31
  • 1
    That actually works well for me. You should have made it an answer. I realised my home page was getting too cluttered with javascript. I believe this doesnt result in an extra request hence the page load speed is the same. Why would you recommend against it? Commented Aug 6, 2013 at 10:36
  • Not any specific reason, I just didn't get the use. Commented Aug 6, 2013 at 10:51
  • I find blade syntax easy. But the main problem is that my page was overloaded with javascript Commented Aug 6, 2013 at 13:38

4 Answers 4

42

Although the accepted solution will work, this is a most definitely an antipattern.

If I saw this not being the one who wrote it, I would be extremely confused to what's going on.

My suggestion is in your PHP file, have a block, which gets all of the values that you'll need in your external files, then call the external files.

So in your PHP file you would have something like:

<script>
    var userID = "{{ Auth::user()->id }}";
    var isUser = "{{ Auth::user() }}"
</script>

{{ HTML::script('path/to/js/file.js') }}

And in your javascript file:

if(isUser)
{
    if(path.indexOf('/user/' + userID ) != -1) {
        $( "#tabs" ).tabs();
    };
}
Sign up to request clarification or add additional context in comments.

6 Comments

plus 1 for seperating responsibilities and allowing me to minify my js if needed
@BillGarrison, keep in mind that yes, you can minify -- but you can't obfuscate because that will change variable names!
How would you use this solution and also be able to obfuscate? Can it be done? Any tricks that might allow obfuscation while preserving the specific variables?
You can ensure that the variable names userID and isUser won't be obfusticated by tying them to the global scope (like window.userID and window.isUser)... which is what you need to do anyway to be able to access those variables in a separate JS file. Obfuscators like UglifyJS should detect that a variable is accessing the global scope and won't obfusticate them by default. But if there's any uncertainty, then accessing it explicitly through window will make it clear.
I don't like this solution because it pollutes the global scope, what I usually do to pass variables to my JS is <input id="isUser" type="hidden" value="{{ Auth::user() }}" /> then just get it from JS using $('#isUser').val()
|
10

you can try this save your javascript file in app/views folder and rename it to xxx.blade.php , yes .blade.php because Blade Engine will parse it only if its .blade.php and use @include('your javascript filename') to include the javascript file parsed by Blade, it will work.

Comments

3

I was doing the same than @BrandonRomano, but I found a better approach. Sending directly the value from PHP to JS vars using: PHP-Vars-To-Js-Transformer

PHP:

JavaScript::put([
    'foo' => 'bar',
    'user' => User::first(),
    'age' => 29
]);

JS:

console.log(foo); // bar
console.log(user); // User Obj
console.log(age); // 29

You can set a namespace like:

console.log(server.foo)

Comments

1

You are outputing string from PHP, so you have to enclose that string in '

<script type="text/javascript">
    @if(Auth::user())
        if(path.indexOf('/user/' + '{{Auth::user()->id}}' ) != -1) {
                $( "#tabs" ).tabs();
        };
    @endif
</script>

3 Comments

I am getting an error: Uncaught SyntaxError: Unexpected token ILLEGAL => @if
What is the name of the file? It should be named whatever.blade.php
It is a .blade.php file but the error disappeared when I enclosed the @if statements with in ' too. like '@if(Auth::user())'. Problem is, the blade php didnt run

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.