简体   繁体   中英

Slim framework with raintpl

I have an existing project which uses RainTpl3 as its template engine. I am trying to port it to Slim framework 2.
Q1 : Is there any way to make slim use my $tpl object to render its views (via DI or any other similar approach).

I have managed to hack a way to configure create template object within an external function which then should return the content which then slim should echo. Earlier it could not configure the tpl properly so I then configured it in my init file. Now when I use $tpl->draw('index'); it is saying template index is not found. Q2: Why it can't find the template ?

: MY_APP_ROOT_/Public/index.php

require_once "../init.php";

# Fire up an app
$app = new Slim\Slim();

$app->get('/', function(){echo MyNS\Router\APIDefault::showHome();});

$app->get('/Hello', function() {
    echo "Get Route is working.";
});
# Run the Slim application
$app->run();

: MY_APP_ROOT_/Routes/APIDefault.php

<?php
namespace MyNS\Router; 


class APIDefault
{

    private function __construct( $argument)
    {
        throw new \Exception("Error Constructor not allowed", 1);

    }


    public static function showHome()
    {

        $tpl = new \Rain\Tpl();

         $tpl->assign('Name','Abhinav');
        $a = $tpl->draw('index',$return_string = true);
        return $a;
    }
}

index.tpl is stored in MY_APP_ROOT_/Templates/ .

  1. You could make your own View Script which wraps that Template Engine. Also Slim did this for Twig and Smarty here (only Slim 2).

  2. It looks like you never set a directory where to search for templates, am I wrong? But if you go with your own View Plugin you could take this as param in Constructor or something.

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