Skip to content

Commit cac9b27

Browse files
committed
init commit
1 parent a5d76e2 commit cac9b27

5 files changed

Lines changed: 168 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/composer.lock
2+
/composer.phar
3+
/vendor/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Shapecode, Nikita Loges
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,52 @@
1-
twig-string-loader
1+
# Shapecode - Twig String Loader
2+
3+
[![paypal](https://img.shields.io/badge/Donate-Paypal-blue.svg)](http://paypal.me/nloges)
4+
5+
[![PHP Version](https://img.shields.io/packagist/php-v/shapecode/twig-string-loader.svg)](https://packagist.org/packages/shapecode/twig-string-loader)
6+
[![Latest Stable Version](https://img.shields.io/packagist/v/shapecode/twig-string-loader.svg?label=stable)](https://packagist.org/packages/shapecode/twig-string-loader)
7+
[![Latest Unstable Version](https://img.shields.io/packagist/vpre/shapecode/twig-string-loader.svg?label=unstable)](https://packagist.org/packages/shapecode/twig-string-loader)
8+
[![Total Downloads](https://img.shields.io/packagist/dt/shapecode/twig-string-loader.svg)](https://packagist.org/packages/shapecode/twig-string-loader)
9+
[![Monthly Downloads](https://img.shields.io/packagist/dm/shapecode/twig-string-loader.svg?label=monthly)](https://packagist.org/packages/shapecode/twig-string-loader)
10+
[![Daily Downloads](https://img.shields.io/packagist/dd/shapecode/twig-string-loader.svg?label=daily)](https://packagist.org/packages/shapecode/twig-string-loader)
11+
[![License](https://img.shields.io/packagist/l/shapecode/twig-string-loader.svg)](https://packagist.org/packages/shapecode/twig-string-loader)
12+
13+
## Install instructions
14+
15+
First you need to add `shapecode/twig-string-loader` to `composer.json`:
16+
17+
Do it by execute `composer require shapecode/twig-string-loader` or do it manually
18+
19+
``` json
20+
{
21+
"require": {
22+
"shapecode/twig-string-loader": "^1.0"
23+
}
24+
}
25+
```
26+
27+
Add the string loader to your `$twig` object
28+
29+
``` php
30+
<?php
31+
32+
// index.php
33+
//...
34+
35+
$loader1 = new \Twig\Loader\FilesystemLoader('/path/to/templates');
36+
$loader2 = new \Shapecode\Twig\Loader\StringLoader();
37+
$loader = new \Twig\Loader\ChainLoader([$loader1, $loader2]);
38+
39+
$twig = new \Twig\Environment($loader);
40+
```
41+
42+
## Usage
43+
44+
Now you can compile strings with twig:
45+
46+
``` php
47+
<?php
48+
49+
$twig->render('Hello {{ world }}', array(
50+
'world' => 'World'
51+
));
52+
```

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "shapecode/twig-string-loader",
3+
"description": "A string loader for twig",
4+
"keywords": [
5+
"shapecode",
6+
"twig",
7+
"template",
8+
"string",
9+
"loader"
10+
],
11+
"type": "library",
12+
"license": "MIT",
13+
"homepage": "https://github.com/shapecode/twig-string-loader",
14+
"support": {
15+
"email": "support@shapecode.de",
16+
"issues": "https://github.com/shapecode/twig-string-loader/issues",
17+
"source": "https://github.com/shapecode/twig-string-loader/releases",
18+
"wiki": "https://github.com/shapecode/twig-string-loader/wiki"
19+
},
20+
"authors": [
21+
{
22+
"name": "Nikita Loges",
23+
"homepage": "https://loges.one",
24+
"email": "dev@loges.one"
25+
}
26+
],
27+
"require": {
28+
"php": "~7.0",
29+
30+
"twig/twig": "~2.11|~3.0"
31+
},
32+
"autoload": {
33+
"psr-4": {
34+
"Shapecode\\": "src/"
35+
}
36+
},
37+
"extra": {
38+
"branch-alias": {
39+
"dev-master": "1.0-dev"
40+
}
41+
},
42+
"minimum-stability": "dev",
43+
"prefer-stable": true
44+
}

src/Twig/Loader/StringLoader.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Shapecode\Twig\Loader;
4+
5+
use Twig\Loader\LoaderInterface;
6+
use Twig\Source;
7+
8+
/**
9+
* Class StringLoader
10+
*
11+
* @package Shapecode\Bundle\TwigStringLoaderBundle\Twig\Loader
12+
* @author Nikita Loges
13+
*/
14+
class StringLoader implements LoaderInterface
15+
{
16+
17+
/**
18+
* @inheritDoc
19+
*/
20+
public function getSourceContext($name)
21+
{
22+
return new Source($name, $name);
23+
}
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
public function getCacheKey($name)
29+
{
30+
return $name;
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function isFresh($name, $time)
37+
{
38+
return true;
39+
}
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function exists($name)
45+
{
46+
return preg_match('/\s/', $name);
47+
}
48+
}

0 commit comments

Comments
 (0)