簡體   English   中英

如何在Twig模板中定義CSS鏈接的href

[英]How to define the href of css link in Twig template

我具有以下應用程序結構:

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。 我使用的版本1.16.0

我用有限的解決方案解決了它:

在index.php中,我使用了$$ SERVER ['REQUEST_URI']並將其與主題路徑連接起來,然后將它們設置在模板文件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樹枝自動加載程序的路徑。

嘗試這個

<?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">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM