flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.63k stars 409 forks source link

How change layout title #300

Closed Iazzetta closed 7 years ago

Iazzetta commented 7 years ago

How I change the layout title? I render my layout and can't change the title.

Flight::render('layout', array('title' => "Home"));

when I try change nothing happens

Flight::route('/login', function(){
      Flight::render('pages/login', array('title' => "Login"));
});

and my html:

<head>
    <title>MKPlace - <?php echo $title; ?></title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/app.css">
</head>
imbr commented 7 years ago

1. You have correct .htaccess file?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

2. You need to set correct folder root.

For example: Flight::render('../pages/login', array('title' => "Login"));

Or by the views path (recommended):

Flight::set('flight.views.path', 'pages/');
(...)
Flight::render('login', array('title' => "Login"));

L ;)

Iazzetta commented 7 years ago

@imbr I have correct .htaccess file and correct root folder. The unique difference, is my architecture.

My views folder:

/views

I need set all views folders or i can't create a directory?

Thanks!

imbr commented 7 years ago

Examples of use for your folder structure:

<?php

    require 'flight/Flight.php';

    Flight::set('flight.views.path', 'views/');

    Flight::route('/login', function(){
      Flight::render('pages/login', array('title' => "Login"));
    });

    Flight::start();

?>

L ;)

Iazzetta commented 7 years ago

@imbr not working..

layout.php

<head>
        <title>Site - <?php echo $title; ?></title>
</head>

index.php

Flight::set('flight.views.path', 'views/');
Flight::render('layout', array('title' => "Home"));

Flight::route('/login', function(){
    Flight::render('pages/login', array('title' => "Login"));
});

The title stay "Home"...

mikecao commented 7 years ago

@Iazzetta When you call render, it sends the output immediately. So if you render layout first and then pages/login it won't change anything. You need to render the layout last, after you render all your page content. See http://flightphp.com/learn/#views.

Iazzetta commented 7 years ago

Oh nice, so I need render layout.php in each route? For me works. Thanks!

Flight::route('/login', function(){
    Flight::render('pages/login', array('title' => "Login"));
    Flight::render('layout', array('title' => "Login"));
});

Flight::route('/home', function(){
    Flight::render('pages/home', array());
    Flight::render('layout', array('title' => "Home"));
});