这可能听起来有点奇怪,但这是一个特殊的情况:
我在$content
中有一个HTML字符串,我想在此内容的head
标签中呈现stylesheet
块。这是我的文件:
$response = new Response();
$response->setContent($content);
$response->headers->set('Content-Type', 'text/html');
// returns the HTTP headers followed by the content
return $response;
,我想加上:
{% stylesheets output='compiled/css/*.css' 'css/all.less' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
知道如何实现吗?
编辑:就像@CarlMarkham建议的那样,我试过了:
$loader = new 'Twig_Loader_Array(array(
'index.html' => $content."{% stylesheets output='compiled/css/*.css' 'css/all.less' %}
<link rel='stylesheet' href='{{ asset_url }}' />
{% endstylesheets %}",
));
$twig = new 'Twig_Environment($loader);
return $twig->render('index.html');
但是我得到了这个错误:
Unknown tag name "stylesheets" in index.html
也试过这个:
$loader = new 'Twig_Loader_Array(array(
'index.html' => $content.'<link rel="stylesheet" href="{{ asset("compiled/css/*.css") }}">'
));
$twig = new 'Twig_Environment($loader);
return $twig->render('index.html');
但是我得到了:
The function "asset" does not exist in index.html
看看astic。您应该能够从php内构建html(第一个代码块点击php标签)。这个例子是针对JS的,但对于CSS也是同样的概念。现在您有了字符串,您可以将其添加到内容中。