PAGENODE

About Documentation Github

Selecting Content

Nodes in Pagenode can be selected with a simple query interface. You first create a Selector for a particular directory of nodes and then apply a query function that filters and sorts nodes.

To create a selector for the nodes/ directory simply call select() with the path:

$selector = select('nodes/');

You can then filter and sort the Nodes with various methods. The newest(), oldest() and query() methods will return an array of PN_Node instances, while the one() method will return just one PN_Node instance or null if a matching node cannot be found.

Some examples:

Load a single node whose keyword is 'hello' - i.e. the 'nodes/hello.md' file:

$helloNode = $selector->one(['keyword' => 'hello']);

Load the 5 newest nodes that are tagged with javascript and web:

$jsNodes = $selector->newest(5, ['tags' => ['javascript', 'web']]);

Load all nodes that have the foo key set to bar in the Node header:

$fooNodes = $selector->newest(null, ['meta' => ['foo' => 'bar']]);

If you have a lot of Nodes, you may want to paginate. To select 3 Nodes, starting after the first 6:

$pagedNodes = $selector->newest(3, ['page' => 2]);

To get the total number of nodes that matched the last query (before limiting and pagination), you can call foundNodes().

$totalNodes = foundNodes();

You can of course combine multiple filter options:

$filteredNodes = $selector->newest(5, [
    'meta' => ['foo' => 'bar'],
    'date' => [2018, 10],
    'tags' => ['javascript', 'web']
]);

Or apply a custom filter function. This one searches for the 10 newest nodes whose title matches a search string:

$matchingNodes = $selector->newest(10, ['filter' => function($node) {
    $matches = stristr($node['title'], $_GET['query']) !== false;
    return $matches;
}]);

Oftentimes it's more convenient to create the selector and query it immediately, instead of assigning the selector to a variable first. I.e. you can just do this:

$nodes = select('nodes/')->newest();

Please refer to the Selector API Reference for the complete list of all selector methods and parameters.

Up Next: Routing Requests »

© 2024 Dominic Szablewski – rendered in (3.52ms)