1

As per the Magento documentation, we can create custom directive like {{var}} {{if}} {{depends}} using this documentation https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/templates/template-email-migration.html#abstraction-example.

We are using filters like nl2br, escape, raw So, Is it possible to extend the filters also? for eg. strtoupper to convert all things In upper case.

UPDATE

etc/di.xml

 <type name="Magento\Framework\Filter\DirectiveProcessor\Filter\FilterPool">
     <arguments>
         <argument name="filters" xsi:type="array">
             <item name="capitalize" xsi:type="object">Vendor\Module\Model\CapitalizeWord</item>
         </argument>
     </arguments>
 </type>

CapitalizeWord.php

 <?php
 namespace Vendor\Module\Model;
 use Magento\Framework\Filter\DirectiveProcessor\FilterInterface; 
 class CapitalizeWord implements FilterInterface
 {
     public function getName(): string
     {
         return 'capitalize';
     }
     /**
     * @inheritDoc
     */
     public function filterValue($value, array $params): string
     {
        return ucwords($value);

     }
 }
0

You write something similar to my comment

https://github.com/magento/magento2/blob/2.4/lib/internal/Magento/Framework/Filter/DirectiveProcessor/Filter/EscapeFilter.php

It would be StrtoupperFilter.php for example

use Vendor\Extension\Filter\DirectiveProcessor\Filter;
use Magento\Framework\Escaper;
use Magento\Framework\Filter\DirectiveProcessor\FilterInterface;

/**
 * StrtoupperInput
 */
class StrtoupperFilter implements FilterInterface {

Where

/**
 * @inheritDoc
 */
public function getName(): string
{
    return 'strtoupper';
}

Adapt the following to you needs

public function filterValue(string $value, array $params): string

And then add di XML. Structure would look a bit like this...

https://github.com/magento/magento2/blob/e42ed8dcf89ce65d7ba4af489fc6e6cd764e161b/app/code/Magento/Email/etc/di.xml#L85-L92

Vendor/Extension/etc/di.xml
    <type name="Magento\Framework\Filter\DirectiveProcessor\Filter\FilterPool">
        <arguments>
            <argument name="filters" xsi:type="array">
                <item name="strtoupper" xsi:type="object">Vendor\Extension\Filter\DirectiveProcessor\Filter\StrtoupperFilter</item>
            </argument>
        </arguments>
    </type>

The FilterPool mechanisnm will pick it up and add it to the pool

Then should be the case of

{{var param|strtoupper}}

in template

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.