Last active
September 30, 2019 12:40
-
-
Save willmendesneto/4541588 to your computer and use it in GitHub Desktop.
Inserindo Twig Template Engine no Codeigniter via Composer Localilzação dos arquivos: Twig.php => application/libraries/Twig.php
Welcome.php => application/controller/Welcome.php autoload.php => application/config/autoload.php
template.html.twig => application/views/template.html.twig
index.html.twig => application/views/index.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
/* | |
| ------------------------------------------------------------------- | |
| AUTO-LOADER | |
| ------------------------------------------------------------------- | |
| This file specifies which systems should be loaded by default. | |
| | |
| In order to keep the framework as light-weight as possible only the | |
| absolute minimal resources are loaded by default. For example, | |
| the database is not connected to automatically since no assumption | |
| is made regarding whether you intend to use it. This file lets | |
| you globally define which systems you would like loaded with every | |
| request. | |
| | |
| ------------------------------------------------------------------- | |
| Instructions | |
| ------------------------------------------------------------------- | |
| | |
| These are the things you can load automatically: | |
| | |
| 1. Packages | |
| 2. Libraries | |
| 3. Helper files | |
| 4. Custom config files | |
| 5. Language files | |
| 6. Models | |
| | |
*/ | |
/* | |
| ------------------------------------------------------------------- | |
| Auto-load Packges | |
| ------------------------------------------------------------------- | |
| Prototype: | |
| | |
| $autoload['packages'] = array(APPPATH.'third_party', '/usr/local/shared'); | |
| | |
*/ | |
$autoload['packages'] = array(); | |
/* | |
| ------------------------------------------------------------------- | |
| Auto-load Libraries | |
| ------------------------------------------------------------------- | |
| These are the classes located in the system/libraries folder | |
| or in your application/libraries folder. | |
| | |
| Prototype: | |
| | |
|41$autoload['libraries'] = array('database', 'session', 'xmlrpc'); | |
*/ | |
$autoload['libraries'] = array('database'); | |
/* | |
| ------------------------------------------------------------------- | |
| Auto-load Helper Files | |
| ------------------------------------------------------------------- | |
| Prototype: | |
| | |
| $autoload['helper'] = array('url', 'file'); | |
*/ | |
$autoload['helper'] = array('url', 'file', 'form'); | |
/* | |
| ------------------------------------------------------------------- | |
| Auto-load Config files | |
| ------------------------------------------------------------------- | |
| Prototype: | |
| | |
| $autoload['config'] = array('config1', 'config2'); | |
| | |
| NOTE: This item is intended for use ONLY if you have created custom | |
| config files. Otherwise, leave it blank. | |
| | |
*/ | |
$autoload['config'] = array(); | |
/* | |
| ------------------------------------------------------------------- | |
| Auto-load Language files | |
| ------------------------------------------------------------------- | |
| Prototype: | |
| | |
| $autoload['language'] = array('lang1', 'lang2'); | |
| | |
| NOTE: Do not include the "_lang" part of your file. For example | |
| "codeigniter_lang.php" would be referenced as array('codeigniter'); | |
| | |
*/ | |
$autoload['language'] = array(); | |
/* | |
| ------------------------------------------------------------------- | |
| Auto-load Models | |
| ------------------------------------------------------------------- | |
| Prototype: | |
| | |
| $autoload['model'] = array('model1', 'model2'); | |
| | |
*/ | |
$autoload['model'] = array(); | |
/* End of file autoload.php */ | |
/* Location: ./application/config/autoload.php */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); | |
/** | |
* | |
* Arquivo inserido em application/config/twig.php | |
* | |
*/ | |
$config['template_dir'] = APPPATH.'views'; | |
$config['cache_dir'] = APPPATH.'cache/twig'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'template.html.twig' %} | |
{% block content %} | |
{{ title }} | |
<div id="infoMessage">{{ message }}</div> | |
<div class="row"> | |
<div class="span4 offset4"> | |
<h1 class="offset1">Login</h1> | |
<p>Please login with your email/username and password below.</p> | |
{{ form_open('auth/login', 'class="well center"')|raw }} | |
<div class="input-prepend"> | |
<label for="password">Username:</label> | |
<span class="add-on"><i class="icon-user"></i></span> | |
{{ form_input( identity, 'username', 'class="input-xlarge" placeholder="username" autofocus="true"' )|raw }} | |
</div> | |
<div class="input-prepend"> | |
<label for="password">Password:</label> | |
<span class="add-on"><i class="icon-lock"></i></span> | |
{{ form_password( password, 'password', 'class="input-xlarge" placeholder="password" autofocus="true"' )|raw }} | |
</div> | |
<div class="input-prepend"> | |
<label for="remember">Remember Me:</label> | |
{{ form_checkbox('remember', '1', FALSE, 'id="remember"')|raw }} | |
</div> | |
{{ form_submit('submit', 'Login', 'class="btn btn-primary"')|raw }} | |
{{ form_close()|raw }} | |
<p><a href="/testtwig/forgot_password">Forgot your password?</a></p> | |
</div> | |
</div> | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html class="no-js" lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<title>{% block title %}Template Twig{% endblock %}</title> | |
<meta name="description" content=""> | |
<meta name="author" content=""> | |
<meta name="viewport" content="width=device-width"> | |
</head> | |
<body> | |
<div class="container"> | |
{% block content %} | |
conteúdo a ser inserido | |
{% endblock %} | |
<hr> | |
<footer> | |
<p>© Company 2012</p> | |
</footer> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (!defined('BASEPATH')) {exit('No direct script access allowed');} | |
/** | |
* | |
* Arquivo inserido em application/libraries/Twig.php | |
* | |
*/ | |
class Twig { | |
/** | |
* Referência da instância da classe CodeIgniter | |
* | |
* @var object | |
*/ | |
protected $CI; | |
/** | |
* Referência da instância da classe TWIG | |
* | |
* @var object | |
*/ | |
protected $_twig; | |
/** | |
* Diretório de templates da aplicação | |
* | |
* @var string | |
*/ | |
protected $_template_dir; | |
/** | |
* Diretório do cache dos templates da aplicação | |
* | |
* @var string | |
*/ | |
protected $_cache_dir; | |
/*** | |
* Construtor da classe | |
* | |
* @param bool $debug verifica o valor do atributo DEBUG para a classe de template TWIG | |
* @return | |
*/ | |
function __construct($debug = false) | |
{ | |
$this->CI =& get_instance(); | |
$this->CI->config->load('twig'); | |
log_message('debug', "Twig Autoloader Loaded"); | |
\Twig_Autoloader::register(); | |
$this->_template_dir = $this->CI->config->item('template_dir'); | |
$this->_cache_dir = $this->CI->config->item('cache_dir'); | |
$loader = new \Twig_Loader_Filesystem($this->_template_dir); | |
$this->_twig = new \Twig_Environment($loader, array( | |
'cache' => $this->_cache_dir, | |
'debug' => $debug, | |
//'auto_reload' => TRUE | |
)); | |
foreach(get_defined_functions() as $functions) { | |
foreach($functions as $function) { | |
$this->_twig->addFunction($function, new Twig_Function_Function($function)); | |
} | |
} | |
} | |
/** | |
* Renderiza o template | |
* | |
* @param string $template nome do template | |
* @param array $data valores a serem passados ao template | |
* @return void | |
*/ | |
public function render($template, $data = array()) { | |
$template = $this->_twig->loadTemplate($template); | |
return $template->render($data); | |
} | |
/** | |
* Renderiza o template verificando o tempo gasto de execução para renderização | |
* | |
* @param string $template nome do template | |
* @param array $data valores a serem passados ao template | |
* @return void | |
*/ | |
public function display($template, $data = array()) { | |
$template = $this->_twig->loadTemplate($template); | |
/* elapsed_time and memory_usage */ | |
$data['elapsed_time'] = $this->CI->benchmark->elapsed_time('total_execution_time_start', 'total_execution_time_end'); | |
$memory = (!function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2) . 'MB'; | |
$data['memory_usage'] = $memory; | |
$template->display($data); | |
} | |
/** | |
* Adiciona as funções no Twig | |
* @param string $name nome da função | |
* @return void | |
*/ | |
public function add_function($name) | |
{ | |
$this->_twig->addFunction($name, new Twig_Function_Function($name)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
/** | |
* | |
* Arquivo inserido em application/controller/Welcome.php | |
* | |
*/ | |
class Welcome extends CI_Controller { | |
/** | |
* Construtor da classe | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->load->library('Twig'); | |
} | |
/** | |
* Envia para a pagina index do Controller | |
* @return void | |
*/ | |
public function index() | |
{ | |
try{ | |
$data['title'] = "Testing Twig!!"; | |
$this->twig->display('twig/index.html.twig', $data); | |
}catch(Exception $e){ | |
show_error($e->getMessage().' --- '.$e->getTraceAsString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For that the file "Twig.php" works correctly is necesary to modify the line number 50:
\Twig_Autoloader::register();
to:
require_once 'Twig/Autoloader.php';
\Twig_Autoloader::register();
I had that problem.
Thank you!