简体   繁体   English

twig 函数将变量设置为未定义

[英]twig function to set variable as undefined

I know it is easy to check if a variable is defined using我知道很容易检查变量是否定义使用

{# defined works with variable names #}
{% if foo is defined %}
    ...
{% endif %}

and to set a variable if it is undefined using:如果变量未定义,则使用以下方法设置变量:

{% set foo = 'bar' %}

Is there a way to set the variable as undefined?有没有办法将变量设置为未定义? I have a variable that is passed to an twig html file and it is not always passed, and I just came across a situation where it could be null.我有一个传递给 twig html 文件的变量,但它并不总是传递,我只是遇到了它可能为空的情况。 When it is null, I would like to set it to undefined because the logical statements through the rest of the file work correctly and it's a pain testing for defined first, then null a second time each time I use this variable.当它为空时,我想将其设置为未定义,因为通过文件其余部分的逻辑语句可以正常工作,并且首先对定义进行痛苦测试,然后每次使用此变量时再次为空。

I came across this conversation and wanted to know if anyone else has a solution to either unset the variable, or a better way to set for a variable that can be undefined or null.我遇到了这个对话,想知道是否有其他人有解决方案来取消设置变量,或者有更好的方法来设置可以未定义或为空的变量。

Updated with code: I'm working on my backend Admin section.更新了代码:我正在处理我的后端管理部分。 For various reason I did not use the SonataAdmin and wanted to build my own.由于各种原因,我没有使用 SonataAdmin,而是想自己构建。 In this area I can view a summary all users and in some pages I can view and edit parts of a user's profile.在此区域中,我可以查看所有用户的摘要,在某些页面中,我可以查看和编辑用户个人资料的部分内容。 I have a header twig file that contains links to the summary pages, then another row that contains links to the user's profile sections.我有一个标题树枝文件,其中包含指向摘要页面的链接,然后是另一行包含指向用户个人资料部分的链接。

For my route admin_profile_show - I need to include the user to load the correct page, but if the user is null here, this will cause an error because it cannot reach the username property.对于我的路由 admin_profile_show - 我需要包含用户以加载正确的页面,但如果用户在这里为空,这将导致错误,因为它无法访问用户名属性。

{% if user is defined %}
    {{ path('admin_profile_show', { 'username': user.username}) }}
    ...other routes and links
{% endif %}

I guess my real problem is that I allowed a case where the username can be null so it is defined, but null.我想我真正的问题是我允许用户名可以为空的情况,因此它被定义,但为空。 Before I change the logic of other files, I wanted to know if there is an unset variable that would work as follows:在我更改其他文件的逻辑之前,我想知道是否有一个未设置的变量可以按如下方式工作:

{% if user is null %}
    {% unset user %}
{% endif %}

As F21 mentioned above, it's not a good practice modyifing data in template, but if you really want to to this, you can simply extend Twig to do this work (I mean you can create you own functons by extending it ), Here is a simple example:正如上面提到的 F21,在模板中修改数据不是一个好习惯,但如果你真的想这样做,你可以简单地扩展 Twig 来完成这项工作(我的意思是你可以通过扩展它来创建你自己的函数),这是一个简单的例子:

class STWebExtension extends \Twig_Extension
{

    /**
     * @return array|\Twig_SimpleFunction
     */
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('array_keys', [$this, 'array_keys']),
        ];
    }

    /**
     * @param array $input
     * @param null $search_value
     * @param null $strict
     * @return array
     */
    public function array_keys(array $input, $search_value = null, $strict = null)
    {
        return array_keys($input, $search_value, $strict); //here we are simply returning PHP's built-in array_keys function.
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'st_web_extension';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM