Perry
August 12, 2022, 9:43pm
1
Hello,
I make a page on a website available as JSON. In this I use a kirby custom tag to include a structur field. now the data of the table is not created when the JSON is created. but in a normal template already. How can I solve this ?
content 2 JSON
<?php
$data = $pages->find('welcome-guide')->children()->published();
$json = [];
foreach ($data as $article) {
$json[] = [
'title' => (string)$article->title_on_page(),
'text' => (string)$article->richtext()->kt()
];
}
echo json_encode($json);
kirby TAG
<?php
Kirby::plugin('st/table', [
'tags' => [
'table' => [
'attr' =>[
'table_name'
],
'html' => function($tag) {
$field = $tag->value;
$html = '<table class="kirbytag-table">';
$loop_counter = 0;
foreach(page()->$field()->toStructure() as $table)
{
$loop_counter++;
if($loop_counter == 1):;
$html .= '<tr>';
$html .= '<th>';
$html .= $table->col01();
$html .= '</th>';
$html .= '<th>';
$html .= $table->col02();
$html .= '</th>';
$html .= '<th>';
$html .= $table->col03();
$html .= '</th>';
$html .= '</tr>';
endif;
if($loop_counter >1):;
$html .= '<tr>';
$html .= '<td>';
$html .= $table->col01();
$html .= '</td>';
$html .= '<td>';
$html .= $table->col02();
$html .= '</td>';
$html .= '<td>';
$html .= $table->col03();
$html .= '</td>';
$html .= '</tr>';
endif;
}
$html .= '</table><br>';
return $html;
}
]
]
]);
thanks for reading
greetings perry
What do you get when you open that json representation in the browser?
Perry
August 13, 2022, 8:35am
3
valid JSON and no error message, but the table has no content: <table class="kirbytag-table"></table><br>
when displaying via template, the table is displayed
Then make sure that your field names are all correct.
Perry
August 13, 2022, 12:50pm
5
the field names are correct, so i add the structure field via tag (table: table01)
and this is what my blueprint looks like
title: page
icon: page
pages: false
options:
preview: false
changeTemplate: true
duplicate: false
delete: true
columns:
# main content
main:
width: 2/3
sections:
content:
type: fields
fields:
title:
label: Title
type: text
title_on_page:
label: Titel
type: text
richtext:
label: Text
extends: fields/markdown_editor
table01:
label: table01
type: structure
style: table
modalsize: small
fields:
col01:
label: Col01
type: text
col02:
label: Col02
type: text
col03:
label: Col03
type: text
table02:
label: table02
type: structure
style: table
modalsize: small
fields:
col01:
label: Col01
type: text
col02:
label: Col02
type: text
col03:
label: Col03
type: text
table03:
label: table03
type: structure
style: table
modalsize: small
fields:
col01:
label: Col01
type: text
col02:
label: Col02
type: text
col03:
label: Col03
type: text
sidebar_images:
label: Sidebar
type: files
width: 1/2
size: large
query: page.images
# files
sidebar:
width: 1/3
sections:
files:
headline: Files
type: files
layout: list
Well, theoretically, it should work.
I’d dump()
some variables in the tag (and then output as standard array instead of json) to try and debug what’s wrong.
Perry
August 13, 2022, 2:18pm
7
this is the raw content of the structur fields, can certain characters cause the problem?
col01: Train
col02: Voie
col03: Heure de départ
-
col01: 'RE --> Chaux-de-Fonds'
col02: "1"
col03: :20 / :47
-
col01: 'ICN --> Bâle'
col02: "2"
col03: :49
-
col01: 'ICN --> Zurich/St. Galles'
col02: "3"
col03: :17 / :46
-
col01: 'ICN --> (Lausanne)/Genève'
col02: "5"
col03: (:16) / :45
-
col01: 'RE --> Berne'
col02: 6/7
col03: :22 / :52"
Perry
August 13, 2022, 3:37pm
8
it seems that if you use a custom tag in a json output, the forEach loop of the tag will never fire.
TAG
'html' => function ($tag) {
$field = $tag->value;
$html = '<table class="kirbytag-table">';
foreach (page()->$field()->toStructure() as $key => $table) {
I’d say the problem is your use of page()
instead of $tag->parent()
Perry
August 13, 2022, 9:17pm
10
texnixe:
$tag->parent()
thanks that was the solution! can you please explain to me why the parent element? I would like to refer to the structure field in the current page and not one level higher.
parent()
refers to the parent of the tag, i.e. the page where the tag is used, not the parent of the page.
Perry
August 14, 2022, 9:37pm
12
thanks for the explanation