Getting started with Kirby

I assume your subfolders of the products folder are the single product pages, right, not only folders for the images…

Ok, you are in the home.php template and want to fetch the products of the products page:

<?php if ($p = page('products')) : ?>
  <?php foreach ($p->children() as $product): ?>
  <div class="products-box">
    <a href="<?= $product->url()">
      <?php if ($image = $product->image()): ?>
      <img src="<?= $image->url() ?>">
      <?php endif ?>
    </a>
    <div class="products-box-title">
      <h3><?= $product->title()->html() ?></h3>
    </div>
  </div>
  <?php endforeach ?>
<?php endif ?>

The products folder which lives inside the content folder, containers folders with different names.

And so when I put it like this it didn’t work:

<!-- ======= Products ======= -->

<div class="products-container">

  <div class="title">
    <h2>List of our products</h2>
  </div>

  <article class="products">
    <?php if ($p = page('produkt')) : ?>
  <?php foreach ($p->children() as $produkt): ?>
  <div class="products-box">
    <a href="<?= $produkt->url()">
      <?php if ($image = $produkt->image()): ?>
      <img src="<?= $image->url() ?>">
      <?php endif ?>
    </a>
    <div class="products-box-title">
      <h3><?= $produkt->title()->html() ?></h3>
    </div>
  </div>
  <?php endforeach ?>
<?php endif ?>
  </article>
</div>

Please post your content folder structure

Sure. We have a content folder. Inside this content folder we have a folder named produkt. Inside this produkt folder we have many different folders each having a different name (e.g Soap, after shave, etc).

Inside each of these folders that have different names we have an image of the product itself and a produkt.txt file. The values inside the produkt.txt file vary according to the name of the product. In each of the folders that have a different name and that live inside the produkt folder, the name of the txt file is always produkt.txt.

Well, yes, but then I don’t understand why the code above shouldn’t work.

content/
  produkt/
    produkt-1/
    produkt-2/

so

if ($p = page('produkt')) {
 // ...
}

Should give you the produkt parent page, then inside you have the children() produkt-1 etc.

So, which part exactly doesn’t work, and what does “doesn’t work” mean? An error? No output at all?

So, the soap folder that lives inside the produkt folder (which lives inside the content folder), has an image of the soap and a produkt.txt file. The produkt.txt file has these values:

Title: Produkt Green Soap


Information: My description text comes here. My description text comes here.


Preis: € 0,50

The after shave folder that lives inside the produkt folder (which lives inside the content folder), has an image of the after shave and a produkt.txt file. The produkt.txt file has these values:

Title: Produkt After Shave


Information: My description text comes here. My description text comes here.


Preis: € 1,50

content/
produkt/
Soap/ (contains the soap image and a produkt.txt file)
aftershave/ (contains the after shave image and a produkt.txt file)

<!-- ======= Products ======= -->

<div class="products-container">

  <div class="title">
    <h2>List of our products</h2>
  </div>

  <article class="products">
    <?php if ($p = page('produkt')) : ?>
  <?php foreach ($p->children() as $produkt): ?>
  <div class="products-box">
    <a href="<?= $produkt->url()">
      <?php if ($image = $produkt->image()): ?>
      <img src="<?= $image->url() ?>">
      <?php endif ?>
    </a>
    <div class="products-box-title">
      <h3><?= $produkt->title()->html() ?></h3>
    </div>
  </div>
  <?php endforeach ?>
<?php endif ?>
  </article>
</div>

At this point it is irrelevant what these children folders are called or which information they contain. All folder names need to be unique, anyway.

soap and after-shave are, however, children of the produkt page.

VS Code is highlighting this in red <a href="<?= $produkt->url()">

Ah, ok, closing php tag missing

<a href="<?= $produkt->url() ?>">
1 Like

The debugger is throwing this message: syntax error, unexpected double-quote mark, expecting "," or ";"

Thank you so much, it worked.

How can I reduce the number of items displayed to nine?

You can use ->limit(8) to do that. Heres an example

So on the start of the foreach:

<?php foreach ($p->children()->limit(8) as $produkt): ?>

Programming languages generally start counting from 0 instead of 1 so setting 8 in the limit should give you 9.

1 Like

Thank you very much. It worked.

In the impressum.yml file, I created the following blocks and they all show up in the Kirby panel.

title: Text Page
preset: page
num: zero

status:
draft: true
listed: true

fields:
Bigheadline:
label: Bigheadline
type: textarea

Headline:
label: Headline
type: textarea

Textfeld:
label: Textfeld
type: textarea

The HTML looks like this:

<div class="legal-text">
<h1><?php echo $page->bigheadline(); ?></h1>
<h2><?php echo $page->headline(); ?></h2>
<p><?php echo $page->textfeld(); ?></p> 
  </div>

In the Impressum.txt file, I’ve the following values:

Titel: Impressum


Bigheadline: Datenschutz


Headline: DGSVO


Textfeld:

Mattis senectus dignissim penatibus hac quam turpis ac lectus nisl cursus elementum etiam. At torquent nisl curae; curabitur sollicitudin aptent etiam nunc est nam lectus. Eu odio porttitor at natoque ultrices lacinia vivamus suspendisse cursus donec nulla. Cursus augue ipsum maecenas rhoncus gravida eu montes imperdiet. Sagittis taciti leo vitae amet sociis vehicula torquent.

Mattis senectus dignissim penatibus hac quam turpis ac lectus nisl cursus elementum etiam. At torquent nisl curae; curabitur sollicitudin aptent etiam nunc est nam lectus. Eu odio porttitor at natoque ultrices lacinia vivamus suspendisse cursus donec nulla. Cursus augue ipsum maecenas rhoncus gravida eu montes imperdiet. Sagittis taciti leo vitae amet sociis vehicula torquent.

Mattis senectus dignissim penatibus hac quam turpis ac lectus nisl cursus elementum etiam. At torquent nisl curae; curabitur sollicitudin aptent etiam nunc est nam lectus. Eu odio porttitor at natoque ultrices lacinia vivamus suspendisse cursus donec nulla. Cursus augue ipsum maecenas rhoncus gravida eu montes imperdiet. Sagittis taciti leo vitae amet sociis vehicula torquent.


The issue is that there is no spacing between the paragraphs even though I added spacing with my keyboard. Also, when I click on bold in the Kirby panel text editor to make the headline text bold, the headline text remains normal and does not change to bold.

Each field has examples how to use in templates: Textarea | Kirby CMS

I saw this page but don’t know how to apply the code to my code.