【问题标题】:How to use the method name in live template variables?如何在实时模板变量中使用方法名称?
【发布时间】:2021-09-13 23:13:14
【问题描述】:

我正在使用 PhpStorm。我想在 Live Template 变量中使用 PHP 类中的方法名称。但是我在PhpStorm live template variables页面上没有找到它的功能。

有没有办法在模板中插入方法名?

/**
 * @return Response
 * @Route(path="/profile", name="profile")
 */
public function profile(): Response
{
    return $this->render("app/profile/$HOW_TO_INSERT_METHOD_NAME_HERE$.html.twig");
}

【问题讨论】:

  • 我认为这应该相当容易,只需使用模板中的相同变量作为方法名称本身。你能发布你的模板是什么样的吗?

标签: php symfony phpstorm


【解决方案1】:

这不是 symfony 特定的,但您可以使用 __METHOD__ 常量。不过,这也将包括类名。使用__FUNCTION__ 仅获取方法本身的名称。

请参阅文档中的 this comment

像这样:

return $this->render("app/profile/" . __FUNCTION__ . ".html.twig");

演示:https://3v4l.org/mu5qB

【讨论】:

  • 不工作。我需要像 jsMethodName() 这样的 PHP 函数。
  • @F.Tomas 哦,我才意识到你的意思是 PHPStorm 中的模板。对不起。是什么阻碍了您再次添加用于方法名称的相同占位符?
【解决方案2】:

不幸的是,这不适用于 PHP。这是相应功能请求的链接:https://youtrack.jetbrains.com/issue/WI-43488。随意投票或评论它,以获得有关其更新的通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 2012-12-29
    • 1970-01-01
    相关资源
    最近更新 更多