Permalink
Browse files

Merge branch 'master' of https://github.com/m3nt0r/cakephp-twig-view

  • Loading branch information...
2 parents f8beaae + 844b10d commit 22f4061d5973a9305be7de83ec69f4266b8fb37a @m3nt0r committed Jun 25, 2012
Showing with 75 additions and 0 deletions.
  1. +75 −0 views/twig.php
View
@@ -220,7 +220,82 @@ function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = fal
return $out;
}
+ /**
+ * Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
+ *
+ * This realizes the concept of Elements, (or "partial layouts")
+ * and the $params array is used to send data to be used in the
+ * Element. Elements can be cached through use of the cache key.
+ *
+ * @param string $name Name of template file in the/app/views/elements/ folder
+ * @param array $params Array of data to be made available to the for rendered
+ * view (i.e. the Element)
+ * Special params:
+ * cache - enable caching for this element accepts boolean or strtotime compatible string.
+ * Can also be an array
+ * if an array,'time' is used to specify duration of cache. 'key' can be used to
+ * create unique cache files.
+ *
+ * @return string Rendered Element
+ * @access public
+ */
+ function element($name, $params = array(), $loadHelpers = false) {
+ $file = $plugin = $key = null;
+
+ if (isset($params['plugin'])) {
+ $plugin = $params['plugin'];
+ }
+
+ if (isset($this->plugin) && !$plugin) {
+ $plugin = $this->plugin;
+ }
+
+ if (isset($params['cache'])) {
+ $expires = '+1 day';
+
+ if (is_array($params['cache'])) {
+ $expires = $params['cache']['time'];
+ $key = Inflector::slug($params['cache']['key']);
+ } elseif ($params['cache'] !== true) {
+ $expires = $params['cache'];
+ $key = implode('_', array_keys($params));
+ }
+
+ if ($expires) {
+ $cacheFile = 'element_' . $key . '_' . $plugin . Inflector::slug($name);
+ $cache = cache('views' . DS . $cacheFile, null, $expires);
+ if (is_string($cache)) {
+ return $cache;
+ }
+ }
+ }
+ $paths = $this->_paths($plugin);
+ $exts = array($this->ext, '.ctp', '.thtml');
+ foreach ($exts as $ext) {
+ foreach ($paths as $path) {
+ if (file_exists($path . 'elements' . DS . $name . $ext)) {
+ $file = $path . 'elements' . DS . $name . $ext;
+ break 2;
+ }
+ }
+ }
+
+ if (is_file($file)) {
+ $params = array_merge_recursive($params, $this->loaded);
+ $element = $this->_render($file, array_merge($this->viewVars, $params), $loadHelpers);
+ if (isset($params['cache']) && isset($cacheFile) && isset($expires)) {
+ cache('views' . DS . $cacheFile, $element, $expires);
+ }
+ return $element;
+ }
+ $file = $paths[0] . 'elements' . DS . $name . $this->ext;
+
+ if (Configure::read() > 0) {
+ return "Not Found: " . $file;
+ }
+ }
+
/**
* Require filter set once, add filter name to self::twigLoadedFilters
*

0 comments on commit 22f4061

Please sign in to comment.