How do I add a class when calling kirbytext?

I currently have this code:
<p class="testimonial__body"><?php echo $page->testimonial()->kirbytext() ?></p>

This outputs a paragraph tag within a paragraph tag.

How can I move do something like this:
<?php echo $page->testimonial()->kirbytext.addClass('testimonial__body') ?>

What would the correct formatting be?

Put the below code into a file in /site/plugins:

function ktRaw($content) {
  $text = kirbytext($content);
  return preg_replace('/(.*)<\/p>/', '$1', preg_replace('/<p>(.*)/', '$1', $text));
}

field::$methods['ktRaw'] = function($field) {
  return ktRaw($field->value);
};

The function removes the p-tags from content.

Then in your template, call the new ktRaw() field method:

<p class="testimonial__body"><?php echo $page->testimonial()->ktRaw() ?></p>
1 Like

Awesome! Thanks! If you use this, how would you output line breaks?

If you need line breaks, you shouldn’t use a <p> tag for the outer container. Use a <div> instead and use the normal built-in Kirbytext behavior.

But the whole point of my question is to figure out how to add a class to the <p> tag so I can style it

All you need to do is start a new line in your text to get a <br> tag. If you need to start a new paragraph (<p> tag), the you would have to follow @lukasbestle’s advice. You can then style the p-tags within the div if you add a class to it.

1 Like

For anyone curious about an alternative, I think this CSS selector is more descriptive of the desired effect, as @lukasbestle began to explain, above.

.testimonial__body p {
  font-family: serif;
}

This way, you can use the vanilla Kirbytext or Markdown output inside a container with the testimonial__body classname, and let the “cascade” apply the rules to individual paragraphs.

<div class="testimonial__body">
  <?php echo $page->testimonial()->kirbytext() ?>
</div>

This is effectively the same solution, except that you can quickly add more rules to treat Kirbytext output differently based on the testimonial’s context:

.testimonial__body ul {
  list-style-type: disc;
}
2 Likes

The problems with <br> is that the plugin is multiplying them:

The plugin suggested above is multiplying the br tags.

How do I fix that?

I can’t reproduce that issue. What does your content file look like?

I think @AugustMiller’s approach would be the cleanest. <br> isn’t very semantic, especially if you want separate paragraphs.

1 Like

I agree, I also think each quote should have its own paragraph within a div container.

1 Like