Hi there,
New to Kirby and just trying it out for a clients blog.
I am using blocks on the article (blog post) page and I have created a new block for a button/link based on the basic example shown here:
https://getkirby.com/docs/reference/plugins/extensions/blocks
In the example above it shows a tab for styles with a class and id text box in the blueprint but these don’t show on the buttons code.
I would like to make the link so it only shows the class and id if they are now empty, I looked at the block reference and it has a isNotEmpty() but doesn’t explain it.
My current code is:
<a href="<?= $block->link() ?>" <?= $block->toggle()->isTrue() ? 'target="_blank"' : '' ?> class="<?= $block->class() ?>" id="<?= $block->id() ?>" title="<?= $block->title() ?>"><?= $block->text() ?></a>
So if id empty don’t show id=“” at all and same with class.
Any help would be appriciated.
Thanks.
You can use an if statement or ternary
<?= $block->id()->isNotEmpty() ? 'id="' . $block->id() . '"' : '' ?>
Thanks, I tried that and just get an error message:
Block error: “Call to a member function isNotEmpty() on string” in block type: “button”
Link code:
<a href="<?= $block->link() ?>" <?= $block->toggle()->isTrue() ? 'target="_blank"' : '' ?> <?= $block->class()->isNotEmpty() ? 'class="' . $block->class() . '"' : '' ?> <?= $block->id()->isNotEmpty() ? 'id="' . $block->id() . '"' : '' ?> title="<?= $block->title() ?>"><?= $block->text() ?></a>
Ok, $block()->id()
refers to the automatically generated id of a block, not to your field, so try $block->content()->id()->isNotEmpty()
and $block->content()->id()
1 Like
Thank you!
That worked but I has to change there field names as id must be taken by something else as it produced a string of numbers and not the text entered, changed it to ‘linkid’ and all works now.
<a href="<?= $block->link() ?>" <?= $block->toggle()->isTrue() ? 'target="_blank"' : '' ?> <?= $block->content()->linkclass()->isNotEmpty() ? 'class="' . $block->linkclass() . '"' : '' ?> <?= $block->content()->linkid()->isNotEmpty() ? 'id="' . $block->linkid() . '"' : '' ?> title="<?= $block->title() ?>"><?= $block->text() ?></a>