首页 > 解决方案 > OctoberCMS:如何从 Twig 调用 Storage::url()?

问题描述

在 Laravel 5.5 中,我使用以下函数从文件系统或云中调用资产:

<link href="{{ Storage::url('assets/my.min.css') }}" rel="stylesheet">

我知道我可以使用<img src="{{ 'banner.jpg'|media }}" />,但这迫使我只能使用媒体管理器。在 Laravel 中,我可以通过 alter in 在本地文件系统和云之间切换'default' => env('FILESYSTEM_DRIVER', 'local'),config/filesystems.php非常方便。

这就是为什么我的问题是我如何Storage::url()在 twig 中使用 OctoberCMS?

标签: octobercms

解决方案


在树枝中都取决于主题,所以你可以使用theme过滤器

包含您可以使用的 js 文件

<script type="text/javascript" src="{{ 'assets/js/menu.js'|theme }}"></script>

它将从当前主题中获取 url,然后assets/js/menu.js 对于 css 相同

<link href="{{ 'assets/my.min.css'|theme  }}" rel="stylesheet">

更多信息:https ://octobercms.com/docs/markup/filter-theme

并包含 js 和 css 组件,您可以使用帮助程序https://octobercms.com/docs/plugin/components#component-assets

// inside component
public function onRun()
{
    $this->addJs('/plugins/acme/blog/assets/javascript/blog-controls.js');
    $this->addCss('assets/css/hello.css');
}

您可以添加然后使用它的插件也是如此。

如有任何疑问,请发表评论。


推荐阅读