简体   繁体   English

Symfony4 '错误:变量不存在',当我在模板中调用一个变量时

[英]Symfony4 'ERROR : Variable does not exist', when i call a variable in my template

I'm blocked on error with Symfony 4.3.我因 Symfony 4.3 的错误而被阻止。

I have installed the bundle : CMENGoogleChartsBundle我已经安装了捆绑包:CMENGoogleChartsBundle

Installation is ok, no errors.安装正常,没有错误。

So i wanted try it, i have create a new controller "TestChartController" and a new template "testChart/index.html.twig".所以我想尝试一下,我创建了一个新控制器“TestChartController”和一个新模板“testChart/index.html.twig”。

I have use the code of tutorial : https://github.com/cmen/CMENGoogleChartsBundle/blob/master/Resources/doc/basic_usage.md我使用了教程的代码: https : //github.com/cmen/CMENGoogleChartsBundle/blob/master/Resources/doc/basic_usage.md

The Case 1.案例一。

Here is i have in my IDE :这是我的 IDE 中的:

Controller控制器

<?php

namespace App\Controller;

use CMEN\GoogleChartsBundle\GoogleCharts\Charts\PieChart;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;

class TestChartController extends AbstractController
{
    /**
     * @var Environment
     **/
    private $twig;

    public function __construct(Environment $twig)
    {
        $this->twig = $twig;
    }

    /**
     * @Route("/test/chart", name="testChart")
     */
    public function index()
    {
        return $this->render('testChart/index.html.twig', [
            'controller_name' => 'TestChartController',
        ]);
    }

    public function indexAction()
    {
        $pieChart = new PieChart();
        $pieChart->getData()->setArrayToDataTable(
            [['Task', 'Hours per Day'],
                ['Work',     11],
                ['Eat',      2],
                ['Commute',  2],
                ['Watch TV', 2],
                ['Sleep',    7]
            ]
        );
        $pieChart->getOptions()->setTitle('My Daily Activities');
        $pieChart->getOptions()->setHeight(500);
        $pieChart->getOptions()->setWidth(900);
        $pieChart->getOptions()->getTitleTextStyle()->setBold(true);
        $pieChart->getOptions()->getTitleTextStyle()->setColor('#009900');
        $pieChart->getOptions()->getTitleTextStyle()->setItalic(true);
        $pieChart->getOptions()->getTitleTextStyle()->setFontName('Arial');
        $pieChart->getOptions()->getTitleTextStyle()->setFontSize(20);

        return $this->render('testChart/index.html.twig', ['piechart' => $pieChart]);
    }
}

Template :模板

{% extends 'base.html.twig' %}

{% block title %}Hello TestChartController !{% endblock %}

    {% block body %}
        <!-- index.html.twig -->
        <html>
        <head></head>
        <body>
        <div id="div_chart"></div>
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <script type="text/javascript">
            {{ gc_draw(piechart, 'div_chart') }}
        </script>
        </body>
        </html>
    {% endblock %}

But Symfony response with fatal error :但是 Symfony 响应致命错误:

Img error图像错误

I have tryed with a simple string in my controller :我在控制器中尝试了一个简单的字符串:

$test = 'test' And i ave modified the render : $test = 'test'我修改了渲染:

return $this->render('testChart/index.html.twig', ['test' => $test]);

Finally i have dump(test) in my template.最后,我的模板中有dump(test)

Same error.同样的错误。

NB: All other templates function correctly.注意:所有其他模板都可以正常运行。

Thx for your future help.感谢您未来的帮助。

RESOLVE: Moove this on the second function indexAction解决:将其移到第二个函数 indexAction 上

/**
 * @Route("/test/chart", name="testChart")
 */

Symfony 4.3.2 Symfony 4.3.2

W10 W10

Try using this controller.尝试使用此控制器。

namespace App\Controller;

use CMEN\GoogleChartsBundle\GoogleCharts\Charts\PieChart;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;

class TestChartController extends AbstractController
{


    /**
     * @var Environment
     **/
    private $twig;

    public function __construct(Environment $twig)
    {
        $this->twig = $twig;
    }

    /**
     * @Route("/test/chart", name="testChart")
     */
    public function index()
    {
        $pieChart = new PieChart();
        $pieChart->getData()->setArrayToDataTable(
            [['Task', 'Hours per Day'],
                ['Work',     11],
                ['Eat',      2],
                ['Commute',  2],
                ['Watch TV', 2],
                ['Sleep',    7]
            ]
        );
        $pieChart->getOptions()->setTitle('My Daily Activities');
        $pieChart->getOptions()->setHeight(500);
        $pieChart->getOptions()->setWidth(900);
        $pieChart->getOptions()->getTitleTextStyle()->setBold(true);
        $pieChart->getOptions()->getTitleTextStyle()->setColor('#009900');
        $pieChart->getOptions()->getTitleTextStyle()->setItalic(true);
        $pieChart->getOptions()->getTitleTextStyle()->setFontName('Arial');
        $pieChart->getOptions()->getTitleTextStyle()->setFontSize(20);

        return $this->render(
            'testChart/index.html.twig', [
                'piechart' => $pieChart
            ]
        );
    }
}

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

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