Content fields load external file

Here’s a solution to parse external files in the content’s fields, maybe it’s good idea to add the method to core? This way you can have metadata in the template file, and the content in any external file you want.


Title: Project Test

----

Text: README.md

We do a custom field method (inside /site/plugins/methods.php) that return the file:

// return file
field::$methods['get_file'] = function($field) {
	$file = kirby()->page()->file($field->value);
	return $file;
};

So we can use get_file() method to get the external file, and work with it:

$file = $page->text()->get_file();
$text = $file->read();
echo date("d/m/Y H:i", filemtime($file));

The problem is that $text right now is not an object, like the one you have on kirby, so you can’t use ->kibrytext(); and other methods on it, still you can use kirbytext($text);

Very interesting and thanks for sharing. Isn’t your use case very specific and keep in mind that with this way of handling your content you cannot use the panel to edit the contents of the .md file or only if you keep it in a different place. Therefor it probably doesn’t make much sense to include it in the core. Are you using .txt for Kirby and have an extra .md file in the same folder?

Thanks, yes i’ve README.md on the folder with the .txt for kirby (if you use .md also for kirby it gives an error I think).
Anyway it’s possible with a simple method.php no need to add in the core.

For the editor, it’s not a problem since this is a solution for integrating with git and github filesystem, and we coders can edit markdown and preview it easily :slight_smile:

Updated with the function that returns only the file so you can get the modification date etc…