0

I`am rendering a twig template in symfony2 and its possible to get a variable with value of array type but i am trying to preview the variable and this error pops:

Array to string conversion ...

So my qustion is not how to print out the array, but is it possible in twig to disable this error, so when an array comes and it cannot be outputed, to set an empty value.

In config.yml, under twig i have set

strict_variables: false

But this dose not include printing arrays.

EDIT:

In controller I pass this array to twig, for example:

'data' => array(
            'description' => array(
                  'desc1', 
                  'desc2'
            )
         );

In twig I am trying to print {{ data['description'] }} and I got the following error:

Array to string conversion ...

This is the normal error that must pop, but I need if I try to print this a no error to pop but instead I need to print nothing(empty value), same way like trying to print an non existing variable with twig set with

strict_variables: false

ANSWER:

Make a custom function:

public function getFunctions()
{
    return array(
       'to_string' => new \Twig_Function_Method($this, 'toString')
    );
}

public function toString($data) {
    if(is_array($data)) {
        return '';
    }
    return $data;
}

And in twig template:

{{ to_string(data['description']) }}
4
  • To print an array you can use print_r, var_export or var_dump, but you should access the data inside with the array indexes. Post your array to show you how to access data Commented Sep 11, 2014 at 10:21
  • im confused... are you trying to print the contents of the array, or display nothing if it is an array? Commented Sep 11, 2014 at 10:23
  • Fix the error, do not walk around it. It is not acceptable to echo a array and try to supress it. Please add you code (minimal example) Commented Sep 11, 2014 at 10:26
  • Why don't you iterate over the array an print out every single value? Commented Sep 11, 2014 at 10:32

1 Answer 1

0

You have two desc: desc1 and desc2, what do you want to print? If both you have to use this notation (for instance)

{{ data['description'][desc1] }} - {{ data['description'][desc2] }}

More in general:

{% for desc in data['description'] %}
    {{ desc }}
{% endfor %}

EDIT

Ok, now I got it (from your comment):

{% if data['description'] is iterable %}
    //echo here an empty string
{% else %}
    {{ data['description'] }}
{% endif %}
Sign up to request clarification or add additional context in comments.

6 Comments

I want to print {{ data['description'] }} and I don't want the error for Array to string conversion ... Just an empty value to be printed.
data['description'] isn't a scalar value so, "I want to print it out" is a nonsense statement
Yes that is the problem, I need something in twig, where if I am trying to echo an array the response to be an empty value(string)
Is theyere another way, becouse I could have a lot of this and if i have to do it every time it is not so cool. Something like: strict_arrays: false
@user3686982: you have to use an iteration statement like for or while. BTW this answered your question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.