在 TWIG 中使用 PHP 和 Mysql 代码
Working with PHP and Mysql code in TWIG
我正在使用 php 的 TWIG 框架,想知道我如何能够像往常一样将这些 php 文件包含在我的 php 代码中.
<?php
session_start();
include("includes/db.php");
include("functions/searchfunctions.php");
include("functions/userSearchSession.php");
?>
db文件通过mysqli与数据库建立连接
在您的评论中,您提到您正在使用 Slim framework, which has an extension to support Twig templates。
然而,使用扩展需要一些额外的设置,您必须安装扩展,称为 Slim Views、以及 Twig 核心 来自作曲家。 Slim Views 没有将 Twig 列为依赖项。
要使其正常工作:
使用 composer 来添加 Slim Views 和 Twig
$ php composer require slim/views
$ php composer require twig/twig:~1.0
配置您的 Slim Framework $app
以使用新的诱人引擎。
$view = $app->view();
$view->parserOptions = array(
'debug' => true,
'cache' => dirname(__FILE__) . '/cache'
);
$view->parserExtensions = array(
new \Slim\Views\TwigExtension(),
);
至此,Slim Framework 在渲染页面时使用了 Twig。您现在可以执行所有包含并将变量传递给 Twig:
<?php
// ./Slim_app.php
require 'vendor/autoload.php';
/*
* foo.php contains the following:
* <?php
* $foo = bar;
*
*/
require 'foo.php';
$app = new \Slim\Slim(array(
'view' => new \Slim\Views\Twig()
));
$view = $app->view();
$view->parserOptions = array(
'debug' => true,
'cache' => dirname(__FILE__) . '/cache'
);
$view->parserExtensions = array(
new \Slim\Views\TwigExtension(),
);
$app->get('/hello', function () use ($app, $foo) {
//twig_template.html.twig exists in the templates directory.
//(./templates/twig_template.html.twig)
$app->render('twig_template.html.twig', array('foo' => $foo));
});
$app->run();
?>
{# ./templates/twig_template.html.twig #}
{{ foo }}
导航到 Slim_app.php/hello
现在显示以下内容:
我正在使用 php 的 TWIG 框架,想知道我如何能够像往常一样将这些 php 文件包含在我的 php 代码中.
<?php
session_start();
include("includes/db.php");
include("functions/searchfunctions.php");
include("functions/userSearchSession.php");
?>
db文件通过mysqli与数据库建立连接
在您的评论中,您提到您正在使用 Slim framework, which has an extension to support Twig templates。
然而,使用扩展需要一些额外的设置,您必须安装扩展,称为 Slim Views、以及 Twig 核心 来自作曲家。 Slim Views 没有将 Twig 列为依赖项。
要使其正常工作:
使用 composer 来添加 Slim Views 和 Twig
$ php composer require slim/views $ php composer require twig/twig:~1.0
配置您的 Slim Framework
$app
以使用新的诱人引擎。$view = $app->view(); $view->parserOptions = array( 'debug' => true, 'cache' => dirname(__FILE__) . '/cache' ); $view->parserExtensions = array( new \Slim\Views\TwigExtension(), );
至此,Slim Framework 在渲染页面时使用了 Twig。您现在可以执行所有包含并将变量传递给 Twig:
<?php
// ./Slim_app.php
require 'vendor/autoload.php';
/*
* foo.php contains the following:
* <?php
* $foo = bar;
*
*/
require 'foo.php';
$app = new \Slim\Slim(array(
'view' => new \Slim\Views\Twig()
));
$view = $app->view();
$view->parserOptions = array(
'debug' => true,
'cache' => dirname(__FILE__) . '/cache'
);
$view->parserExtensions = array(
new \Slim\Views\TwigExtension(),
);
$app->get('/hello', function () use ($app, $foo) {
//twig_template.html.twig exists in the templates directory.
//(./templates/twig_template.html.twig)
$app->render('twig_template.html.twig', array('foo' => $foo));
});
$app->run();
?>
{# ./templates/twig_template.html.twig #}
{{ foo }}
导航到 Slim_app.php/hello
现在显示以下内容: