Skip to content

Commit d77eecb

Browse files
Sean MolenaarSean Molenaar
authored andcommitted
More templating support
1 parent 1c8fe71 commit d77eecb

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

src/PHPDraft/Out/HTML/default.phtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $base = $this->categories;
2323
<link rel="stylesheet" href="<?= $style ?>">
2424
<?php endforeach; ?>
2525
<style>
26-
<?= Minifier::minify_css(file_get_contents(__DIR__ . '/'.$this->template.'.css'));?>
26+
<?= Minifier::minify_css(file_get_contents($this->find_include_file($this->template, 'css'), true));?>
2727
</style>
2828
</head>
2929
<body>
@@ -340,6 +340,6 @@ if (!empty($extras)):
340340
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
341341
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
342342
crossorigin="anonymous"></script>
343-
<script><?= Minifier::minify_js(file_get_contents(__DIR__ . '/' . $this->template . '.js')); ?></script>
343+
<script><?= Minifier::minify_js(file_get_contents($this->find_include_file($this->template, 'js'), true)); ?></script>
344344
</body>
345345
</html>

src/PHPDraft/Out/TemplateGenerator.php

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,7 @@ public function __construct($template, $image)
8484
*/
8585
public function get($object)
8686
{
87-
$include = null;
88-
if (stream_resolve_include_path('templates' . DIRECTORY_SEPARATOR . $this->template . DIRECTORY_SEPARATOR . $this->template . '.phtml')) {
89-
$include =
90-
'templates' . DIRECTORY_SEPARATOR . $this->template . DIRECTORY_SEPARATOR . $this->template . '.phtml';
91-
}
92-
if (stream_resolve_include_path($this->template . DIRECTORY_SEPARATOR . $this->template . '.phtml')) {
93-
$include = $this->template . DIRECTORY_SEPARATOR . $this->template . '.phtml';
94-
}
95-
96-
if (stream_resolve_include_path($this->template . '.phtml')) {
97-
$include = $this->template . '.phtml';
98-
}
99-
100-
if (stream_resolve_include_path('PHPDraft/Out/HTML/' . $this->template . '.phtml')) {
101-
$include = 'PHPDraft/Out/HTML/' . $this->template . '.phtml';
102-
}
87+
$include = $this->find_include_file($this->template);
10388
if ($include === null) {
10489
file_put_contents('php://stderr', "Couldn't find template '$this->template'\n");
10590
exit(1);
@@ -143,6 +128,46 @@ public function get($object)
143128
require_once $include;
144129
}
145130

131+
/**
132+
* Get the path to a file to include
133+
*
134+
* @param string $template The name of the template to include
135+
* @param string $extension Extension of the file to include
136+
*
137+
* @return null|string File path or null if not found
138+
*/
139+
function find_include_file($template, $extension = 'phtml')
140+
{
141+
$include = null;
142+
$fextension = '.'.$extension;
143+
if (stream_resolve_include_path('templates' . DIRECTORY_SEPARATOR . $template . DIRECTORY_SEPARATOR . $template . $fextension)) {
144+
$include = 'templates' . DIRECTORY_SEPARATOR . $template . DIRECTORY_SEPARATOR . $template . $fextension;
145+
return $include;
146+
}
147+
if (stream_resolve_include_path('templates' . DIRECTORY_SEPARATOR . $template . $fextension)) {
148+
$include = 'templates' . DIRECTORY_SEPARATOR . $template . $fextension;
149+
return $include;
150+
}
151+
if (stream_resolve_include_path($template . DIRECTORY_SEPARATOR . $template . $fextension)) {
152+
$include = $template . DIRECTORY_SEPARATOR . $template . $fextension;
153+
return $include;
154+
}
155+
156+
if (stream_resolve_include_path($template . $fextension)) {
157+
$include = $template . $fextension;
158+
return $include;
159+
}
160+
161+
if (stream_resolve_include_path('PHPDraft/Out/HTML/' . $template . $fextension)) {
162+
$include = 'PHPDraft/Out/HTML/' . $template . $fextension;
163+
return $include;
164+
}
165+
166+
if ($include === null && $extension === 'phtml') {
167+
return $this->find_include_file('default', $extension);
168+
}
169+
}
170+
146171
/**
147172
* Get an icon for a specific HTTP Method
148173
*

0 commit comments

Comments
 (0)