ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
TemplateHelper.php
Go to the documentation of this file.
1 <?php
7 namespace Whoops\Util;
8 
13 {
18  private $variables = array();
19 
26  public function escape($raw)
27  {
28  $flags = ENT_QUOTES;
29 
30  // HHVM has all constants defined, but only ENT_IGNORE
31  // works at the moment
32  if (defined("ENT_SUBSTITUTE") && !defined("HHVM_VERSION")) {
33  $flags |= ENT_SUBSTITUTE;
34  } else {
35  // This is for 5.3.
36  // The documentation warns of a potential security issue,
37  // but it seems it does not apply in our case, because
38  // we do not blacklist anything anywhere.
39  $flags |= ENT_IGNORE;
40  }
41 
42  return htmlspecialchars($raw, $flags, "UTF-8");
43  }
44 
52  public function escapeButPreserveUris($raw)
53  {
54  $escaped = $this->escape($raw);
55  return preg_replace(
56  "@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@",
57  "<a href=\"$1\" target=\"_blank\">$1</a>", $escaped
58  );
59  }
60 
67  public function slug($original)
68  {
69  $slug = str_replace(" ", "-", $original);
70  $slug = preg_replace('/[^\w\d\-\_]/i', '', $slug);
71  return strtolower($slug);
72  }
73 
82  public function render($template, array $additionalVariables = null)
83  {
84  $variables = $this->getVariables();
85 
86  // Pass the helper to the template:
87  $variables["tpl"] = $this;
88 
89  if ($additionalVariables !== null) {
90  $variables = array_replace($variables, $additionalVariables);
91  }
92 
93  call_user_func(function () {
94  extract(func_get_arg(1));
95  require func_get_arg(0);
96  }, $template, $variables);
97  }
98 
105  public function setVariables(array $variables)
106  {
107  $this->variables = $variables;
108  }
109 
116  public function setVariable($variableName, $variableValue)
117  {
118  $this->variables[$variableName] = $variableValue;
119  }
120 
129  public function getVariable($variableName, $defaultValue = null)
130  {
131  return isset($this->variables[$variableName]) ?
132  $this->variables[$variableName] : $defaultValue;
133  }
134 
140  public function delVariable($variableName)
141  {
142  unset($this->variables[$variableName]);
143  }
144 
150  public function getVariables()
151  {
152  return $this->variables;
153  }
154 }
slug($original)
Convert a string to a slug version of itself.
escape($raw)
Escapes a string for output in an HTML document.
Exposes useful tools for working with/in templates.
delVariable($variableName)
Unsets a single template variable, by its name.
setVariables(array $variables)
Sets the variables to be passed to all templates rendered by this template helper.
getVariable($variableName, $defaultValue=null)
Gets a single template variable, by its name, or $defaultValue if the variable does not exist...
render($template, array $additionalVariables=null)
Given a template path, render it within its own scope.
setVariable($variableName, $variableValue)
Sets a single template variable, by its name:
escapeButPreserveUris($raw)
Escapes a string for output in an HTML document, but preserves URIs within it, and converts them to c...
getVariables()
Returns all variables for this helper.
Whoops - php errors for cool kids.
Definition: Misc.php:7