Support the ongoing development of Laravel.io →
Views Blade
Last updated 1 year ago.
0

Show us your code.

0

Short Answer

It's not processing the tag because it's not in a blade file. You have to pass it in another way.

Longer Answer

Blade is a template system. Blade is reading in the .blade.php file you specified and parsing it. It will find appropriate tags and replace them with equivalent PHP code in the background and cache this elsewhere. While it does dive in deeper when using @include, it does not read in files referenced by other actual HTML tags, such as JavaScript (<script>) or CSS (<link>) files.

What you CAN do is make some sort of name spaced object to store variables that gets set before that script is loaded.

Potential solution example

Here's an idea of what I mean. You can put THIS in your blade template. Forgive me if this doesn't work exactly but hopefully you get the idea. I'm typing this blindly on the train:

<script>
// Some sort of name space (like company or site name) to prevent colliding
var chriz = {
    vars: {
        some_value: {{ number_format($size->it,1) }}
        another_value: {{ ... }}
    }
};
</script>
<!-- in the below script, replace the blade text with chriz.vars.some_value
<script src="js/yourscript.js"></script>

And that's it! You'll have your value passed into javascript land while you keep your logic in the single file.

Similar solution

I can't provide you code on how to do this or know if this is recommended behavior, but I've seen JSON encoded data put in as a data element on the html or body tag. like:

<html data-jsdata="{ var1: 'foo', var2: 'bar', a_number: 42 }">

And this value may be provided by echoing out data through blade on your main template. Then you come up with a system to set JavaScript values within your controller that end up in that JSON encoded value. Again maybe there are pitfalls to this method, but if you do find a way to implement this, then you'll have an easy to use solution that is global to your entire app and not have to rewrite the above. You'd just need to use your namespaced variable in your javascript files.

Last updated 8 years ago.
0

won't work, I don't know my vars before post. What about this idea, have the callback function load a separate blade.php file that contains the string to pass to the view. This way blade should parse it.

0

I'm confused. What do you mean you "don't know your vars before post"? If you're able to print it out with the javascript embedded into the blade.php file, you already have them.

You're simply setting them on a variable that's embedded in the .blade.php instead. This variable is then accessed by the code in the .js file instead of using the blade template language.

Edit: Ok I see you're doing an Ajax call now. Isn't THAT where you would want to set the variable then? Either the return call is json that has this set, OR if you're rendering it, just put that in the variable in the data you're rendering.

Last updated 8 years ago.
0

I tried your solution

var chriz = { vars: { some_value: {{ number_format($size->it,1) }} another_value: {{ ... }} }

doesn't work, error "size" not defined. In the script part in the callback function I define size as index like foreach sizes as size ... so here there is no role for $size, but javascript doesn't treat it as text but as a variable

Last updated 8 years ago.
0

If you're doing it correctly, this shouldn't be seen by javascript at all:

Controller

public function your_entrypoint()
{
    /* ... */
    /* $size gets defined in your controller somehow */
    return view('view_file')->with('size', $size);
    /* Or you can use the array style like view('view_file', ['size' => $size]) */
}

View

<script>
// Some sort of name space (like company or site name) to prevent colliding
var chriz = {
    vars: {
        some_value: {{ number_format($size->it,1) }}
    }
};
</script>

JavaScript File

// The below will set the variable to whatever was in $size->it with number_format() around that
var some_variable = chriz.vars.some_value;

Please note that for the final product, don't use chriz, or some_variable or some_value or anything like that. They are generic as I don't know what the purpose of your script is, and I am showing a vague practice

Last updated 8 years ago.
0

You are not reading my messages, I am not returning a view, I am returning a json and updating the view on the fly.

thomaswardiii said:

If you're doing it correctly, this shouldn't be seen by javascript at all:

Controller

public function your_entrypoint()
{
   /* ... */
   /* $size gets defined in your controller somehow */
   return view('view_file')->with('size', $size);
   /* Or you can use the array style like view('view_file', ['size' => $size]) */
}

View

<script>
// Some sort of name space (like company or site name) to prevent colliding
var chriz = {
   vars: {
       some_value: {{ number_format($size->it,1) }}
   }
};
</script>

JavaScript File

// The below will set the variable to whatever was in $size->it with number_format() around that
var some_variable = chriz.vars.some_value;

Please note that for the final product, don't use chriz, or some_variable or some_value or anything like that. They are generic as I don't know what the purpose of your script is, and I am showing a vague practice

0

thomaswardiii said:

I'm confused. What do you mean you "don't know your vars before post"? If you're able to print it out with the javascript embedded into the blade.php file, you already have them.

The ajax function gets data from the server and return the json so for example I don't have the $size variable before hitting the submit button hence I can't embed those variables in the blade template as all I get is an undefined variable $size"

0

Sign in to participate in this thread!

Eventy

Your banner here too?

chriz74x chriz74x Joined 8 Apr 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.