我如何使用Twig在* .html中打印* .json文件的值

我想在屏幕上通过“ for ... in ...”循环显示以下值,但是在解码* .json文件和实现时遇到麻烦。

// Example of *.json file
[{ "ano":       "2019","nomeCurso": "ABI - CIÊNCIAS BIOLÓGICAS","idCurso":   "01ABI","grupo":     "PISM Grupo A","campus":    "JUIZ DE FORA","turno":     "DIURNO","idGrupo":    75} }]

我有一个index.php:

<?php

    // Putting the 'autoload' file of 'vendor' directory to use Twig
    require 'vendor/autoload.php';

    // Declaring directory 'views' (used to show using Twig)
    $loader = new Twig_Loader_Filesystem('views');

    // Loading 'views' directory
    $twig = new Twig_Environment($loader);

    // Create a filter to use in the code
    $md5Filter = new Twig_SimpleFilter('md5',function ($string) {
        return md5($string);
    });

    // Add filter
    $twig->addFilter($md5Filter);

    // Decode the *.json file
    $classificacoes = file_get_contents("PISM.json");
    $classificacao = json_decode($classificacoes,true);

    $twig->render('index.html.twig',array($classificacoes));

?>

...和index.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="Twig Table Template">
        <meta name="author" content="fabricioguidine">
        <meta name="keywords" content="twig table template">
        <title>Twig Table Template</title>
    </head>

    <body></body>

    <!-- for each value in "*.json" add a table with especified values -->

    {% for classicacao in classificacoes %}
        {{ classificacao.ano }}
        {{ classificacao.idCurso }}
        {{ classificacao.grupo }}
        {{ classificacao.campus }}
        {{ classificacao.turno }}
        {{ classificacao.idGrupo }}
    {% endfor %}

    </body>

    <footer></footer>

</html> 

请注意,我是该框架中的新手,可能是愚蠢的实现错误。

您能帮我吗?

tb_eleven 回答:我如何使用Twig在* .html中打印* .json文件的值

在我看来,您正在将错误的变量传输到Twig模板,并使用常规数组而不是哈希数组。

代替

$twig->render('index.html.twig',array($classificacoes));

尝试

$twig->render('index.html.twig',array('classificacoes' => $classificacao));
本文链接:https://www.f2er.com/3163930.html

大家都在问