Introduction to Hyde Data Collections#
Hyde provides DataCollections
, a subset of Laravel Collections giving you
a similar developer experience to working with Eloquent Collections. However, instead of accessing a database,
it's all entirely file-based using static data files such as Markdown, Yaml, and JSON files which get
parsed into objects that you can easily work with.
As you have access to all standard Laravel Collection methods, you are encouraged to read the Laravel Collections documentation for more information.
This article covers advanced usage intended for those who are writing their own Blade views, and is not required as Hyde comes pre-packaged with many templates for you to use.
High-Level Concept Overview#
To make collections easy to use and understand, Hyde makes a few assumptions about the structure of your collections. Follow these conventions and creating dynamic static sites will be a breeze.
- Collections are accessed through static methods in the
DataCollections
class. - Collections are stored as files in subdirectories of the
resources/collections
directory. - To get a collection, specify name of the subdirectory the files are stored in.
- Data will be parsed into differing objects depending on which facade method you use. See the table below.
- The class is aliased so that you can use it in Blade files without having to include the namespace.
- While not enforced, each subdirectory should probably only have the same filetype to prevent developer confusion
- Unlike source files for pages, files starting with underscores are not ignored.
- You can customize the source directory for collections through a service provider.
- If the base source directory does not exist, it will be created for you.
Available Collection Types#
Quick Reference Overview#
The following facade methods for creating data collections are available:
1\Hyde\Support\DataCollections::markdown(string $name);2\Hyde\Support\DataCollections::yaml(string $name);3\Hyde\Support\DataCollections::json(string $name, bool $asArray = false);
Quick Reference Table#
Collection Type | Facade Method | Returned Object Type | File Extension |
---|---|---|---|
Markdown | ::markdown() |
MarkdownDocument |
.md |
Yaml | ::yaml() |
FrontMatter |
.yaml , .yml |
Json | ::json() |
stdClass OR array |
.json |
Markdown Collections#
Usage#
1$collection = \Hyde\Support\DataCollections::markdown('name');
Example returns#
Here is an approximation of the data types contained by the variable created above:
1\Hyde\Support\DataCollections {2 "testimonials/1.md" => Hyde\Markdown\Models\MarkdownDocument3 "testimonials/2.md" => Hyde\Markdown\Models\MarkdownDocument4 "testimonials/3.md" => Hyde\Markdown\Models\MarkdownDocument5 ]6}
The returned MarkdownObjects look approximately like this:
1\Hyde\Markdown\Models\MarkdownDocument { 2 +matter: Hyde\Markdown\Models\FrontMatter { 3 +data: array:1 [ 4 "author" => "John Doe" 5 ] 6 } 7 +markdown: Hyde\Markdown\Models\Markdown { 8 +body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit..." 9 }10}
Assuming the Markdown document looks like this:
1---2author: "John Doe"3---4 5Lorem ipsum dolor sit amet, consectetur adipiscing elit...
YAML Collections#
Usage#
1$collection = \Hyde\Support\DataCollections::yaml('name');
Example returns#
Here is an approximation of the data types contained by the variable created above:
1\Hyde\Support\DataCollections {2 "authors/1.yaml" => Hyde\Markdown\Models\FrontMatter {3 +data: array:1 [4 "name" => "John Doe",6 ]7 }8}
Assuming the Yaml document looks like this:
1---2name: "John Doe"
Note that the Yaml file should start with
---
to be parsed correctly.
Json Collections#
Usage#
1$collection = \Hyde\Support\DataCollections::json('name');
By default, the entries will be returned as stdClass
objects. If you want to return an associative array instead, pass true
as the second parameter:
1$collection = \Hyde\Support\DataCollections::json('name', true);
Since both return values use native PHP types, there are no example returns added here, as I'm sure you can imagine what they look like.
Markdown Collections - Hands-on Guide#
I think the best way to explain DataCollections is through examples, so let's create a Blade page with customer testimonials!
This example will use Markdown Collections, but the same concepts apply to all other collection types.
Setting up the file structure#
We start by setting up our directory structure. We will create a testimonials
subdirectory, which will be the collection name.
In it, we will place Markdown files. Each file will be a testimonial.
The Markdown will be parsed into a MarkdownDocument
object which parses any optional YAML front matter.
Here is the sample Markdown we will use:
Filepath: resources/collections/testimonials/1.md1---2author: John Doe3---4 5Lorem ipsum dolor sit amet, consectetur adipiscing elit...
Let's take a look at our directory structure. I just copied the same file a few times. You can name the files anything you want, I kept it simple and just numbered them.
1resources/collections2└── testimonials3 ├── 1.md4 ├── 2.md5 └── 3.md
Using the Facade to Access the Collections#
Now for the fun part! We will use the DataCollections::markdown()
to access all our files into a convenient object.
The class is registered with an alias, so you don't need to include any namespaces when in a Blade file.
The general syntax to use the facade is as follows:
1DataCollections::markdown('subdirectory_name')
This will return a Hyde DataCollections object, containing our Markdown files as MarkdownDocument objects. Here is a quick look at the object the facade returns:
^ Hyde\Support\DataCollections {#651 ▼ #items: array:3 [▼ "testimonials/1.md" => Hyde\Markdown\Models MarkdownDocument {#653 ▼ +matter: Hyde\Markdown\Models FrontMatter {#652 ▶} +markdown: Hyde\Markdown\Models Markdown {#654 ▶} } "testimonials/2.md" => Hyde\Markdown\Models MarkdownDocument {#656 ▶} "testimonials/3.md" => Hyde\Markdown\Models MarkdownDocument {#659 ▶} ] }
Implementing it in a Blade view#
Let's create a Blade page to display all our testimonials.
1php hyde make:page "Testimonials" --type="blade"
And we can use the collection almost like any other Laravel one. As you can see, since each entry is a MarkdownDocument
class,
we are able to get the author from the front matter, and the content from the body.
Filepath: _pages/testimonials.blade.php1@foreach(DataCollections::markdown('testimonials') as $testimonial)2 <blockquote>3 <p>{{ $testimonial->body }}</p>4 <small>{{ $testimonial->matter['author'] }}</small>5 </blockquote>6@endforeach