将变量发送到Symfony模板文件

时间:2022-12-22 11:27:56

I'm trying to send a variable to a twig file which is not in the typical location, Usually I'm loading views by specifying their path through the Bundle but the file I want to send a variable to is not, hierarchically, on the same level as the other twig templates.

我正在尝试将变量发送到不在典型位置的twig文件中。通常我通过指定它们通过Bundle的路径来加载视图但是我要发送变量的文件不是分层次地在与其他树枝模板相同的水平。

I've a controller which looks like the following:

我有一个控制器,如下所示:

public function fooAction(Request $request)
{

   *//Query*

    return $this->render('Bundle:file.html.twig', array('varToSend' => $queryResult));
}

I'm pretty sure the Bundle:file.html.twig is wrong but I don't know how else to specify the relevant path in twig.

我很确定Bundle:file.html.twig是错误的,但我不知道如何在twig中指定相关路径。

2 个解决方案

#1


You have to use a path like that :

你必须使用这样的路径:

return $this->render('Bundle:Something:file.html.twig', array(
    'varToSend' => $queryResult
));

And put your file in this folder :

并将您的文件放在此文件夹中:

src/Bundle/Resources/views/Something/file.html.twig

#2


Twig you would get from container would not work with arbitrary paths, but you can initialize your own twig:

你从容器中获得的Twig不能使用任意路径,但你可以初始化自己的树枝:

$twig = new \Twig_Environment(new \Twig_Loader_String());
$rendered = $twig->render(
  file_get_contents('/path/to/your/file.html.twig'),
  array('varToSend' => $queryResult)
);

If you need this in more than one place, consider making it a Symfony service in order to not initialize Twig Environment every time.

如果您需要在多个地方使用它,请考虑将其设置为Symfony服务,以便不每次都初始化Twig环境。

Note that renderer in this case won't have any of Symfony's Twig Extensions, you'll have to add them manually.

请注意,在这种情况下,渲染器不会包含任何Symfony的Twig Extensions,您必须手动添加它们。

If possible, try to avoid this, and put templates into app/Resources/views or src/AppBundle/Resources/views directories.

如果可能,请尝试避免这种情况,并将模板放入app / Resources / views或src / AppBundle / Resources / views目录中。

#1


You have to use a path like that :

你必须使用这样的路径:

return $this->render('Bundle:Something:file.html.twig', array(
    'varToSend' => $queryResult
));

And put your file in this folder :

并将您的文件放在此文件夹中:

src/Bundle/Resources/views/Something/file.html.twig

#2


Twig you would get from container would not work with arbitrary paths, but you can initialize your own twig:

你从容器中获得的Twig不能使用任意路径,但你可以初始化自己的树枝:

$twig = new \Twig_Environment(new \Twig_Loader_String());
$rendered = $twig->render(
  file_get_contents('/path/to/your/file.html.twig'),
  array('varToSend' => $queryResult)
);

If you need this in more than one place, consider making it a Symfony service in order to not initialize Twig Environment every time.

如果您需要在多个地方使用它,请考虑将其设置为Symfony服务,以便不每次都初始化Twig环境。

Note that renderer in this case won't have any of Symfony's Twig Extensions, you'll have to add them manually.

请注意,在这种情况下,渲染器不会包含任何Symfony的Twig Extensions,您必须手动添加它们。

If possible, try to avoid this, and put templates into app/Resources/views or src/AppBundle/Resources/views directories.

如果可能,请尝试避免这种情况,并将模板放入app / Resources / views或src / AppBundle / Resources / views目录中。