shuba.ivan
shuba.ivan

Reputation: 4061

Symfony Doctrine Visible in Twig

I have field type

 /**
 * @var array
 *
 * @ORM\Column(name="raw", type="json_array", nullable=true)
 */
private $raw;

and I set in this filed data format json

 $jsonRaw = array(
                    'repository_language' => $repository['language'],
                    'used_languages' => $languages,
                    'commits' => $commits,
                    'scooped_branch' => $sha,
                    'first_commit' => empty($this->committer[0]['author']) ? null : $this->committer[0],
                    'last_commit' => empty($this->committer[1]['author']) ? null : $this->committer[1]
                );

and set in entity

$entity->setRaw(json_encode($jsonRaw));

and then I have data in this field field_data and I rendering this field in template but have error

                {% for languages in project.raw.used_languages|json_decode %}
                    <div class="row col-sm-2" style="position: relative; text-align: center; margin: 0; padding: 0;">
                        <small style="color: #00312f;">
                            <strong>{{ languages.lang }}</strong>:<br>
                            {{ languages.percent|round(1, 'floor') }}%<br>
                        </small>
                    </div>
                {% endfor %}

Impossible to access an attribute ("used_languages") on a string variable ("{"repository_language":"PHP","used_languages":[{"lang":"PHP","percent.....

why?? I this maybe twig not friend with json and create custom twig filter

class VarsExtension extends Twig_Extension
{
protected $container;

public function __construct(ContainerInterface $container)
{
    $this->container = $container;
}

public function getName()
{
    return 'some.extension';
}

public function getFilters() {
    return array(
        'json_decode'   => new \Twig_Filter_Method($this, 'jsonDecode'),
    );
}

public function jsonDecode($str) {
    return json_decode($str);
}
}

and try use

               {% for languages in project.raw.used_languages|json_decode %}
                    <div class="row col-sm-2" style="position: relative; text-align: center; margin: 0; padding: 0;">
                        <small style="color: #00312f;">
                            <strong>{{ languages.lang }}</strong>:<br>
                            {{ languages.percent|round(1, 'floor') }}%<br>
                        </small>
                    </div>
                {% endfor %}

and still have error

Impossible to access an attribute ("used_languages") on a string variable ("{"repository_language":"PHP","used_languages":[{"lang":"PHP","percent":75.976812123425},{"lang":"JavaScript","percent":13.194136518949},{"lang":"CSS","percent":10.829051357625}],

help please, I have not idea(

Upvotes: 1

Views: 392

Answers (1)

Matteo
Matteo

Reputation: 39380

You can simply apply the filter json_decode before access to the element by key, as @Snoozer example:

{% set raw = project.raw | json_decode %} {{ raw.used_languages }}

Upvotes: 1

Related Questions