douping3891 2016-09-02 01:57
浏览 28
已采纳

Laravel - 嵌套刀片回声

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条回答 默认 最新

  • douchai7891 2016-09-02 02:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败