我们可以创建一个AssetListingPage页面,可以遍历assets目录的内容并显示所有文件夹和文件链接.
在AssetListingPage控制器中,我们有一个RootAssets函数,它将返回assets目录中的文件和文件夹列表.
AssetListingPage.php
class AssetListingPage extends Page {
}
class AssetListingPage_Controller extends Page_Controller {
public function RootAssets() {
return File::get()->filter('ParentID', 0);
}
}
Run Code Online (Sandbox Code Playgroud)
我们为AssetListingPage这些循环添加了一个布局模板RootAssets.
模板/布局/ AssetListingPage.ss
<div class="content-container">
<h1>$Title</h1>
$Content
<% if $RootAssets %>
<ul>
<% loop $RootAssets %>
<% include AssetList %>
<% end_loop %>
</ul>
<% end_if %>
</div>
Run Code Online (Sandbox Code Playgroud)
我们添加一个包含模板AssetList以递归列出文件夹或文件并列出所有子项.
模板/包括/ 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
118 次 |
| 最近记录: |