A
A
Alexey2015-10-31 15:36:23
PHP
Alexey, 2015-10-31 15:36:23

How to set dynamic title MVC?

I can't figure out how to dynamically set the title on the page. I am writing a self-made CMS using the MVC template.
Here is the view class, in the render method I include the main template

class View
{
    public $title;
    public $template_path;
    public $view_path;


    public function __construct()
    {
        $module = strtolower(Settings::get('module'));
        $modules = Settings::get('modules');

        $template_path = Settings::get('root_path').$modules[$module]['template'];
        $this->template_path = $template_path;

        $controller = strtolower(Settings::get('controller'));
        $view_path = Settings::get('root_path').'/modules/'.$module.'/'.'views'.'/'.$controller.'/';

        $this->view_path = $view_path;
    }

    public function render($view, Array $data = null)
    {
        if(!empty($data))
        {
            extract($data);
        }

        include $this->template_path;

    }

Next, in the main template, I connect the content part, which I passed to the render method as the first parameter
<?php
  require_once APP_PATH.'/Core/URL.php';
  require_once APP_PATH.'/Core/Request.php';
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <link rel="stylesheet" type="text/css" href="/css/main.css">
  <title><?=$this->title?></title>
</head>
<body>
  <div id="container">
    <div id="wrapper">
      <div id="clear">
        <?php include 'header.php'; ?>
        <?php include $this->view_path.$view; ?>
      </div>
 </div>
    <footer>

    </footer>
</div>
</body>
</html>

Here in this content part, I would like to set the title of the page or, for example, add meta tags.
<?php
    $this->title = "Главная";
?>


<aside  class="bar left">

</aside>
<aside class="bar right">

</aside>
<main>
    <div>Главная </div>
</main>

I can’t think of a way to solve this problem, I tried it through output buffering, but somehow it didn’t grow together

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dk-web, 2015-10-31
@Feramon4ik

So insert $data['title'] or $data->title... into the template and pass... you are on the right track

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question