Skip to content

Commit 01226db

Browse files
authored
Merge pull request #36 from gushphp/kalypso63-master
Adds support for variable path on twig tag inlinecss
2 parents 15c9543 + 06dd6b9 commit 01226db

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ Paths relative to bundle are supported as well:
9898
</div>
9999
{% endinlinecss %}
100100
```
101+
Dynamic variable is supported (Use absolute path with variable with asset or directly with full path to file)
102+
103+
``` html
104+
{% set path = asset('css/email.css', null, true) %}
105+
{% inlinecss path %}
106+
<div class="foo">
107+
...
108+
</div>
109+
{% endinlinecss %}
110+
```
111+
Dynamic variable is supported (Use absolute path with variable with asset or directly with full path to file)
112+
113+
``` html
114+
{% set path = asset('css/email.css', null, true) %}
115+
{% inlinecss path %}
116+
<div class="foo">
117+
...
118+
</div>
119+
{% endinlinecss %}
120+
```
101121

102122
Read the docs in the files for further details on the usage of the service.
103123

Twig/InlineCssNode.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,29 @@ public function __construct(\Twig_Node $body, $css, $lineno = 0, $debug, $tag =
2525

2626
public function compile(Twig_Compiler $compiler)
2727
{
28-
if ($this->debug) {
29-
$css = sprintf("file_get_contents('%s')", $this->getAttribute('css'));
28+
if (is_string($this->getAttribute('css'))) {
29+
if ($this->debug) {
30+
$css = sprintf("file_get_contents('%s')", $this->getAttribute('css'));
31+
} else {
32+
$css = '"' . addslashes(file_get_contents($this->getAttribute('css'))) . '"';
33+
}
34+
$compiler->addDebugInfo($this)
35+
->write("ob_start();\n")
36+
->subcompile($this->getNode('body'))
37+
->write(sprintf('echo $context["inlinecss"]->inlineCSS(ob_get_clean(), %s);' . "\n", $css))
38+
;
3039
} else {
31-
$css = '"' . addslashes(file_get_contents($this->getAttribute('css'))) . '"';
40+
//get path of css
41+
$compiler
42+
->addDebugInfo($this)
43+
->write("ob_start();\n")
44+
->write('$css = addslashes(file_get_contents(')
45+
->subcompile($this->getAttribute('css'))
46+
->raw('));')
47+
->subcompile($this->getNode('body'))
48+
->write('echo $context["inlinecss"]->inlineCSS(ob_get_clean(), $css);' . "\n")
49+
;
3250
}
3351

34-
$compiler->addDebugInfo($this)
35-
->write("ob_start();\n")
36-
->subcompile($this->getNode('body'))
37-
->write(sprintf('echo $context["inlinecss"]->inlineCSS(ob_get_clean(), %s);' . "\n", $css))
38-
;
3952
}
4053
}

Twig/InlineCssParser.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ public function parse(Twig_Token $token)
5656
{
5757
$lineNo = $token->getLine();
5858
$stream = $this->parser->getStream();
59-
$path = $stream->expect(Twig_Token::STRING_TYPE)->getValue();
59+
if ($stream->test(Twig_Token::STRING_TYPE)) {
60+
$css = $this->resolvePath($stream->expect(Twig_Token::STRING_TYPE)->getValue());
61+
} else {
62+
$css = $this->parser->getExpressionParser()->parseExpression();
63+
}
6064
$stream->expect(Twig_Token::BLOCK_END_TYPE);
6165
$body = $this->parser->subparse(array($this, 'decideEnd'), true);
6266
$stream->expect(Twig_Token::BLOCK_END_TYPE);
6367

64-
return new InlineCssNode($body, $this->resolvePath($path), $lineNo, $this->debug);
68+
return new InlineCssNode($body, $css, $lineNo, $this->debug);
6569
}
6670

6771
/**
@@ -90,7 +94,7 @@ private function resolvePath($path)
9094
return $this->locator->locate($path, $this->webRoot);
9195
} catch (\InvalidArgumentException $e) {
9296
// happens when path is not bundle relative
93-
return $this->webRoot.'/'.$path;
97+
return $this->webRoot . '/' . $path;
9498
}
9599
}
96-
}
100+
}

0 commit comments

Comments
 (0)