Giter Club home page Giter Club logo

templater's Introduction

Templater

I developed this library for a project where I had to replace the legacy PHP templates to Twig templates. The job was enormous: we had more than 16,000 PHP tags (sometimes with dozens of lines) in 400+ files.

So thanks to Templater I managed to successfully convert (almost barely) with automatic techniques, the 92% of the different tags, and the 85% of all the tags (the majority of the difference comes from the fact that block endings, like "}" cannot be converted without extra work).

So only ~2200 tags remained for manual conversion, but it came evident soon, that it's still too slow for me. Then I introduced "partial conversion", which means that a log file stores those tags which couldn't be fully converted, but some parts of them. It's very safe and useful for long, multiline tags: your templates are not threatened with partially working tags but you can copy the most parts of them to your template file.

Suggested working method:

Copy your original templates from your project's template directory to a safe location (e.g. name the folder as "my-converted-templates"). Then make another backup of it (e.g.: name it as "my-original-templates"). Now you can use Templater to modify your files in the "my-converted-templates" directory. Finally start "merging" your new templates with the original ones in your project. Remember to check for partial conversions! If you have to re-convert the tags, you can replace "my-converted-templates" with "my-original-templates".

The advantage of this method is that you can make as many retries as you want, and modify conversion options without affecting your manually converted tags. Of course, if you have an error in your manual work, you have to find a solution to efficiently correct it.

Usage:

<?php
require "vendor/autoload.php";

use app\twig\TwigTemplate;

$converter= new TwigTemplate("extension_of_templates", "/path/of/templates", "name_of_the_project");

$converter->convertFromPHP();
$converter->setIsConvertStaticMethods(false);
$converter->setIsPartialConversionEnabled(true);
$converter->printConversionInfo();
$converter->saveConversion();
$converter->renameFileExtensions("twig");

In the temp directory, you will get three files:

  • {name_of_the_project}-unsuccessful: These tags are not yet converted
  • {name_of_the_project}-partial: These tags could have only been partially converted (only if partial conversion is enabled)
  • {name_of_the_project}-successful: These tags were converted fully and successfully

Tag injection:

You can inject your own searched and replacement tags to Templater. You have to add a {name_of_the_project}-injection.php file to the "temp" directory which returns an array. An example is:

return array(
    '/<\?php echo \$header; \?>/' => '{% include "header.twig" %}',
);

It will convert those tags which echo a variable named "header" into an included Twig template. Important to know, that Templater starts with the conversion of possible injected tags then continues with the automatic replacements.

Static method conversion:

You can also set static method conversion with TwigTemplate::setIsConvertStaticMethods(). Currently, Templater can only convert PHP static method invocations with the following signature:

<?php echo ClassName::methodName(); ?>

into Twig tags with the following signature:

{{ staticCall("ClassName", "methodName") }}

You have to ensure that a "staticCall" function is supported by your Twig environment (e.g.: by extending it).

templater's People

Contributors

kocsismate avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.