This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

octobro
octobro

I have a variable in twig that is a string. I want to get a specific number from this and display only that number. So something like

 {{ this.theme.social_widget | regex(id="\d+(\.\d+)*(,\d+)?") }}

But from what I see Twig doesn't support this. Is there any other way around this?

Last updated

douglas14166
douglas14166

Twig has a 'replace' filter but it does not support regex patterns. But you can overwrite the filter with your own method. See this page for details on how to add your own twig filters https://octobercms.com/docs/plugin/registration#extending-twig

public function replaceFilter($str, $search, $replace = null)
    {
        // Are they using the standard Twig syntax?
        if (is_array($search) && $replace === null)
        {
            return strtr($str, $search);
        }
        // Is this a regular expression?
        else if (preg_match('/^\/.+\/[a-zA-Z]*$/', $search))
        {
            return preg_replace($search, $replace, $str);
        }
        else
        {
            // Otherwise use str_replace
            return str_replace($search, $replace, $str);
        }
    }
octobro
octobro

Thanks douglas14166, although twig doesn't support regex your answer is more than helpful :)

octobro
octobro

How do I pass the twig filter into my returnTwitter function?

So far I have..

public function registerMarkupTags()
{
    return [
        'filters' => [
            'twitterStrip' => [$this, 'returnTwitterID']
        ],

    ];
}

public function returnTwitterID($text)
{

}

// Twig filter I want to insert into the returnTwitterID function

$filter = new Twig_SimpleFilter('data_widget_id', function ($value) {
        $matches;
        if (preg_match('#data-widget-id="\d+(\.\d+)*(,\d+)?"#', $str, $matches)) return $matches[0];
        return $value;
    });

   $twig = new Twig_Environment($loader);
   $twig->addFilter($filter);
Gregbee
Gregbee

Hi you don't need to use Twig_whatever class!

 public function returnTwitterID($text){

      //do yourlogic here and return the value you want  your filter to compute.
 }

then in your html call

{{ 'mystirngto parse' | twitterStrip }}

Last updated

achalhi12319430
achalhi12319430

Hi octobro, did you solve the problem, i have the same and i dont know how to fix that !! I would appreciate your help

alxy
alxy

Why do you need that in Twig? Normally you want to display data provided by (busniess) models, so you could define an accessor that returns the correctly formatted value.

Can you describe your use case?

Last updated

JeffGoldblum
JeffGoldblum

I've created a plugin for adding regex functions to Twig: http://octobercms.com/plugin/luketowers-twigpcre

Stranger Pings
Stranger Pings

LukeTowers said:

I've created a plugin for adding regex functions to Twig: http://octobercms.com/plugin/luketowers-twigpcre

Nice!

I will check this one out when I get some time :)

1-9 of 9

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.