M0NSTERC4T
@M0NSTERC4T
Front End Engineer in Live Typing

Как подключить css к шаблону tpl?

В случае запуска tpl шаблона из функции не применяются css стили. В случае запуска этого шаблона из браузера все работает.

Из функции:
e59848dc3db947c082dd3fa55aa082ae.png

Из браузера:
33fb383c756841ecbb6402af9bab3827.png
//index.php

include_once 'Router.php';

function new_template(){
    $smarty = new Smarty(); 
	$smarty->template_dir = 'templates';
	$smarty->compile_dir = 'templates_c';
	$smarty->config_dir = 'configs';
	$smarty->cache_dir = 'cache';
	return $smarty;
}

$content = Router::process($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
$smarty = new_template();
$smarty->assign('content', $content);
$smarty->display('template-1.tpl');
?>


//template-1.tpl

<!DOCTYPE html>
<html lang="ru">
  <head>
    <meta charset="utf-8">
    <title>Template</title>
    <link rel="stylesheet" type="text/css" href="D:/Server/OpenServer/domains/php.loc/templates/style.css">
  </head>
  <body>
      <div class="header">Head</div>
      <div>{$content}</div>
      <div class="footer">Footer</div>
  </body>
</html>


//style.css
.header {
    background-color: black;
}
  • Вопрос задан
  • 2359 просмотров
Решения вопроса 1
@magazovski
<link rel="stylesheet" type="text/css" href="D:/Server/OpenServer/domains/php.loc/templates/style.css">


У вас неправильно указан путь

в первом случае он превращается в d:/Server/OpenServer/domains/php.loc/templates/style.css - это вообще непонятно как итереритровать

во втором file://D:/Server/OpenServer/domains/php.loc/templates/style.css - это путь к локальному файлу

можно использовать относительный путь, тогда php.loc подставится

<link rel="stylesheet" type="text/css" href="templates/style.css">


В итоге получится php.loc/templates/style.css
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 2
@onpavlov
Инженер, программист. JS, PHP, Python, Golang
А что у вас делает тег img в href где подключаете css?
Ответ написан
// Создаем и настраиваем Смарти
		$this->smarty = new Smarty();
		$this->smarty->compile_check = $this->config->smarty_compile_check;
		$this->smarty->caching = $this->config->smarty_caching;
		$this->smarty->cache_lifetime = $this->config->smarty_cache_lifetime;
		$this->smarty->debugging = $this->config->smarty_debugging;
		$this->smarty->error_reporting = E_ALL & ~E_NOTICE;

		// Берем тему из настроек
		$theme = $this->settings->theme;
		

		$this->smarty->compile_dir = $this->config->root_dir.'/compiled/'.$theme;
		$this->smarty->template_dir = $this->config->root_dir.'/design/'.$theme.'/html';		

		// Создаем папку для скомпилированных шаблонов текущей темы
		if(!is_dir($this->smarty->compile_dir))
			mkdir($this->smarty->compile_dir, 0777);
						
		$this->smarty->cache_dir = 'cache';


<link rel="stylesheet" href="design/{$settings->theme|escape}/css/base.css">


вот вариант для понимания, вообще все описано в самом смарти....
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы