-1

I am using Bolt CMS and want to have a table including all files of a specific directory on the server. How can this be achieved using a Twig template?

walderich
  • 500
  • 1
  • 9
  • 24
  • can you give more details, e.g. what you've tried so far, what your output would look like etc – duncan Feb 10 '15 at 17:48
  • There are audio files in that directory and I want to create links to them, so visitors can listen to the files I think the parsing is no problem, I just need to know how it works. I need something like this: `PartOfFileNameHere`. Actually the only thing I found on the net was on the Twig homepage stating there is a `Twig_Loader_Filesystem` class. But I'm new to twig and `{{ fs = new Twig_Loader_Filesystem('/files') }}` or `{{ set fs = Twig_Loader_Filesystem('/files') }}` does not seem to work. – walderich Feb 10 '15 at 18:53

2 Answers2

1

I have found that the following call works. The argument is a path in the files directory.

{% set files = app.filesystem.browse('SomeDirectory')[0] %}

Fred
  • 706
  • 7
  • 6
  • This actually works quite well for me and now I can finally abandon the custom extension. I only struggled a bit, as the root directory was under "/files" and not "/". But that also worked out well. Thanks for this! – walderich Dec 25 '15 at 11:18
  • Update: In Bolt 3 the interface slightly changed, now you need to use `{% set files = app.filesystem.listContents('files://SomeDirectory') %}` – walderich Jun 05 '16 at 11:08
0

This is not twig's task. This is something the controller layer should be responsible.

Get the list of files in your controller (though I'd probably create a model-layer class for this), and pass the list to your view, where it only needs to display it.

Maerlyn
  • 33,687
  • 18
  • 94
  • 85
  • And the "controller level" in Bolt-CMS is Symphony, right? How can I extend this? Do you have an example or a link to the documentation? Sorry, I'm really new with Bolt and only have performed some very basic tasks. – walderich Feb 11 '15 at 19:14
  • The site says silex, so yeah, effectively symfony. Never used bolt so cannot help, sorry. – Maerlyn Feb 11 '15 at 22:53
  • I solved this by creating a little bolt extension which returns a list of files from a directory. Like that I only need to create links from the file list in Twig. – walderich Jun 04 '15 at 14:10