Home  >  Q&A  >  body text

Is there a Symfony-compatible way to remove all twig tags from a string?

I have a Symfony validation constraint that removes all Twig tags with a regex before counting the number of characters and validating against the character length limit. (My form allows people to enter a limited subset of Twig tags into a field.) So it does the following:

$parsedLength = mb_strlen(
        preg_replace('/{%[^%]*%}/', '', $stringValue)
    );

...If the $parsedLength value is too long, a build violation occurs.

This works, but it doesn't work for me. Is there a way to pass some kind of service into my validation class and then use that service to render the text without the Twig tags? This seems to be a more harmonious way of doing things than using regular expressions.

P粉043470158P粉043470158185 days ago309

reply all(2)I'll reply

  • P粉038161873

    P粉0381618732024-03-22 18:07:32

    Can you share your code? From what I understand, you are applying validation logic inside constraints, but this should go inside the validator.

    The correct steps to achieve the desired results are:

    1. Create custom constraints that do not contain validation logic
    2. Create a custom validator for this constraint and configure it as a service. The validator should accept your service as constructor parameter.

    one example:

    twig_char_lenght_validator:
        class: ...\TwigCharLengthValidator
        arguments:
            - "@your.service"
    1. Complete your validator logic using injected services.

    Official documentation: https://symfony.com/doc/current/validation/ custom_constraint.html

    reply
    0
  • P粉854119263

    P粉8541192632024-03-22 16:44:46

    I'm not 100% sure this is what you're asking for, but you can create a template based on your input and then render it. Of course remove all the branches, although I'm not sure you always know what the variables are.

    I checked and all the examples look very old and I'm not sure if things still work. I can't even find an example in the documentation, although I'm sure it's there somewhere. anyway:

    use Twig\Environment;
    
    #[AsCommand(
        name: 'app:twig',
        description: 'Add a short description for your command',
    )]
    class TwigCommand extends Command
    {
        public function __construct(private Environment $twig)
        {
            parent::__construct();
        }
    
        protected function execute(InputInterface $input, OutputInterface $output): int
        {
            $input = '{{ hello }}';
            // This is the important line
            $template = $this->twig->createTemplate($input);
            $rendered = $template->render(['hello' => 'Hello World']);
            echo $rendered . "\n";
    
            return Command::SUCCESS;
        }
    }

    If nothing else, this also lets you verify the actual template. But as already mentioned, I'm not quite sure what parsed length means. Anyway, createTemplate is (to me) an interesting method.

    reply
    0
  • Cancelreply