Viewed   75 times

I'm getting this error in a app I am migrating from SF2.0.x to SF2.7:

[1] SymfonyComponentDebugExceptionFatalErrorException: Compile Error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
    at n/a
        in /var/www/html/reptooln_admin/app/cache/dev/twig/68/7f/63589dd3687cb849dd68e6b6c10aa99eda1d82f95a5f3ac52a864d200499.php line 39

I don't know what is failing or how to fix this so I need some advise. This is the line at cache file where the Stacktrace is reported:

    if ((((empty((isset($context["form_action"]) ? $context["form_action"] : $this->getContext($context, "form_action"))) == true) || (isnull((isset($context["form_action"]) ? $context["form_action"] : $this->getContext($context, "form_action"))) == true)) || (isset((isset($context["form_action"]) ? $context["form_action"] : $this->getContext($context, "form_action"))) == false))) {
                echo " ";
                $context["form_action"] = "";
                echo " ";

What I have this TwigExtension:

class PDOneTwigExtension extends Twig_Extension
{
    public function getFilters()
    {
        return array(
            'var_dump'   => new Twig_Filter_Function('var_dump'),
            'empty' => new Twig_Filter_Function('empty', array($this, 'is_empty')),
            'isset' => new Twig_Filter_Function('isset', array($this, 'is_set')),
            'isnull' => new Twig_Filter_Function('isnull', array($this, 'is_null')),
            'ucfirst' => new Twig_Filter_Function('ucfirst', array($this, 'uc_first')),
            'ucwords' => new Twig_Filter_Function('ucwords', array($this, 'uc_words')),
            'count' => new Twig_Filter_Function('count', array($this, 'co_unt')),
            'sizeof' => new Twig_Filter_Function('sizeof', array($this, 'size_of')),
            'concat' => new Twig_Filter_Function('concat', array($this, 'concat')),
            'in_array' => new Twig_Filter_Function('in_array', array($this, 'inarray')),
            'array' => new Twig_Filter_Function('array', array($this, 'array_')),
            'add_to_array' => new Twig_Filter_Function('add_to_array', array($this, 'add_to_array')),
            'replace' => new Twig_Filter_Function('replace', array($this, 'replace')),
            'htmlentitydecode' => new Twig_Filter_Function('htmlentitydecode', array($this, 'htmlentitydecode'))
        );
    }

    public function is_empty($sentence)
    {
        return empty($sentence) ? true : false;
    }

    // rest of methods goes here

    public function getName()
    {
        return 'pdone_twig_extension';
    }
}

And I'm using at template as follow:

{% if form_action|empty == true or form_action|isnull == true or form_action|isset == false %} {% set form_action = '' %} {% endif %}

Where could be the issue here? Any advice?

 Answers

3

From documentation:

isset() only works with variables as passing anything else will result in a parse error.

You're not directly passing a variable to isset(). So you need to calculate the value first, assign it to a variable, and then pass that to isset().

For example, what you're doing at the moment is something like:

if(isset($something === false)) { } // throws a parse error, because $something === false is not a variable

What you need to do instead is:

$something = false;
if(isset($something)) { ... }
Saturday, December 10, 2022
3

your code is so LONG that you can not really read it:

isset($_POST['idPresented']

is missing his ')' so change

isset($_POST['idPresented'] into isset($_POST['idPresented'])

Wednesday, September 14, 2022
 
5

Its not possible to access any PHP function inside Twig directly.

What you can do is write a Twig extension. A common structure is, writing a service with some utility functions, write a Twig extension as bridge to access the service from twig. The Twig extension will use the service and your controller can use the service too.

Take a look: http://symfony.com/doc/current/cookbook/templating/twig_extension.html

Cheers.

Friday, September 30, 2022
 
3

As mentioned in the comments (and the error message), you cannot pass the result of an expression to isset.

You can use multiple isset calls, or reverse the logic of your if/else block and pass multiple parameters to isset, which i think is the cleanest solution:

//true if both are set
if(isset($size, $color)) {
    $style = 'font-size : ' . $size . ';color:' . $color;
}else{
    $style = '';
}

You can clean this up a little further by setting the default value first, thus avoiding the need for an else section:

$style = '';
if(isset($size, $color)) {
    $style = 'font-size : ' . $size . ';color:' . $color;
}

You could even use a ternary, though some people find them harder to read:

$style = isset($size, $color) ? 'font-size : ' . $size . ';color:' . $color : '';
Thursday, August 4, 2022
 
2

The Twig is not part of acme bundle, Twig is vendor bundle itself and therefore the Error is correct. There is not such namespace as acme.twig.extension.loader

The fixed code would be:

# services.yml
services:
    twig.extension.stringloader:
        class: Twig_Extension_StringLoader
        tags:
            - { name: twig.extension }

This can be added to /app/config/config.yml to use to in each bundle or add it into your bundle folder into /Resources/config/services.yml to use in only in certain bundle.

Then in twig templates use them as:

{{ include(template_from_string(page.template)) }}

The above works for me in Symfony v2.5

Saturday, September 24, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :