Custom content in kirbytext

Hello, a client wish to have the possibility to custom himself (in this case: center) some content.

For the moment I do it this way with kirbytag:

<?php
kirbytext::$tags['custom-text'] = array(
'attr' => array(
    'class'
  ),
'html' => function($tag) {
    $class = $tag->attr('class');
	$content = $tag->attr('custom-text');
	return '<div class="' . $class . '">' . kirbytext($content) . '</div>';
      }
   );
   ?>

And in text:

(custom-text: 
##Title
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas impedit, quia aut deserunt reprehenderit minima               sa velit ratione, at cupiditate sequi placeat adipisci quidem, repellendus.
class:center
)  

It works fine, but it is not very pleasant to watch and write for the user, and I guess that using kirbytag is better for small content like Link, Image, Title etc, not for this type of content.

I thought it was certainly better to do it the same way as column plugin but I’m not able to reproduce it, for exemple:

(custom-text..)
...content...
(..custom-text)

Any help, suggestion is welcome.

Thank you.

Try this:

<?php

kirbytext::$pre[] = function($kirbytext, $text) {
  $text = preg_replace_callback('!\(centertext(…|\.{3})\)(.*?)\((…|\.{3})centertext\)!is', function($matches) use($kirbytext) {
    $customtext = $matches[2];
    $html    = "";
    $field = new Field($kirbytext->field->page, null, trim($customtext));
    $html = '<div class="center">' . $field->kt() . '</div>';
    return $html;
  }, $text);
  return $text;
};

I will send to you and @lukasbestle some good wine from France :wink:
Thanks!

You are welcome :slight_smile:

If you want to make this more flexible, you can also allow the user to add an optional class, the code would then look like below. The class will fall back to center in this example if there is no class given.

<?php

kirbytext::$pre[] = function($kirbytext, $text) {
  $text = preg_replace_callback('!\(customtext(…|\.{3})\)(.*?)\((…|\.{3})customtext(\s*)?(class:\s?)?(.*?)?\)!is', function($matches) use($kirbytext) {
    $customtext = $matches[2];
    if(! empty($matches[6])) {
      $class = $matches[6];
    }
    else {
      $class = "center";
    }
    $html    = "";
    $field = new Field($kirbytext->field->page, null, trim($customtext));
    $html = '<div class="' . $class . '">' . $field->kt() . '</div>';
    return $html;
  }, $text);
  return $text;
};

In the text:

(customtext...)
Lorem ipsum dolor set amet ...
(...customtext class: pull-right)

Seems to work fine so far …

1 Like

Yes seems to work. Such a nice option ! Even if I think that giving too many possibilities to the users in kirbytext may not be so good, it is very interesting to know about it. Still have to anderstand $matches[2] and $matches[6] :slight_smile:

The index refers to the separate matches in the $matches array. By creating groups of matches - included within parenthesis in the regex - you can then fetch these individual groups. $matches[0] contains the complete search string, $matches[1] refers to this group (…|\.{3}), i.e. the three dots, $matches[2] to (.*?), i.e. the text, $matches[6] to the last group(.*?)`, i.e. the class.

Maybe not so well explained, hope it helps, though.

BTW: I recommend this website: http://regexr.com

1 Like