Skip to content

Commit 71742e7

Browse files
committed
[FEATURE] ✨ Add static method to fetch example template
Add a static method to get the example template. - No need to create the object - Preparation to solve the bug that example styles are set by default
1 parent 7e4edf6 commit 71742e7

File tree

4 files changed

+55
-23
lines changed

4 files changed

+55
-23
lines changed

README.md

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,31 @@ status pages, TOC pages, or any other minimal single-serving site.
1313

1414
## Vision
1515

16-
This package provides a single class to turn a text or HTML-snippet into a valid
17-
HTML5 document.
16+
This package provides a single class to turn a message or HTML-snippet into
17+
a valid HTML5 document.
1818

1919
This way it is possible to let an app return an HTML response without the need
2020
to store a template file beforehand or initialize a full-blown template engine.
2121

22-
The package therefore does not have template variables, modifiers or parsers.
23-
Three lines of code should be sufficient to wrap a given message into a valid
24-
HTML document. See [»Usage«](#usage) for some examples.
22+
The package therefore does not have template variables or modifiers.
23+
Two lines of code should be sufficient to wrap a given text into a valid
24+
HTML document. One more to add a link to fancy stylesheet.
25+
26+
See [»Usage«](#usage) for some examples.
2527

2628
The package follows the KISS principle.
2729

2830
## Webapp
2931

3032
This package is used on [html5example.com](https://html5example.com/).
3133

32-
If you are in need of an HTML document once only, then use a commandline tool
33-
like HTTPie and run `http https://html5example.com > index.html`.
34+
If you are in need of an HTML document once only, then you may use a
35+
commandline tool like HTTPie and run
36+
`http https://html5example.com > index.html` to save a template file.
3437

3538
The webapp supports some [options](#options) of this package as well,
36-
for example `http POST https://html5example.com title=Minimal-Template > index.html`
39+
for example
40+
`http POST https://html5example.com title=Minimal-Template > index.html`
3741
to pass a custom title.
3842

3943
## Requirements
@@ -48,29 +52,42 @@ Packagist Entry https://packagist.org/packages/pixelbrackets/html5-mini-template
4852

4953
https://gitlab.com/pixelbrackets/html5-mini-template/
5054

55+
Mirror https://github.com/pixelbrackets/html5-mini-template
56+
5157
## Usage
5258

53-
1. Get the example template
59+
1. Wrap a message into a HTML5 document, return to a PSR-7 implementation
5460
```php
55-
$document = (new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate())->getMarkup();
61+
$template = new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate();
62+
$template->setContent('<h1>Status</h1><p>All Systems Operational</p>');
63+
return $template->getMarkup();
5664
```
5765

58-
1. Get template with custom content and write to file
66+
1. Wrap a message, use a preconfigured CSS framework CDN
67+
(see [»options«](#options) for a list of supported frameworks),
68+
and save the document into a file
5969
```php
6070
$template = new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate();
71+
$template->setStylesheet('skeleton');
6172
$template->setContent('<h1>Index</h1><p>Nothing to see here</p>');
6273
file_put_contents('/var/www/example/index.html', $template->getMarkup());
6374
```
6475

65-
1. Get template with custom content, an external link to a CSS framework
66-
output the document as response
76+
1. Wrap a message, set a custom stylesheet URL,
77+
set a title, output the document
6778
```php
6879
$template = new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate();
69-
$template->setStylesheet('skeleton');
70-
$template->setContent('<h1>Status</h1><p>All Systems Operational</p>');
80+
$template->setStylesheet('/assets/styles.css');
81+
$template->setTitle('Index');
82+
$template->setContent('<h1>TOC</h1><ul><li>a</li><li>b</li></ul>');
7183
echo $template->getMarkup();
7284
```
7385

86+
1. Get the example template only (👉 or use the [Webapp](#webapp))
87+
```php
88+
echo \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate::getTemplate();
89+
```
90+
7491
### Options
7592

7693
- `setContent()` the message to show, any HTML string to replace the main
@@ -90,9 +107,12 @@ https://gitlab.com/pixelbrackets/html5-mini-template/
90107
- `setTitle()` the title is the first headline found in the document, unless
91108
it is overwritten with this option
92109
- `setAdditionalMetadata()` any additional metadata like metatags or link
93-
references, for example a canonical link to avoid duplicate content - Usage
94-
of this option is an indicator that the given use case is too specific and
95-
switching to a template engine should be considered
110+
references, for example a canonical link to avoid duplicate content
111+
- ⚠️ Usage of this option is an indicator that the given use case is too
112+
specific and switching to a template engine like the minimal
113+
[slim/php-view](https://packagist.org/packages/slim/php-view) or the
114+
powerfull [twig/twig](https://packagist.org/packages/twig/twig)
115+
should be considered
96116

97117
## License
98118

src/Html5MiniTemplate.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Html5MiniTemplate
6767
/**
6868
* Mini Template
6969
*
70-
* @return string
70+
* @return void
7171
*/
7272
public function __construct()
7373
{
@@ -118,6 +118,16 @@ public function getMarkup()
118118
return $this->parseMarkup();
119119
}
120120

121+
/**
122+
* Get example template
123+
*
124+
* @return string
125+
*/
126+
public static function getTemplate()
127+
{
128+
return file_get_contents(__DIR__ . '/../resources/template.html');
129+
}
130+
121131
/**
122132
* Set title
123133
*
@@ -195,7 +205,7 @@ protected function getStylesheetContent()
195205
$stylesheetUrl = $this->getStylesheetUrl();
196206

197207
// try to get a cached version first
198-
$cachefile = sys_get_temp_dir(). '/' . md5($stylesheetUrl) . '.css';
208+
$cachefile = sys_get_temp_dir() . '/' . md5($stylesheetUrl) . '.css';
199209
if (file_exists($cachefile) && (filemtime($cachefile) > (time() - 86400))) { // 1 day
200210
return file_get_contents($cachefile);
201211
}

tests/example-page.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
require __DIR__ . '/../vendor/autoload.php';
44

5-
echo (new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate())->getMarkup();
5+
echo \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate::getTemplate();
6+
7+
// same as
8+
// echo (new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate())->getMarkup();

tests/unit/Html5MiniTemplateTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ class Html5MiniTemplateTest extends TestCase
77
{
88
public function testExampleTemplate()
99
{
10-
$template = new \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate();
10+
$document = \Pixelbrackets\Html5MiniTemplate\Html5MiniTemplate::getTemplate();
1111

12-
$document = $template->getMarkup();
1312
$this->assertNotEmpty($document);
1413
$this->assertStringContainsString('<!DOCTYPE html>', $document);
1514
$this->assertStringContainsString('<h1>HTML5 Example Page</h1>', $document);

0 commit comments

Comments
 (0)