Skip to content
This repository was archived by the owner on Nov 14, 2023. It is now read-only.

Commit 274a8ca

Browse files
author
Florens Verschelde
authored
Merge pull request #36 from fvsch/bugfix-29-composer
Normalize path slashes for Windows (fixes #29)
2 parents 203806f + 2f0c139 commit 274a8ca

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

CHANGELOG.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
Kirby Twig Plugin: Change log
22
=============================
33

4-
v3.0.0
5-
------
4+
## v3.0.2
5+
6+
- #29 Fix loading some templates on Windows
7+
8+
## v3.0.1
9+
10+
- #25 Can't register namespaces, functions & filters in plugins
11+
- #26 Somehow allow adding safe functions
12+
13+
## v3.0.0
614

715
**Breaking changes:**
816

@@ -18,8 +26,7 @@ Deprecated (still working):
1826
- `twig.env.filters` in favor of `twig.filter.myFilter`;
1927
- `twig.env.namespace.xyz` in favor of `twig.namespace.xyz`.
2028

21-
v2.x
22-
----
29+
## v2.x
2330

2431
See on GitHub:
2532

@@ -30,8 +37,7 @@ See on GitHub:
3037
- [v2.0.1](https://github.com/fvsch/kirby-twig/releases/tag/v2.0.1)
3138
- [v2.0.0](https://github.com/fvsch/kirby-twig/releases/tag/v2.0.0)
3239

33-
v1.x
34-
----
40+
## v1.x
3541

3642
- [v1.3.0](https://github.com/fvsch/kirby-twig/releases/tag/v1.3.0)
3743
- [v1.2.0](https://github.com/fvsch/kirby-twig/releases/tag/v1.2.0)

src/TwigEnv.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function __construct()
116116
{
117117
$kirby = Kirby::instance();
118118
$this->debug = $kirby->get('option', 'debug');
119-
$this->templateDir = $kirby->roots()->templates();
119+
$this->templateDir = $this->normalizePath($kirby->roots()->templates());
120120

121121
// Get environment & user config
122122
$options = [
@@ -128,9 +128,9 @@ public function __construct()
128128
],
129129
'namespace' => [
130130
'templates' => $this->templateDir,
131-
'snippets' => $kirby->roots->snippets(),
132-
'plugins' => $kirby->roots->plugins(),
133-
'assets' => $kirby->roots->assets()
131+
'snippets' => $this->normalizePath($kirby->roots->snippets()),
132+
'plugins' => $this->normalizePath($kirby->roots->plugins()),
133+
'assets' => $this->normalizePath($kirby->roots->assets())
134134
],
135135
'function' => $this->cleanNames(array_merge(
136136
$this->defaultFunctions,
@@ -205,8 +205,11 @@ public function renderPath($filePath='', $tplData=[], $return=true, $isPage=fals
205205
{
206206
// Remove the start of the templates path, since Twig asks
207207
// for a path starting from one of the registered directories.
208-
$path = ltrim(str_replace($this->templateDir, '',
209-
preg_replace('#[\\\/]+#', '/', $filePath)), '/');
208+
$path = $this->normalizePath($filePath);
209+
$prefix = $this->templateDir . '/';
210+
if (strpos($path, $prefix) === 0) {
211+
$path = substr($path, strlen($prefix));
212+
}
210213

211214
try {
212215
$content = $this->twig->render($path, $tplData);
@@ -348,6 +351,15 @@ private function getSourceExcerpt($source='', $line=1, $plus=1, $format=false)
348351
return implode("\n", $excerpt);
349352
}
350353

354+
/**
355+
* Normalize file paths, especially for Windows slashes
356+
*/
357+
private function normalizePath($path)
358+
{
359+
$clean = trim(preg_replace('#[\\\/]+#', '/', $path));
360+
return rtrim($clean, '/');
361+
}
362+
351363
/**
352364
* Clean up function names for use in Twig templates
353365
* Returns ['twig name' => 'callable name']

0 commit comments

Comments
 (0)