Skip to main content
Minor grammar fixes and clarification
Source Link

My guts tells me that this isn't the proper way to handle views / templates, so that's why I'm asking you what could be done better.

Some background: This application I'm building now is for use in ana school project with people that don't have not that much knowledge of PHP. Therefore I'm not using ana framework like Laravel, or implementing ana whole template engine, but I'm trying it to keep as simple as possible while still using classes and code separation.

The homeController:

Class homeController extends BaseController
{

    public function index()
    {
        $this->view->make("common/home");
    }
}

The View class:

Class View{

    public static $file;

    public function make($file){
        View::$file = $file;
        require_once DIR_VIEW . "default/template.php";
    }
}

The template.php file:

<!DOCTYPE html>
<html>
<head>
    <title>Just an title</title>
</head>
<body>
    <?php require_once 'header.php'; ?>

    <div id="mainContent">
        <?php require_once DIR_VIEW . View::$file . ".php"; ?>
    </div>

    <?php require_once 'footer.php'; ?>
</body>
</html>

BaseController:

class BaseController
{

    public $load;
    public $url;
    public $view;

    public function __construct()
    {
        $this->load = new Loader();
        $this->url = new Url();
        $this->view = new View();
    }
}

The files are required by an loader class with this piece of code: (don't know if it ads some relevance)

foreach (glob(DIR_LIB . "*.php") as $filename) {
    require_once $filename;
}

The problem is probably that i'm using an static variable $file inside the View class. I don't think that that is right, but don't know any better way (besides using global(), but I know that that isn't the solution.

What do you think? Is this good enough or not even close?

Note: there will always be 1 file included, so that's why I thought that a simple require would succeed.

My guts tells me that this isn't the proper way to handle views / templates, so that's why I'm asking you what could be done better.

Some background: This application I'm building now is for use in an school project with people that have not that much knowledge of PHP. Therefore I'm not using an framework like Laravel, or implementing an whole template engine, but I'm trying it to keep as simple as possible while still using classes and code separation.

The homeController:

Class homeController extends BaseController
{

    public function index()
    {
        $this->view->make("common/home");
    }
}

The View class:

Class View{

    public static $file;

    public function make($file){
        View::$file = $file;
        require_once DIR_VIEW . "default/template.php";
    }
}

The template.php file:

<!DOCTYPE html>
<html>
<head>
    <title>Just an title</title>
</head>
<body>
    <?php require_once 'header.php'; ?>

    <div id="mainContent">
        <?php require_once DIR_VIEW . View::$file . ".php"; ?>
    </div>

    <?php require_once 'footer.php'; ?>
</body>
</html>

BaseController:

class BaseController
{

    public $load;
    public $url;
    public $view;

    public function __construct()
    {
        $this->load = new Loader();
        $this->url = new Url();
        $this->view = new View();
    }
}

The files are required by an loader class with this piece of code: (don't know if it ads some relevance)

foreach (glob(DIR_LIB . "*.php") as $filename) {
    require_once $filename;
}

The problem is probably that i'm using an static variable $file inside the View class. I don't think that that is right, but don't know any better way (besides using global(), but I know that that isn't the solution.

What do you think? Is this good enough or not even close?

Note: there will always be 1 file included, so that's why I thought that a simple require would succeed.

My guts tells me that this isn't the proper way to handle views / templates, so that's why I'm asking you what could be done better.

Some background: This application I'm building now is for use in a school project with people that don't have much knowledge of PHP. Therefore I'm not using a framework like Laravel, or implementing a whole template engine, but I'm trying it to keep as simple as possible while still using classes and code separation.

The homeController:

Class homeController extends BaseController
{

    public function index()
    {
        $this->view->make("common/home");
    }
}

The View class:

Class View{

    public static $file;

    public function make($file){
        View::$file = $file;
        require_once DIR_VIEW . "default/template.php";
    }
}

The template.php file:

<!DOCTYPE html>
<html>
<head>
    <title>Just an title</title>
</head>
<body>
    <?php require_once 'header.php'; ?>

    <div id="mainContent">
        <?php require_once DIR_VIEW . View::$file . ".php"; ?>
    </div>

    <?php require_once 'footer.php'; ?>
</body>
</html>

BaseController:

class BaseController
{

    public $load;
    public $url;
    public $view;

    public function __construct()
    {
        $this->load = new Loader();
        $this->url = new Url();
        $this->view = new View();
    }
}

The files are required by an loader class with this piece of code: (don't know if it ads some relevance)

foreach (glob(DIR_LIB . "*.php") as $filename) {
    require_once $filename;
}

The problem is probably that i'm using an static variable $file inside the View class. I don't think that that is right, but don't know any better way (besides using global(), but I know that that isn't the solution.

What do you think? Is this good enough or not even close?

Note: there will always be 1 file included, so that's why I thought that a simple require would succeed.

deleted 104 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Is this the proper way to handle Handling views and templates

My guts tells me that this isn't the proper way to handle views / templates, so that's why I'm asking you guys what could be done better.

Some background: This application I'm building now is for use in an school project with people that have not that much knowledge of PHP. Therefore I'm not using an framework like laravelLaravel, or implementing an whole template engine, but i'mI'm trying it to keep as simple as possible while still using classes and code separation.

Time to dive into some code:

The homeControllerhomeController:

And theThe template.php file:

The problem is probably that i'm using an static variable $file inside the View class. I don't think that that is right, but don't know any better way (besides using global(), but i know that that isn't the solution.

So what do you guys think? Is this good enough or not even close?

-note, there will always be 1 file included, so no that's why i thought that an simple require would succeed

Edit1

As @Ismael Miguel questions, here's my BaseController:

The files are required by an loader class with this piece of code: ( don'tdon't know if it ads some relevanve relevance)

The problem is probably that i'm using an static variable $file inside the View class. I don't think that that is right, but don't know any better way (besides using global(), but I know that that isn't the solution.

What do you think? Is this good enough or not even close?

Note: there will always be 1 file included, so that's why I thought that a simple require would succeed.

Is this the proper way to handle views

My guts tells me that this isn't the proper way to handle views / templates, so that's why I'm asking you guys what could be done better.

Some background: This application I'm building now is for use in an school project with people that have not that much knowledge of PHP. Therefore I'm not using an framework like laravel, or implementing an whole template engine, but i'm trying it to keep as simple as possible while still using classes and code separation.

Time to dive into some code:

The homeController:

And the template.php file:

The problem is probably that i'm using an static variable $file inside the View class. I don't think that that is right, but don't know any better way (besides using global(), but i know that that isn't the solution.

So what do you guys think? Is this good enough or not even close?

-note, there will always be 1 file included, so no that's why i thought that an simple require would succeed

Edit1

As @Ismael Miguel questions, here's my BaseController

The files are required by an loader class with this piece of code: ( don't know if it ads some relevanve )

Handling views and templates

My guts tells me that this isn't the proper way to handle views / templates, so that's why I'm asking you what could be done better.

Some background: This application I'm building now is for use in an school project with people that have not that much knowledge of PHP. Therefore I'm not using an framework like Laravel, or implementing an whole template engine, but I'm trying it to keep as simple as possible while still using classes and code separation.

The homeController:

The template.php file:

BaseController:

The files are required by an loader class with this piece of code: (don't know if it ads some relevance)

The problem is probably that i'm using an static variable $file inside the View class. I don't think that that is right, but don't know any better way (besides using global(), but I know that that isn't the solution.

What do you think? Is this good enough or not even close?

Note: there will always be 1 file included, so that's why I thought that a simple require would succeed.

added 578 characters in body
Source Link
Mathlight
  • 261
  • 1
  • 5

My guts tells me that this isn't the proper way to handle views / templates, so that's why I'm asking you guys what could be done better.

Some background: This application I'm building now is for use in an school project with people that have not that much knowledge of PHP. Therefore I'm not using an framework like laravel, or implementing an whole template engine, but i'm trying it to keep as simple as possible while still using classes and code separation.

Time to dive into some code:

The homeController:

Class homeController extends BaseController
{

    public function index()
    {
        $this->view->make("common/home");
    }
}

The View class:

Class View{

    public static $file;

    public function make($file){
        View::$file = $file;
        require_once DIR_VIEW . "default/template.php";
    }
}

And the template.php file:

<!DOCTYPE html>
<html>
<head>
    <title>Just an title</title>
</head>
<body>
    <?php require_once 'header.php'; ?>

    <div id="mainContent">
        <?php require_once DIR_VIEW . View::$file . ".php"; ?>
    </div>

    <?php require_once 'footer.php'; ?>
</body>
</html>

The problem is probably that i'm using an static variable $file inside the View class. I don't think that that is right, but don't know any better way (besides using global(), but i know that that isn't the solution.

So what do you guys think? Is this good enough or not even close?

-note, there will always be 1 file included, so no that's why i thought that an simple require would succeed

Edit1

As @Ismael Miguel questions, here's my BaseController

class BaseController
{

    public $load;
    public $url;
    public $view;

    public function __construct()
    {
        $this->load = new Loader();
        $this->url = new Url();
        $this->view = new View();
    }
}

The files are required by an loader class with this piece of code: ( don't know if it ads some relevanve )

foreach (glob(DIR_LIB . "*.php") as $filename) {
    require_once $filename;
}

My guts tells me that this isn't the proper way to handle views / templates, so that's why I'm asking you guys what could be done better.

Some background: This application I'm building now is for use in an school project with people that have not that much knowledge of PHP. Therefore I'm not using an framework like laravel, or implementing an whole template engine, but i'm trying it to keep as simple as possible while still using classes and code separation.

Time to dive into some code:

The homeController:

Class homeController extends BaseController
{

    public function index()
    {
        $this->view->make("common/home");
    }
}

The View class:

Class View{

    public static $file;

    public function make($file){
        View::$file = $file;
        require_once DIR_VIEW . "default/template.php";
    }
}

And the template.php file:

<!DOCTYPE html>
<html>
<head>
    <title>Just an title</title>
</head>
<body>
    <?php require_once 'header.php'; ?>

    <div id="mainContent">
        <?php require_once DIR_VIEW . View::$file . ".php"; ?>
    </div>

    <?php require_once 'footer.php'; ?>
</body>
</html>

The problem is probably that i'm using an static variable $file inside the View class. I don't think that that is right, but don't know any better way (besides using global(), but i know that that isn't the solution.

So what do you guys think? Is this good enough or not even close?

-note, there will always be 1 file included, so no that's why i thought that an simple require would succeed

My guts tells me that this isn't the proper way to handle views / templates, so that's why I'm asking you guys what could be done better.

Some background: This application I'm building now is for use in an school project with people that have not that much knowledge of PHP. Therefore I'm not using an framework like laravel, or implementing an whole template engine, but i'm trying it to keep as simple as possible while still using classes and code separation.

Time to dive into some code:

The homeController:

Class homeController extends BaseController
{

    public function index()
    {
        $this->view->make("common/home");
    }
}

The View class:

Class View{

    public static $file;

    public function make($file){
        View::$file = $file;
        require_once DIR_VIEW . "default/template.php";
    }
}

And the template.php file:

<!DOCTYPE html>
<html>
<head>
    <title>Just an title</title>
</head>
<body>
    <?php require_once 'header.php'; ?>

    <div id="mainContent">
        <?php require_once DIR_VIEW . View::$file . ".php"; ?>
    </div>

    <?php require_once 'footer.php'; ?>
</body>
</html>

The problem is probably that i'm using an static variable $file inside the View class. I don't think that that is right, but don't know any better way (besides using global(), but i know that that isn't the solution.

So what do you guys think? Is this good enough or not even close?

-note, there will always be 1 file included, so no that's why i thought that an simple require would succeed

Edit1

As @Ismael Miguel questions, here's my BaseController

class BaseController
{

    public $load;
    public $url;
    public $view;

    public function __construct()
    {
        $this->load = new Loader();
        $this->url = new Url();
        $this->view = new View();
    }
}

The files are required by an loader class with this piece of code: ( don't know if it ads some relevanve )

foreach (glob(DIR_LIB . "*.php") as $filename) {
    require_once $filename;
}
Source Link
Mathlight
  • 261
  • 1
  • 5
Loading