Create a new tag with informations from another page

i want to create a new tag which should get some data from another page from kirby and display this information in another way on the site.

a short example.

/products/product-1/product.txt

title: Product 1
----
price: 49 $

/blog/a-new-product/article.txt

title: a new product arrived
----
text: 
we have a new product in store. 
marketing blabla
(product: product-1)

and this tag (product: product-1) should create a little html snippet inside my blog-post with contains the most important information about this new product directly from the product site.

i have tried this with a tag, but in a tag i couldn’t use $site->find() or something like this.

how can i do this?

kind regards,
stefan

Try:

kirbytext::$tags['product'] = array(
  'html' => function($tag) {
    $product = kirby()->pages()->find('products/' . $tag->attr('product'));
    return '<div>' . $product->title() . ' (' . $product->price() . ')</div>';
  }
);

Untested, but I think kirby()->pages() might do the job.

1 Like

thank you for your comment and tipp.

now i get this error message:

Fatal error: Call to a member function find() on a non-object in /kirby/site/tags/product.php on line 6

I’d wrap that in an if clause:

kirbytext::$tags['product'] = array(
  'html' => function($tag) {
    if($product = page('products/' . $tag->attr('product'))) {
        return '<div>' . $product->title() . ' (' . $product->price() . ')</div>';
    }
  }
);
2 Likes

Thanks a lot!

With Bastians help i could find the error. It was a typo in the foldername of the project. :-/