Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9103517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:39:00+00:00 2026-06-17T01:39:00+00:00

I am writing my own MVC framework and has come to the view renderer.

  • 0

I am writing my own MVC framework and has come to the view renderer. I am setting vars in my controller to a View object and then access vars by echo $this->myvar in the .phtml script.

In my default.phtml I call the method $this->content() to output the viewscript.

This is the way I do it now. Is this a proper way to do that?

class View extends Object {

    protected $_front;

    public function __construct(Front $front) {
        $this->_front = $front;
    }

    public function render() {                
        ob_start();
        require APPLICATION_PATH . '/layouts/default.phtml' ;            
        ob_end_flush();
    }

    public function content() {
        require APPLICATION_PATH . '/views/' . $this->_front->getControllerName() . '/' . $this->_front->getActionName() . '.phtml' ;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:39:02+00:00Added an answer on June 17, 2026 at 1:39 am

    Here’s an example of how i did it :

    <?php
    
    
    class View
    {
    private $data = array();
    
    private $render = FALSE;
    
    public function __construct($template)
    {
        try {
            $file = ROOT . '/templates/' . strtolower($template) . '.php';
    
            if (file_exists($file)) {
                $this->render = $file;
            } else {
                throw new customException('Template ' . $template . ' not found!');
            }
        }
        catch (customException $e) {
            echo $e->errorMessage();
        }
    }
    
    public function assign($variable, $value)
    {
        $this->data[$variable] = $value;
    }
    
    public function __destruct()
    {
        extract($this->data);
        include($this->render);
    
    }
    }
    ?>
    

    I use the assign function from out my controller to assign variables, and in the destructor i extract that array to make them local variables in the view.

    Feel free to use this if you want, i hope it gives you an idea on how you can do it

    Here’s a full example :

    class Something extends Controller 
    {
        public function index ()
        {
        $view = new view('templatefile');
        $view->assign('variablename', 'variable content');
        }
    }
    

    And in your view file :

    <?php echo $variablename; ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing my own MVC framework in PHP, just for learning purposes. It wasn't
I am writing my own HtmlHelper extenstion for ASP.NET MVC: public static string CreateDialogLink
I'm writing my first real MVC-application with PHP. I'm not using a framework because
Basic problem I've come across a bit of a problem while writing my own
As almost every programmer, I'm writing my own PHP framework for educational purposes. And
I'm writing my own little MVC and I got a question... I'm passing $_SERVER['REQUEST_URI']
I am writing my own Joomla component (MVC), its based heavily on the newsflash
At the moment I am writing my own ValidationAttribute for my mvc application. I
I am writing my own Firebird database browser using the ibpp library. Is there
I am writing my own programming language just for the fun of it. Currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.