Template file to read all and any fields in the content.txt file

Is there a way to access all and any field types in from a data txt file?

I want to be able to have the default.php template file be able to to automatically read any and all data types, eg.:

The dummy content, 1-about/about.txt with a new and random field:

Title: About Us


Text: Lorem ipsum dolor sit amet, con.

## Lorem Ipsum
In enim justo, rhoncus ut, imperdiet.

(image: about.jpg)


otherThing: testest

Here we have 3 fields:
1 - Title
2 - Text
3 - otherThing

The 1st two are fairly straight forward and std fields… however the 3rd is non-standard. The default template file might look like:

<div class="text">
    <h1><?php echo $page->title()->html() ?></h1>
    <?php echo $page->text()->kirbytext() ?>
    <?php echo $page->otherThing()->kirbytext() ?>

What I need to be able to do is loop through all the field types with a switch statement to catch all non-std field names.

Is this possible with kirby?


At least there is a way to loop through all fields:

$c = $page->content();
foreach ($c as $key => $value) {
  if ($key == "fields") {
    foreach ($value as $field) {
      echo $field . ": " . $page->$field() . "<br/>";

Maybe this help as a starting point.

“Non-standard field names” don’t exist. You can and should use any appropriate field name with Kirby, there are no limitations. What are you trying to do? Maybe there’s an easier solution.

This is exactly the thing i was looking for! thank you very much :smiley:

I am putting together something along the lines of an “attribute view gui” from eZPublish. Default view snippets for different data types. But to be able to start doing this i needed to be able to cycle through the different data fields in any given *.txt content file. The reason is two fold, the obv being this will make templating life a little easier. The other generating correctly formatted content for a rest api of which angular can plug into.