Reputation: 31
I would like to install twig for a project, but have no command-line acces to the server. I can only upload files via ftp. This would mean I have to setup the twig lib manually i.e. create the Autoload.php file myself. I have searched thoroughly but information on this subject is scarce. I have tried the following autoload "borrowed" from a different project but this does not produce a working setup.
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Autoloads Twig classes.
*
* @author Fabien Potencier <[email protected]>
*/
class Twig_Autoloader
{
/**
* Registers Twig_Autoloader as an SPL autoloader.
*
* @param bool $prepend Whether to prepend the autoloader or not.
*/
public static function register($prepend = false)
{
if (version_compare(phpversion(), '5.3.0', '>=')) {
spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
} else {
spl_autoload_register(array(__CLASS__, 'autoload'));
}
}
/**
* Handles autoloading of classes.
*
* @param string $class A class name.
*/
public static function autoload($class)
{
if (0 !== strpos($class, 'Twig')) {
return;
}
if (is_file($file = dirname(__FILE__).'/../'.str_replace(array('_', "\0"), array('/', ''), $class).'.php')) {
require $file;
}
}
}
Any help would be appreciated.
Upvotes: 3
Views: 9635
Reputation: 71
Twig-2.x/src
folder to Twig-2.x/Twig
Example directory structure:
Appdir/
Appdir/Twig-2.x/
Appdir/Twig-2.x/Twig/ <- this is the original src dir renamed to Twig
Appdir/templates/
Appdir/templates/index.html
Appdir/cache/
Appdir/index.php
Code "index.php":
<?php
#ini_set('display_errors',1); # uncomment if you need debugging
spl_autoload_register(function ($classname) {
$dirs = array (
'./Twig-2.x/' #./path/to/dir_where_src_renamed_to_Twig_is_in
);
foreach ($dirs as $dir) {
$filename = $dir . str_replace('\\', '/', $classname) .'.php';
if (file_exists($filename)) {
require_once $filename;
break;
}
}
});
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [
'cache' => 'cache',
]);
echo $twig->render('index.html', ['name' => 'Carlos']);
?>
Code "index.html":
<h1>Hello {{ name }}!</h1>
Upvotes: 7
Reputation: 31
After studying the link provided by András I realized that the key is not the loading mechanism, but the fact that "autoload.php" appears to be missing. I than found out, that the version od Twig I was usings, was in fact a 2.X version, that requires at least PHP 7. Since I am working with 5.4, this would not work, obviously. Fortunately the latest 1.X version of the lib does provide an autoload.php, so from there on everything worked fine.
Upvotes: 0
Reputation: 464
If you really need load Twig without Composer you can use this: https://gist.github.com/sarciszewski/b6cd3776fbd20acaf26b
I recommend to setup Composer in your local development environment. (You can download from https://getcomposer.org/) Install twig with Composer.
composer require twig/twig:~2.0
Then include autoloader to your project:
require_once 'vendor/autoload.php';
You can work locally and when the project is ready deploy to your server with the vendor directory, which contains the installed packages. You doesn't need composer on your server.
Upvotes: -1