Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

542
Visualizações
How to install twig without using composer

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 <fabien@symfony.com>
 */
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.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

  1. Download Twig. (From for ex: https://github.com/twigphp/Twig/archive/2.x.zip )
  2. Extract it to relative path you want (ex: ./Twig-2.x).
  3. Rename Twig-2.x/src folder to Twig-2.x/Twig
  4. Include this spl_autoload_register() function into your bootstrap script.
  5. Remember to use FQCN to references Twig's classes

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>
about 4 years ago · Santiago Trujillo Relatório

0

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.

about 4 years ago · Santiago Trujillo Relatório

0

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.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda