N
N
Nikita Filatov2015-07-16 20:10:14
PHP
Nikita Filatov, 2015-07-16 20:10:14

How to connect css to tpl template?

In case of launching a tpl template from a function, css styles are not applied. If you run this template from a browser, everything works.
From function:
e59848dc3db947c082dd3fa55aa082ae.png
From browser:
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;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
magazovski, 2015-07-17
@M0NSTERC4T

<link rel="stylesheet" type="text/css" href="D:/Server/OpenServer/domains/php.loc/templates/style.css">

You have incorrectly specified the path
in the first case, it turns into d:/Server/OpenServer/domains/php.loc/templates/style.css - it's generally unclear how to iterate
in the second file://D:/Server/OpenServer/domains/ php.loc/templates/style.css is the path to the local file
, you can use a relative path, then php.loc will be substituted
The result will be php.loc/templates/style.css

O
Oleg Pavlov, 2015-07-16
@onpavlov

And what does the img tag in href do for you where you include css?

A
Alexey Skleinov, 2015-07-16
@lexskal

// Создаем и настраиваем Смарти
    $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';

Here is an option for understanding, in general, everything is described in the smart itself ....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question