【发布时间】:2014-07-08 22:56:27
【问题描述】:
我有以下应用结构:
index.php
<templates>
|____________ <theme1>
|
index.html
<css>
|______________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">
在 index.php 中如下代码:
<?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'));
我意识到 Twig 可以包含 css 文件的静态路径并对其进行修改。但是,我可以在href="{{?}}css/style.css"> 中的路径之前添加什么作为变量或其他内容以使其可供最终显示的页面访问?
注意 我的应用程序非常简单,它不使用 symfony。我使用的是 twig 的 1.16.0 版本。
【问题讨论】:
-
也许你可以使用css的绝对路径?这也使得将它们添加到子路径中的页面变得简单 (example.com/foo/bar/something.html)。
-
@user3557327 这不是一个选项,因为从不同的 URL 访问网站会破坏设计,即
localhost, 127.0.0.1, domain.com, www.domain.com等 -
只要它始终位于域的根目录中,您可以直接使用“/path/to/style.css”(注意起始斜杠)而不指定域。
标签: twig php twig template-engine