【问题标题】:Slim - Use variables of a route inside an included fileSlim - 在包含的文件中使用路径的变量
【发布时间】:2018-04-18 08:16:37
【问题描述】:

我正在使用Slim 3 构建一个应用程序。

在某条路线中,我通过include_once 包含模板的标题(slim/php-view 2.2),并且有一些我需要发送到模板的变量在标题中使用。

有没有办法做到这一点?

我的路线:

$app->get('/', function ( $request,  $response) {
    // some code here...
    $somedata = ' this is just a test';

    return $this->renderer->render($response, "/home.phtml",[
        'somedata' => $somedata,
    ]);
})

目标模板(home.phtml):

<?php include_once('myheader.php'); ?>

    <h1>This is my template</h1>
    <p> I need to use this variable <?=$somedata?> 
    in the included myheader.php file</p>

【问题讨论】:

    标签: php templates routes slim slim-3


    【解决方案1】:

    我的错。看了the docs发现可以使用模板变量实现我想要的,像这样:

    我的路线:

    $templateVariables = [
        "title" => "Title"
    ];
    $phpView = new PhpRenderer("./path/to/templates", $templateVariables);
    

    我的包含文件(myheader.php):

    <?php echo $title ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      相关资源
      最近更新 更多