goulashsoup
goulashsoup

Reputation: 3076

PhpStorm - How to escape dollar sign (php variable) in File Template?

I have a PhpStorm Php file template under Editor > File and Code Templates:

PhpStorm File Template

According to the linked Apache Velocity Docs to escape a dollar sign a back slash should be used:

<?php

declare(strict_types=1);
#parse("PHP File Header.php")
#if (${NAMESPACE})
namespace ${NAMESPACE};
#end

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

class ${NAME} implements DataTransformerInterface
{
    public function __construct()
    {}

    public function transform(\$value)
    {
        return ;
    }
}

But when I select the template when creating a class, the backslash stays:

Use Template

File output:

<?php

declare(strict_types=1);
namespace SomeProject\SomeBundle\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

class TestTransformer implements DataTransformerInterface
{
    public function __construct()
    {}

    public function transform(\$value)
    {
        return ;
    }

    public function reverseTransform()
    {
        return ;
    }
}

It works when i directly select the template and not create class, but then the namespace is not taken automatically...

So how do i escape the dollar sign properly? Is that a known bug?

Upvotes: 7

Views: 1237

Answers (1)

LazyOne
LazyOne

Reputation: 165118

As per PhpStorm help page (link below) the best way would be to use predefined ${DS} variable if you need to have $ (dollar sign) in the file template output.

https://www.jetbrains.com/help/phpstorm/file-template-variables.html

Upvotes: 3

Related Questions