简体   繁体   English

Raintpl的超薄框架

[英]Slim framework with raintpl

I have an existing project which uses RainTpl3 as its template engine. 我有一个使用RainTpl3作为模板引擎的现有项目。 I am trying to port it to Slim framework 2. 我正在尝试将其移植到Slim框架2。
Q1 : Is there any way to make slim use my $tpl object to render its views (via DI or any other similar approach). 问题1:有什么方法可以使我的$tpl对象苗条使用(通过DI或任何其他类似方法)呈现其视图。

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. 之前它无法正确配置tpl,所以我在初始化文件中对其进行了配置。 Now when I use $tpl->draw('index'); 现在当我使用$tpl->draw('index'); it is saying template index is not found. 这表示找不到模板索引。 Q2: Why it can't find the template ? Q2:为什么找不到模板?

: MY_APP_ROOT_/Public/index.php 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 :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/ . index.tpl存储在MY_APP_ROOT_ / Templates /中。

  1. You could make your own View Script which wraps that Template Engine. 您可以制作自己的包含该模板引擎的View脚本。 Also Slim did this for Twig and Smarty here (only Slim 2). Slim也在这里为Twig和Smarty做到了这一点 (仅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. 但是,如果您使用自己的View插件,则可以将其作为Constructor或其他工具中的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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