1 | <?php |
||
40 | class Php extends TemplateEngine { |
||
41 | |||
42 | 31 | public function generate($templateVariables) { |
|
43 | // Escape each variable by passing it through the variable class. |
||
44 | // Users would have to unescape them by calling the escape method directly |
||
45 | // on the variable. |
||
46 | 31 | foreach ($templateVariables as $_key => $_value) { |
|
47 | 25 | $$_key = php\Variable::initialize($_value); |
|
48 | } |
||
49 | |||
50 | // Expose helpers |
||
51 | 31 | $helpers = $this->getHelpersLoader(); |
|
52 | |||
53 | // Start trapping the output buffer and include the PHP template for |
||
54 | // execution. |
||
55 | 31 | ob_start(); |
|
56 | try { |
||
57 | 31 | include $this->template; |
|
58 | 2 | } catch (\Exception $e) { |
|
59 | 2 | ob_get_flush(); |
|
60 | 2 | throw $e; |
|
61 | } |
||
62 | 29 | return ob_get_clean(); |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * A utility function to strip the text of all HTML code. This function |
||
67 | * removes all HTML tags instead of escaping them. |
||
68 | * |
||
69 | * @param string $text |
||
70 | * @return string |
||
71 | */ |
||
72 | 1 | public function strip($text) { |
|
75 | |||
76 | /** |
||
77 | * A utility function to cut long pieces of text into meaningful short |
||
78 | * chunks. The function is very good in cases where you want to show just |
||
79 | * a short preview snippet of a long text. The function cuts the long string |
||
80 | * without cutting through words and appends some sort of ellipsis |
||
81 | * terminator to the text. |
||
82 | * |
||
83 | * @param string $text The text to be truncated. |
||
84 | * @param string $length The maximum lenght of the truncated string. Might |
||
85 | * return a shorter string if the lenght ends in the middle of a word. |
||
86 | * @param string $terminator The ellipsis terminator to use for the text. |
||
87 | * @return string |
||
88 | */ |
||
89 | 1 | public function truncate($text, $length, $terminator = ' ...') { |
|
90 | 1 | while (mb_substr($text, $length, 1) != ' ' && $length > 0) { |
|
91 | 1 | $length--; |
|
92 | } |
||
93 | 1 | return mb_substr($text, 0, $length) . $terminator; |
|
94 | } |
||
95 | |||
96 | protected function generateFromString($string, $data) { |
||
97 | \ntentan\utils\StringStream::register(); |
||
98 | file_put_contents('string://template', $string); |
||
99 | $this->template = 'string://template'; |
||
100 | return $this->generate($data); |
||
101 | } |
||
102 | |||
103 | 2 | public function __destruct() { |
|
106 | |||
107 | } |
||
108 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.