我有以下app结构:
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。
答案 0 :(得分:0)
我用有限的解决方案解决了它:如下:
index.php中的我使用`$ _SERVER [&#39; REQUEST_URI&#39;]并将其与主题路径连接,然后将它们设置在模板文件index.html中使用的Twig变量中:
<?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']));
然后在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="{{themePath}}css/style.css">
此解决方案中的限制在根目录的子目录的php文件中使用时,应该修改值$config['themeURI']
以相对匹配新路径,并且可能需要更改require
树枝自动装弹机的路径。
答案 1 :(得分:0)
试试这个
<?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'));
这是在你的模板中
<!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="{{assets}}css/style.css">