to follow up on that thought. i have been able to at least make it somewhat a bit more usable by having multiple pages being able to use the same methods to get it done…
searching for the parent page which has informations about the url’s and inserting a link via kirbytag
<?php
kirbytext::$tags['out'] = array(
'attr' => array(
'text'
),
'html' => function($tag) {
if($tag->page()->template() == 'book.cover'){
$page = $tag->page()->url();
}
if($tag->page()->parent()->template() == 'book.cover'){
$page = $tag->page()->parent()->url();
}
if($tag->page()->parent()->parent()->template() == 'book.cover'){
$page = $tag->page()->parent()->parent()->url();
}
$out = $tag->attr('html');
$text = $tag->attr('text');
return '<a href="'.$page.'/'.$tag->attr('out').'">'.$text.'</a>';
}
);
Just like before, but using site->structure() each independant page:
array(
'pattern' => 'shop/(:any)/(:any)',
'action' => function ($product,$url) {
$link = page('shop/'.$product)->links()->toStructure();
$out = $link->filterBy('name','==',$url);
if(count($out) >= 1){
go($out->nth(0)->link());
} else {
go('error');
}
return false;
}
)
is there a good way to go though all parent levels to search for in this case the right template?
EDIT:
When i think about it… Wouldn’t it be possible to directly update the structure with links if the entry is not available of just yet??
kirbytext::$tags['out'] = array(
'attr' => array(
'text'
),
'html' => function($tag) {
if($tag->page()->template() == 'book.cover'){
$page = $tag->page()->url();
// PSEUDO CODE
$link = $page->links()->toStructure();
$out = $link->filterBy('name', '==',$tag->attr('out'));
if(count($out) == 0){
// UPDATE Structure accordingly directly here??
}
}
}
// ............ More
$out = $tag->attr('html');
$text = $tag->attr('text');
return '<a href="'.$page.'/'.$tag->attr('out').'">'.$text.'</a>';
}
);
EDIT 2:
<?php
kirbytext::$tags['out'] = array(
'attr' => array(
'text', 'link'
),
'html' => function($tag) {
// Looking for a nicer way to check each parent site
if($tag->page()->template() == 'book.cover'){
$page = $tag->page();
}
// This works just fine, just need to apply to parent pages in a more beautiful way
if($tag->page()->parent()->template() == 'book.cover'){
$page = $tag->page()->parent();
$links = $page->links()->toStructure();
$out = $links->filterBy('name', '==',$tag->attr('out'));
if(count($out) == 0){
// UPDATE Structure accordingly directly here??
$exist = $page->content()->get('links')->yaml();
$exist[] = ['name' => $tag->attr('out'), 'link' => $tag->attr('link')];
$page->update(['links' => yaml::encode($exist)]);
}
}
// Looking for a nicer way to check each parent site
if($tag->page()->parent()->parent()->template() == 'book.cover'){
$page = $tag->page()->parent()->parent();
}
$out = $tag->attr('html');
$text = $tag->attr('text');
return '<a href="'.$page->url().'/'.$tag->attr('out').'">'.$text.'</a>';
}
);
So this is the solution to create it on the fly which works fine, just need to cleanup going through all parent pages to find the template file. so my question applies, is there any good way to check each parent without repeating the same code over and over again? 