Code Monkey home page Code Monkey logo

kohana-twig's Introduction

Twig Module

From twig-project.org

Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum.

Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a templating language for applications where users may modify the template design.

Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL.

Credit goes to Jonathan Geiger and John Heathco for creating the original modules. This fork contains the following improvements.

  • Sandbox configuration
  • Syntax ใ€ƒ
  • Kohana template loader. Now templates aren't restricted to one directory
  • Updated to follow the Kohana convention

Installation

  1. git submodule add https://[email protected]/ThePixelDeveloper/kohana-twig.git modules/twig
  2. cd modules/twig && git submodule update --init
  3. Enable twig in your bootstrap.php file
  4. Extend Controller

Usage

Pretty similar to using the Controller_Template class.

class Controller_Example extends Controller
{
  // Template names are generated automatically if not specified. So this
  // action would map to: example/index.html
  public function action_index()
  {
    $this->variable = "Hello World";
  }
}

Not Happy?

Either file a bug report or try an alternative project:

kohana-twig's People

Contributors

bistory avatar coffeeich avatar icyleaf avatar jheathco avatar jonathangeiger avatar jturmel avatar mastercj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

kohana-twig's Issues

Extensions could have a docs

Hey,

I'm trying to figure out the Kohana + Twig extensions you've created, mostly reading code. We could make a README file, I'd be happy to help

install docs would be nice

mostly it was easy, but I was briefly hung up because I didn't know to put the latest Twig in vendors/

Stacktrace is always shown.

In the __toSting method of Twig.php

try
{
    return $this->render();
}
catch (Exception $e)
{
    // Display the exception message
    Kohana::exception_handler($e);

    return '';
}

A stacktrace is always shown, which is probably not appropriate for production environments. I'm working on a patch.

Twig_Loader_String

We have error when use another Loader for twig, when try parse string
Text text
In result we have:
Text <img src="http://example.com/image
Cose Kohana_Twig class find extension in the first param in the factory. FIx this:
Remove code:
if ($extension = pathinfo($file, PATHINFO_EXTENSION))
{
$this->set_extension($extension);
}
From function Kohana_Twig::set_filename . I don't know why we must use this code(search extension in input data), cose this is loader functional, and extension declared in config file.

Use this to make twig faster with kohana

Twig inside kohana is much slower due to the way kohana finds files (when it autoloads classes or whatever).

I tested Twig standalone in the document root and it is very fast. Inside kohana it takes as much time to render a small template as the whole framework itself takes to render the same thing with views with php in them + alittle more (and this is after the template has been cached !). I first thought it wasnt caching properly, but no, that wasnt it, its the file loading.

I was ready to give up twig in favor of mustache which is lightning fast (again with some tweaks, from the original kostache)

But after some more testing I found the problem (and the solutions)

Itried 2 variants:

  1. because twig naming convention and folder structure is same as kohana (class Twig_Lexer is in
    modules/twig/vendor/Twig/lib/Twig/Lexer.php
    (Twig/Lexer.php))
    you can move the whole
    lib/Twig folder
    inside classes (that file would now be at:
    modues/twig/classes/Twig/Lexer.php

Now twig autoloader is no longer needed (in init.php).

This halved the time it took for loading files. (I tested it, for ex, the time it took from passing execution from ... = new Twig_lexer; to the constructor inside Lexer.php)

  1. I found an even faster way:

I Changed the profiler call inside kohana::find_file to print also the file name it loads, then printed the profiling view at the bottom of index.php

<div id="kohana-profiler">
<?php echo View::factory('profiler/stats') ?>
</div>

Now I only did this for cached templates, when twig rebuilds them there are probably more.

In any case, here is what I added to /modules/twig/classes/twig.php:

If you DID move the Twig folder from inside vendor into classes/twig (notice small t in twig) use this:

<?php defined('SYSPATH') or die('No direct script access.');

    $folder = realpath(dirname(__FILE__).'/twig');

If you did NOT move the Twig folder from inside vendor into classes use this:

<?php defined('SYSPATH') or die('No direct script access.');

    $folder = realpath(dirname(__FILE__).'/../vendor/Twig/lib/Twig');

Then underneath:

require_once "$folder/LexerInterface.php" ;
require_once "$folder/ParserInterface.php" ;
require_once "$folder/CompilerInterface.php" ;
require_once "$folder/LoaderInterface.php" ;
require_once "$folder/ExtensionInterface.php" ;
require_once "$folder/TemplateInterface.php" ;
require_once "$folder/NodeInterface.php" ;

require_once "$folder/Extension.php" ;
require_once "$folder/Extension/Core.php" ;

require_once "$folder/Template.php" ;

require_once "$folder/Node.php" ;
require_once "$folder/Node/Expression.php" ;
require_once "$folder/Node/Expression/GetAttr.php" ;

require_once "$folder/Lexer.php" ;
require_once "$folder/Parser.php" ;
require_once "$folder/Compiler.php" ;

require_once "$folder/Environment.php" ;

class Twig extends Kohana_Twig {}

Helper Parser is broken

ErrorException [ Recoverable Error ]: Argument 1 passed to Twig_NodeTraverser::traverse() must implement interface Twig_NodeInterface, instance of Twig_Token given, called in /var/www/nolimits/public/modules/twig/vendor/Twig/lib/Twig/NodeTraverser.php on line 73 and defined

Need to find the solution, just leaving a note for myself and anyone else. Will update at a later date.

Use Kohana::find_file ?

Hello !
Thanks a lot for your library !
I've a small question.
It would be great to use Kohana::find_file to find templates instead of looking directly into /application/views (to use the hierarchical structure of Kohana).

I wasn't able to modify it by myself, maybe you've a better view to the code to make it :-)

Thanks for your help !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.