如何使用twig的源函数加载css文件?

时间:2016-12-13 15:15:58

标签: css twig

我是twig初学者,我想获取CSS文件内容而不是要清除的路径,此文件位于: -

 <?php

$mode=$_POST['mode'];

if ($mode=='true') //mode is true when button is enabled 
{
    //Retrive the values from database you want and send using json_encode
    //example
    $message='Hey my button is enabled!!';
    $success='Enabled';
    echo json_encode(array('message'=>$message,'$success'=>$success));
}

else if ($mode=='false')  //mode is false when button is disabled
{
    //Retrive the values from database you want and send using json_encode
    //example
    $message='Hey my button is disabled!!';
    $success='Disabled';
    echo json_encode(array('message'=>$message,'success'=>$success));

} 
 ?>

var是一个动态变量,所以我使用了twig的source函数,但似乎我没有指向正确的路径。这是我的代码:

Bundle/Ressources/public/css/'~var~'/main.css 

请帮忙。

1 个答案:

答案 0 :(得分:0)

你是对的。您没有对路径使用正确的表示法。

在您的情况下,如果您的包名为 FooBundle ,则应使用:

{{ source('@FooBundle/Resources/public/css/'~var~'/main.css',  ignore_missing = true)  }} 

如果您的文件位于Resources/views/内,例如Resources/views/foo/bar.html.twig,您可以使用更短的变体:

{{ source('FooBundle:foo:bar.html.twig',  ignore_missing = true)  }} 

或:

{{ source('@Foo/yourTemplate.html.twig',  ignore_missing = true)  }}

另请参阅:http://symfony.com/doc/current/templating/namespaced_paths.html