Skip to content

Instantly share code, notes, and snippets.

@arcostasi
Last active August 29, 2015 14:18
Show Gist options
  • Save arcostasi/308620886a2300166234 to your computer and use it in GitHub Desktop.
Save arcostasi/308620886a2300166234 to your computer and use it in GitHub Desktop.
USING TWIG WITH PHALCON
// Using Twig with Phalcon PHP
// Twig is a modern template engine for PHP
// Now we can Using Twig with Phalcon
// How to do:
// 1. Install Twing in library as git submodule.
// > git submodule add https://github.com/twigphp/Twig.git app/library/Twig
// 2. Install phalcon incubator in library as git submodule.
// > git submodule add https://github.com/phalcon/incubator.git app/library/incubator
// 3. Edit your app/config/loader.php file and add a twig,incubator to autoloader.
<?php
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir
)
);
$loader->registerNamespaces(array(
'Phalcon' => __DIR__ . '/../../app/library/incubator/Library/Phalcon/',
));
$loader->register();
require __DIR__ . '/../../app/library/Twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
// 4. Edit your app/config/services.php file and add a twig service to DI
/**
* Setting up the view component
*/
$di->set('twigService', function($view, $di) use ($config) {
$options = array(
'debug' => true,
'charset' => 'UTF-8',
'base_template_class' => 'Twig_Template',
'strict_variables' => false,
'autoescape' => false,
$config->application->cacheDir,
'auto_reload' => null,
'optimizations' => -1,
);
$twig = new \Phalcon\Mvc\View\Engine\Twig($view, $di, $options);
return $twig;
}, true);
$di->set('view', function () use ($config) {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array(
".twig" => 'twigService'
));
return $view;
}, true);
// 5. Add New Twig templete Files app/views/index.twig
<!DOCTYPE html>
<html>
<head>
<title>Phalcon PHP Framework</title>
</head>
<body>
<h1>Congratulations!</h1>
<p>Youre now flying with Phalcon!</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment