.Nice with a parameter

I love using stuff like this in my Template:


I have also created my own methods instead of Nice … e.g.


Now I would like to create


where $ID is the ID of the page currently being displayed.

Can this be done in Silverstripe 4?

Have you thought about using shortcodes instead? If you register a shortcode, any HTML field will automatically use it. If your field is plain text, you can instance a shortcode parser and use it. That would allow you to configure your parameters directly from database fields and apply that sitewide.

If you instead would like to do what are you looking for, you will need to

  1. either extend the Text field and add a new method, looks like you already did it
  2. add the method to your page, so you will have ReplaceKeywords($MyText).

I’m not sure if you can pass params to functions in templates.

Thank you for looking at this Giancarlo.

I don’t think shortcodes will work for what I am doing here (building an automated glossary system - where we replace certain terms - on certain pages with a pop-up definition), but I will have a closer look, because maybe I am missing something …

I just write an extension for dbtext amd inject it

Useful for things like $Mystring.YoutubeID

The following is a snippet of such in SS4:

/* yml */
    - \CustomText

/* CustomText.php */
use SilverStripe\Core\Convert;
use SilverStripe\ORM\DataExtension;

class \CustomText extends DataExtension {
    public function ReplaceYoutubeID($ID) {
        $url = $this->owner->value;
        $pattern = 
            '%^# Match any youtube URL. use delimiter percent instead of default / so no need to escape
            (?:https?://)?  # Optional scheme. Either http or https
            (?:www\.)?      # Optional www subdomain
            (?:             # Group host alternatives
              youtu\.be/    # Either youtu.be,
            | youtube\.com  # or youtube.com
              (?:           # Group path alternatives
                /embed/     # Either /embed/
              | /v/         # or /v/
              | /watch\?v=  # or /watch\?v=
              )             # End path alternatives.
            )               # End host alternatives.
            ([\w-]{10,12})  # Allow 10-12 for 11 char youtube id.
            ; // x modifier - ignores white space; ?: in parenthesis - non-capturing group
        $result = preg_match($pattern, $url, $matches);
        if (false !== $result && isset($matches[1])) {
            return str_replace($matches[1], $ID, $url);
        // no match
        return $url;

/* .ss */
<% loop $Videos %>
    $YoutubeLink : $YoutubeLink.ReplaceYoutubeID($ID)
<% end_loop %>

/* results */
https://www.youtube.com/watch?v=IesIsKMjB4Y : https://www.youtube.com/watch?v=2

So yes, passing parameters work