如何使用 SilverStripe 显示文件存档

How to display a File Archive with SilverStripe

我想在我的网站上显示一个包含文件和子目录的目录。我找不到任何示例。

有谁知道如何列出所有文件和目录?

我们可以创建一个 AssetListingPage 页面,可以循环遍历资产目录的内容并显示所有文件夹和文件链接。

AssetListingPage 控制器中,我们有一个 RootAssets 函数,它将 return 资产目录中的文件和文件夹列表。

AssetListingPage.php

class AssetListingPage extends Page {
}

class AssetListingPage_Controller extends Page_Controller {

    public function RootAssets() {
        return File::get()->filter('ParentID', 0);
    }

}

我们为 AssetListingPage 添加了一个循环遍历 RootAssets 的布局模板。

templates/Layout/AssetListingPage.ss

<div class="content-container">

    <h1>$Title</h1>

    $Content

    <% if $RootAssets %>
    <ul>
        <% loop $RootAssets %>
        <% include AssetList %>
        <% end_loop %>
    </ul>
    <% end_if %>

</div>

我们添加了一个包含模板 AssetList 以递归地列出文件夹或文件并列出所有子文件。

templates/Includes/AssetList.ss

<li class="$ClassName">
<% if $ClassName == 'Folder' %>
    $Title
    <% if $Children %>
    <ul>
        <% loop $Children %>
        <% include AssetList %>
        <% end_loop %>
    </ul>
    <% end_if %>
<% else %>
    <a href="$Link">$Name</a>
<% end_if %>
</li>