简体   繁体   中英

How to integrate twig template engine into PHP MVC architecture with no framework

I am working on a project which is based on MVC architecture (not using any framework) and working fine right now, but the issue is how to integrate twig php template engine. I am posting the layout structure of my project and the code which i am using to load the template and render the view. Please look into and suggest me the right solution to integrate the twig into php mvc architecture,

Project structure

  • Project1

    • app
      • controllers
        • welcome_controllers.php
      • models
      • views
        • welcome
          • index.html.twig
        • templates
          • layout.html.twig
    • config
    • libraries
      • Twig(twig files resides here)
      • router.php Controller base class

    class Controller {

     var $twig = null; public function loadTwig() { Twig_Autoloader::register(); $loader = new Twig_Loader_Filesystem( array (APP_PATH.'/views/') ); // set up environment $params = array( 'cache' => BASE_PATH.'/cache', 'auto_reload' => true, // disable cache 'autoescape' => true ); $this->twig = new Twig_Environment($loader, $params); } 

    }

WelcomeController class

class WelcomeController extends AppController{

    public function index(){
        $this->loadTwig();
        $this->twig->render('welcome/index.html.twig');
    }
}

index.html.twig file

{% extends '../templates/layout.html.twig' %}

{% block title %}
    Index Page Title
{% endblock %}

layout.html.twig file

<html>
    <head>
        <title>{% block title %}{% endblock %} - My Webpage</title>
    </head>
    <body>
       base template text
    </body>
</html>

Now, I have defined template layout.html.php in views/templates folder and i want to inherit all the view pages for the controllers from it. Please someone help me if implemented this thing before or have any idea about this.

Not sure If this is what you're asking for, your question is kind of confusing, but maybe telling my own experience will help.

I created my own MVC framework trying to copy some of the best functionalities of Symfony2. I did it by using some of the same vendors, including Twig.

I installed Twig through Composer which automatically generates the autoloader. The way I'm integrating Twig is by creating a loadTwig method inside the __construct of my main controller:

public function loadTwig() {
    $loader = new Twig_Loader_Filesystem(
        array (
            "../html/views",
            "../html/widgets"
        )
    );
    // set up environment
    $params = array(
        'cache' => "../cache", 
        'auto_reload' => true, // disable cache
        'autoescape' => true
    );
    $this->twig = new Twig_Environment($loader, $params);
    // ...
}

Then, every time I need to render a template located inside "views" or "widgets" folder I just call:

 $this->twig->display('myview.html.twig', array('foo' => 'bar'));

Hope this helps!

EDIT:

The twig loader filesystem loads every template at the same level inside the dirs you define (not sure if it loads the files recursively or just the ones on the root of the dir, you may try this). This means that you don't have to define a path when rendering, extending or including other twig templates. The templates just load and exist at the same level. Try the following:

Filesystem Loader:

$loader = new Twig_Loader_Filesystem(
    // array (APP_PATH.'/views/') you may try if this work, but:
    array (
        APP_PATH.'views/templates',
        APP_PATH.'views/welcome'
        APP_PATH.'views/foo'
        APP_PATH.'views/bar'
    )
);

Rendering (use display instead of render):

$this->twig->display('index.welcome.html.twig', array('foo' => 'bar')); // if you have more than one index.html.twig, rename your templates :)

Template:

{% extends 'layout.html.twig' %}

{% block title %}
    Index Page Title
{% endblock %}

If you just wanted to use Twig to Render data to your templates, take care of passing $twig var to the ___construct of each object class you instantiate and then validate that it's passed okay before render.

control

$objEmployee = new Employee($objConexion->conectar(), $twig);

class

function __construct($l,$q,$t){

    $this->link     = $l;
    $this->query    = $q;
    $this->twig     = $t;
}

validate twig on class

if (isset($this->twig)){

    all print view and render stuff;

}

Then you could easily use your templates on every PHP class instantiated with Twig as a parameter.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM