getkirby / ideas

This is the backlog of ideas and feature requests from the last two years. Use our new feedback platform to post your new ideas or vote on existing ideas.
https://feedback.getkirby.com
20 stars 0 forks source link

Add `tag()` helper for `Kirby\Toolkit\Html::tag()` #34

Open ghost opened 6 years ago

ghost commented 6 years ago

@wottpal commented on Jul 28, 2018, 11:23 AM UTC:

First: I know this could easily be a nice plugin but I truly believe every php-/kirby-beginner could profit of this DRY-way of writing template-code.

There is the tag($name, $content, $attrs) function we can use to easily generate various tags with different attributes and content. Like the documentation says when $content == '' the tag is returned with not content - but it's returned. This is only useful in a very limited number of cases but often when it comes to templating you want no surrounding tag at all:

<?php if($page->subheading()->isNotEmpty()): ?>
<h3><?= $page->subheading() ?></h3>
<?php endif ?>

Implementing this behavior as a default in the tag() function would make so much code shorter and more readable (the tag()-function finally beneficial):

<?= tag('h3', $page->subheading()) ?>

(This example also assumes there is a tag() shorthand for Kirby\Toolkit\Html::tag)

To still make the old behavior accessible there could be for example an $allowEmpty parameter at the end which defaults to false.

What do you think?

This issue was moved by bastianallgeier from k-next/kirby#771.

ghost commented 6 years ago

@texnixe commented on Jul 28, 2018, 10:00 PM UTC:

I actually don't really think HTML template code should be written like this. It might be readable for developers, but probably isn't for designers and the HTML and the logic should be clearly separated.

ghost commented 6 years ago

@wottpal commented on Jul 30, 2018, 12:16 PM UTC:

I think it's a question of taste and we should not write code with people in mind who will never read it. I think for people who spend all day writing template code this solution will be just fine and very readable.

I created a plugin for it but not with the proposed syntax but $page->field()->tag('h2') which looks really nice imho.

https://github.com/wottpal/kirby-field-tagger