1

I am using a blade echo inside of a stylesheet reference in my header section. This is for specifying a CSS for a skin to the site.

The original line is:

<link href="{{ asset("/dist/css/skins/skin-default.min.css"}}" rel="stylesheet" type="text/css\" />

I am grabbing a skin setting per user and want to insert it into that line to change what the user wants as their skin. I have the setting pulled out into a variable from within a view service provider.

$view->with('visualSkin', Auth::user()->visualSkin);

This way I have $visualSkin set when all views are rendered.

How can I insert that into the asset href above and default it to a setting if the user has none set?

I can call the variable or set a default by doing:

{{ $visualSkin or "skin-default" }}

However, how can I inline that section with my asset href? Something like this does not work:

<link href="{{ asset("/dist/css/skins/{{ $visualSkin or "skin-red-trim" }}.min.css"}}" rel="stylesheet" type="text/css\" />

I've also tried some php trickery, but since it's blade formatting when inserting it into php echo, it doesn't get rendered by the blade process.

Can you nest blade echos?
Am I missing some character escaping?

1 Answer 1

5

You don't need nesting! Inside blade tags, PHP is running, so you can concatenate strings naturally.

<link href="{{ asset("/dist/css/skins/" . $visualSkin or "skin-red-trim" . ".min.css")}}" rel="stylesheet" type="text/css\" />

Note that was missing the ) to close the asset( function.


Using {{ 'something' }} is the same that type: echo 'something'

So anything that you can do with an echo, you can do on the blade echo tags.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok. A late night falling-asleep mistake. I was using "+" instead of "." for the concatenation. The ) missing was just a copy/paste issue when posting this. It's in the code now. The error now is that $visualSkin isn't defined. I thought the "or" would take care of that? Or do I need to check with isset?
Correct me if I'm wrong, but wouldn't $visualSkin or "skin-red-trim" in standard PHP evaluate to a boolean (in this case, true)? Not the actual value? I think you want $visualSkin ?: "skin-red-trim".
That should be correct. The "or" would be plain blade templating if it were by itself. Now that it's PHP inside of the blade, the "or" won't work as expected.
Ok. In my view provider, I'm checking if the user is auth'd. If so, get their settings including the $visualSkin. When you hit a page that doesn't have the user logged in, but is still referencing that asset href, it throws an error because $visualSkin has never been set. I guess I can default that var to something like $visualSkin = false; ? That would initialize it.

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.