As the title says. $page->blocks()->kt()
doesn’t seem to work. Do I have to loop through each block and specifically check for text blocks?
Yes, or rather, you would modify the block snippets. And if the text blocks are writer fields, then I wouldn’t recommend it.
Yeah, the blocks are the default text blocks, i. e. writer fields.
I’m wondering now: why does the kirbytext method work on standard writer fields but not on writer fields in a layout field? And is there a way to get this to work? I have a custom KirbyTag I’d like to be able to use; it’s used in regular writer fields throughout the site so it’s kind of a break in UX that this doesn’t work in layouts by default.
KirbyText is an extension of markdown.
The writer field (no matter if by itself or in a block) however is a wysiwyg editor saving HTML, not markdown. This might give you unexpected results when, for example, you by chance use some markdown syntax inside of your HTML (and markdown might just be a line break being replaced with a <br>
).
If you just want to use a kirbytag, you would probably risk less by calling kirbytags()
on the field instead of kirbytext()
Thanks, man, I didn’t even realize that there was a kirbytags method (probably forgot or never noticed). The strange thing is that, in a page, I have a single writer field and am applying the kti() method on it, and it formats the content properly. A writer field within a layout field on the same page prints the tag literally (i. e. doesn’t parse it) if I’m using the kt() method on the block.
Anyway, using the kirbytags method on the block inside the layout doesn’t seem to work either. It just prints nothing.
Could you post what you did in the block snippet?
Sure. Basically this:
foreach($layout->columns() as $column):
foreach($column->blocks() as $block):
if($block->type() === 'text'):
echo($block->kirbytags());
else:
echo($block);
endif;
endforeach;
endforeach;
It prints the contents of all non-text blocks but not the text ones. If I remove the kirbytags method, it prints the text alright.
You have to call the method on a field in the block, not on the block object itself. 1 blocks = 1 or more fields.
If you want to apply the method on each writer field you use in any block type or in special blocks, you better adapt the corresponding block snippets.
Ooh, now I get it. I already thought to myself “How do I access the writer field in a block if that field doesn’t have a dedicated name?“. I realize now that it’s as simple as doing $block->text()->kirbytags()
. And I don’t know whether it’s the right thing to do but apparently I don’t even need to adapt the block snippet, I can do it right in the template.
Yes, you can, of course. That was just a suggestion to make things easier if you need the same code in all blocks of that type. Keeps your template clean, no if statement needed…