其实
public function styleAction()
{
// Fetch it from service or whatever strategy you have
$backgroundColor = '#ff0000';
return $this->render(
'AcmeMyBundle::somefile.css.twig',
['backgroundColor' => $backgroundColor],
['Content-Type' => 'text/css']
);
}
应该更喜欢这个
/**
* @Route("/css/style", name="style")
* @param Request $request
* @return Response
*/
public function styleAction(Request $request)
{
$firstColor = 'rgba(130, 30, 30, 0.9)';
/** @var Response $response */
$response = $this->render(':css:style.css.twig', [
'firstColor' => $firstColor,
]);
$response->headers->set('Content-Type', 'text/css');
return $response;
}
请注意,我有注解,因为我使用的Symfony 3.但我的代码示例中最重要的事情是,为响应我设置Content-Type转换为“text/css”。
对我来说,改变HTML类等或模板而不是改变(假设内联)CSS(事实上,这不是Twig的目的)可能更有意义。但是,当然,您可以在另一个模板中包含模板,它位于您链接到的文档中。你这样做了什么问题? –