如何在Twig模板中定义css链接的href

如何在Twig模板中定义css链接的href,twig,php,template-engine,Twig,Php,Template Engine,我有以下应用程序结构: index.php <templates> |____________ <theme1> | index.html <css> |______________style.css index.php |____________ | index.html |______________style.css 在index.h

我有以下应用程序结构:

index.php
<templates>
|____________ <theme1>
              |
              index.html 
              <css>
              |______________style.css 
index.php
|____________ 
|
index.html
|______________style.css
在index.html中,输入以下代码:

<!DOCTYPE html>
<!--[if IE 9]><html class="lt-ie10" lang="en" > <![endif]-->
<html class="no-js" lang="en" >

<head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Foundation 5</title>

      <!-- If you are using the CSS version, only link these 2 files, you may add app.css to use for your overrides if you like -->
      <link rel="stylesheet" href="css/style.css">
<?php
require_once("../twig/lib/Twig/Autoloader.php");
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('templates/theme1/');
$twig = new Twig_Environment($loader, array(
                              'cache' => 'compilation_cache',
                              ));
$template = $twig->loadTemplate('index.html');
echo $template->render(array('desc' => 'variables', 'go' => 'here'));

基金会5
在index.php中,输入以下代码:

<!DOCTYPE html>
<!--[if IE 9]><html class="lt-ie10" lang="en" > <![endif]-->
<html class="no-js" lang="en" >

<head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Foundation 5</title>

      <!-- If you are using the CSS version, only link these 2 files, you may add app.css to use for your overrides if you like -->
      <link rel="stylesheet" href="css/style.css">
<?php
require_once("../twig/lib/Twig/Autoloader.php");
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('templates/theme1/');
$twig = new Twig_Environment($loader, array(
                              'cache' => 'compilation_cache',
                              ));
$template = $twig->loadTemplate('index.html');
echo $template->render(array('desc' => 'variables', 'go' => 'here'));

我用有限的解决方案解决了它:如下所示:

在index.php中,我使用了`$\u SERVER['REQUEST\u URI'],并将其与主题路径连接起来,然后在模板文件index.html中使用的细枝变量中设置它们:

<?php
/** Config **/
$config = array(
    'themeURI' => 'templates/theme1/',
);
/*****************/
require_once("../twig/lib/Twig/Autoloader.php");
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem($config['themeURI']);
$twig = new Twig_Environment($loader, array(
'cache' => 'compilation_cache',
'auto_reload' => true,
));
$template = $twig->loadTemplate('index.html');
echo $template->render(array('desc' => 'variables', 'themePath' => $_SERVER['REQUEST_URI'].$config['themeURI']));
试试这个

<?php
require_once("../twig/lib/Twig/Autoloader.php");
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('templates/theme1/');
$twig = new Twig_Environment($loader, array(
                              'cache' => 'compilation_cache',
                              ));
$template = $twig->loadTemplate('index.html');
echo $template->render(array('desc' => 'variables', 'go' => 'here','assets'=>'themePath/theme1/assets'));

也许你可以使用css的绝对路径?这也使得将它们添加到子路径()中的页面变得简单。@user3557327这不是一个选项,因为从不同的URL访问网站会破坏设计,即
localhost、127.0.0.1、domain.com、www.domain.com
etcAs只要它始终位于域的根目录中,就可以使用´/path/to/style.css´(注意起始斜杠)而不指定域。