# Custom block can't get to work

**URL:** <https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153>\
**Category:** Questions\
**Tags:** v4\
**Created:** [March 20, 2024, 12:53pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153 "2024-03-20T12:53:01Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [March 20, 2024, 12:53pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/1 "2024-03-20T12:53:01Z")

</div>

I’m trying to get my first custom block to work. Using Kirby 4.

I have the following page Blueprint which is working for me in the Panel:

```auto
title: News

fields:
	newsblock:
		type: blocks
		pretty: true
		fieldsets:
			- type: newsarticle
				preview: fields
				wysiwyg: true
				fields:
					news_article_heading:
						type: text
					news_article_text:
						type: writer
					news_article_image:
						type: files

```

I’ve then made a Block snippet in the site \> snippets \> blocks folder called newsarticle.php:

```auto
<?php foreach ($block->items() as $starjump): ?>

<article>
	<h2><?= $starjump->news_article_heading() ?></h2>
	<p><?= $starjump->news_article_text() ?></p>
</article>

<?php endforeach ?>

```

And then in the page template I have the following code:

`<?= $page->newsblock() ?>`

In browser I can see the JSON content. So I’ve added the toBlocks field method:

`<?= $page->newsblock()->toBlocks() ?>`

But now nothing is showing up in the browser.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Cris](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/cris/32/14642_2.png) [@Cris](https://forum.getkirby.com/u/Cris)\
**Post date:** [March 20, 2024, 2:00pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/2 "2024-03-20T14:00:50Z")

</div>

You don’t iterate in the block snippet, but rather call the fields directly.  
(Check out the dump helper, if You need a little more info on what is happening in that snippet.)

```php
// snippets/blocks/newsarticle

<?php /** @var \Kirby\Cms\Block $block */
 // dump($block);
?>
<article>
  <h2><?= $block->news_article_heading() ?></h2>
  <p><?= $block->news_article_text() ?></p>
</article>

```

Then in Your template You echo all blocks:

```php

<?php foreach($page->newsblock()->toBlocks() as $newsarticle): ?>
  <?= $newsarticle ?>
<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [March 20, 2024, 2:11pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/3 "2024-03-20T14:11:37Z")

</div>

Hey thanks!

---

<div class="post-metadata">

**Author:** ![Cris](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/cris/32/14642_2.png) [@Cris](https://forum.getkirby.com/u/Cris)\
**Post date:** [March 20, 2024, 2:14pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/4 "2024-03-20T14:14:18Z")

</div>

You’re welcome! =)  
If You want to know more on how blocks work, I’d recommend checking out the  
[guide](https://getkirby.com/docs/guide/page-builder) and the [reference](https://getkirby.com/docs/reference/panel/fields/blocks).

---

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [March 20, 2024, 2:39pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/5 "2024-03-20T14:39:44Z")

</div>

I’ve spent the last day staring at the videos, guides and references!

My next problem is extra, empty `<p>` tags are being added to the HTML.

![Screenshot 2024-03-20 at 2.35.30 pm](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/9/9f9624d71e2d66a5f118d0d25096fc9e78f64d5e.png)

Which are not showing in my Contents page:

 ![Screenshot 2024-03-20 at 2.37.19 pm](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/f/f572d16b67599ac951ca8423b5e8344d3c58be24.png)

Here’s the latest Block snippet

```auto
<article>
	<div class="text-content">
		<h2><?= $block->news_article_heading() ?></h2>
		<p><?= $block->news_article_text() ?></p>
	</div>
	<?php if($image = $block->news_article_image()->toFile()): ?>
		<img src="<?= $image->url() ?>" alt="">
	<?php endif ?>
</article>

```

---

<div class="post-metadata">

**Author:** ![mikeharrison](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mikeharrison/32/9620_2.png) [@mikeharrison](https://forum.getkirby.com/u/mikeharrison)\
**Post date:** [March 20, 2024, 2:45pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/6 "2024-03-20T14:45:45Z")

</div>

Hi Mark,

You are adding those in your snippet - try:

```auto
<article>
	<div class="text-content">
		<h2><?= $block->news_article_heading() ?></h2>
		<?= $block->news_article_text() ?>
	</div>
	<?php if($image = $block->news_article_image()->toFile()): ?>
		<img src="<?= $image->url() ?>" alt="">
	<?php endif ?>
</article>

```

The `<p>` tags are already added in the Writer field

---

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [March 20, 2024, 2:46pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/7 "2024-03-20T14:46:41Z")

</div>

Hi Mike! Good to hear from you on here! I’ll take a look

---

<div class="post-metadata">

**Author:** ![Cris](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/cris/32/14642_2.png) [@Cris](https://forum.getkirby.com/u/Cris)\
**Post date:** [March 20, 2024, 2:48pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/8 "2024-03-20T14:48:11Z")

</div>

You could also check, if the field has some content in an `if` statement, just like You verify, that the image actually exists, before You render it.

```php
<?php if($block->news_article_text()->isNotEmpty()): ?>
<p><?= $block->news_article_text() ?></p>
<?php endif ?>

```

---

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [March 20, 2024, 2:50pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/9 "2024-03-20T14:50:39Z")

</div>

> [@mikeharrison](#):
>
> The `<p>` tags are already added in the Writer field

Yeah, the Writer writes html tags to the contents page file, so yes, I don’t need to add the `<p>` tags in the Block snippet.

---

<div class="post-metadata">

**Author:** ![Cris](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/cris/32/14642_2.png) [@Cris](https://forum.getkirby.com/u/Cris)\
**Post date:** [March 20, 2024, 2:52pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/10 "2024-03-20T14:52:23Z")

</div>

Yes, only if not in inline mode =)

---

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [March 20, 2024, 2:54pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/11 "2024-03-20T14:54:20Z")

</div>

How do I know if the `type: writer` is inline or not? What does inline mode even mean?

---

<div class="post-metadata">

**Author:** ![Cris](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/cris/32/14642_2.png) [@Cris](https://forum.getkirby.com/u/Cris)\
**Post date:** [March 20, 2024, 2:56pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/12 "2024-03-20T14:56:00Z")

</div>

It’s disabled by default

> Enables inline mode, which will not wrap new lines in paragraphs and creates hard breaks instead.

See the field [properties](https://getkirby.com/docs/reference/panel/fields/writer).

---

<div class="post-metadata">

**Author:** ![Mark\_E](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mark_e/32/10834_2.png) [@Mark\_E](https://forum.getkirby.com/u/Mark_E)\
**Post date:** [March 20, 2024, 3:00pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/13 "2024-03-20T15:00:22Z")

</div>

Blimey, there’s a lot to take in!

---

<div class="post-metadata">

**Author:** ![Cris](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/cris/32/14642_2.png) [@Cris](https://forum.getkirby.com/u/Cris)\
**Post date:** [March 20, 2024, 8:26pm UTC](https://forum.getkirby.com/t/custom-block-cant-get-to-work/31153/14 "2024-03-20T20:26:51Z")

</div>

😊 I know - but that also makes this CMS so flexible, and I’m sure You’ll get used to it.  
Everything is well documented (heads up to the kirby team).  
Good luck with Your project!
