Giter Site home page Giter Site logo

robertfausk / twigstringbundle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from attilabukor/twigstringbundle

0.0 1.0 1.0 146 KB

This Symfony2 Bundle adds the possibillity to render strings instead of files with the Symfony native Twig templateing engine.

License: MIT License

PHP 100.00%

twigstringbundle's Introduction

TwigstringBundle information & howto

This Symfony2 Bundle adds the possibility to render strings instead of files with the Symfony2 native Twig templating engine. The Bundle adds an additional service named twigstring. It is used the same way as the original templating service. The only difference is that the first argument of the method render(<string>, [<array]) is a string to parse instead of a template file.

The bundles supports variable output, conditions, loops and rendering of an controller. It does NOT support including templates, yet. It would interpret the template name as twigstring.

One example usage scenario is to load templates from the database instead of using files.

Extension ideas:

  • option to include templates
  • add a black-/whitelist for allowed allowed twig tags

Installation

If you are an advanced Symfony developer, you know how things are done. The package's name is r1pp3rj4ck/TwigstringBundle and you need the dev-master version of it, and the bundle's FQN is LK\TwigstringBundle\LKTwigstringBundle(). You also need a top-level entry to your config: lk_twigstring: ~.

Of course, if you're not sure about yourself, here is the step-by-step installation procedure:

1. Register the bundle in composer

You need to add it to your composer.json requirements:

{
    "require": {
         "r1pp3rj4ck/TwigstringBundle": "dev-master"
    }
}

2. Downloading the bundle from packagist

Install it by running the following command:

$ ./composer.phar update r1pp3rj4ck/TwigstringBundle

3. Registering the bundle in the kernel

<?php
// app/AppKernel.php

public function registerBundles() {
    $bundles = array(
        // ...
        new LK\TwigstringBundle\LKTwigstringBundle(),
        // ...
    );
}

4. Registering the bundle in the configuration

# app/config/config.yml
lk_twigstring: ~

5. Add additional extensions you are in need of (optional)

# app/config/services.yml
services:
    twigstring.extension.foo:
        class: Foo\BarBundle\Twig\FooBarExtension
        tags:
            - { name: twigstring.extension }

For a list of available extensions see fabpot/Twig-extensions or create one on your own with Symfony Cookbook and twig doc.

Usage

// set example parameter
$vars = array('var'=>'x');

// get twigstring service
$tpl_engine = $this->get('twigstring');

// render example string
$vars['test'] = 'u ' . $tpl_engine->render('v {{ var }} {% if var is defined %} y {% endif %} z{% for i in 1..5 %} {{ i }}{% endfor %}', $vars);

or use the short way:

// set example parameter
$vars = array('var'=>'x');

// render example string
$vars['test'] = 'u ' . $this->get('twigstring')->render('v {{ var }} {% if var is defined %} y {% endif %} z{% for i in 1..5 %} {{ i }}{% endfor %}', $vars);

Example output:

u v x y z

License

The bundle is licensed under MIT license. For full license see LICENSE file

Authors

LarsK (Lars Krüger), cordoval (Luis Cordova), r1pp3rj4ck (Attila Bukor)

twigstringbundle's People

Contributors

attilabukor avatar cordoval avatar kcivey avatar

Watchers

 avatar

Forkers

offerista

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.