Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 33601efe3e2804d98972bb4e4bfe4c09079b349e @damz committed Dec 7, 2010
Showing with 179 additions and 0 deletions.
  1. +3 −0 .gitmodules
  2. +14 −0 README.md
  3. +6 −0 hairy/hairy.info
  4. +56 −0 hairy/page.mustache
  5. +99 −0 mustache.engine
  6. +1 −0 mustache.php
@@ -0,0 +1,3 @@
+[submodule "mustache.php"]
+ path = mustache.php
+ url = https://github.com/damz/mustache.php.git
@@ -0,0 +1,14 @@
+Drupal Mustache
+===============
+
+An (experimental) Drupal 7 theme engine based on "Mustache":http://mustache.github.com/.
+
+Currently requires a patch to Mustache.PHP to support lambda tags.
+
+Usage
+-----
+
+ cd sites/all/themes
+ mkdir engines
+ cd engines
+ git clone --recursive https://github.com/damz/mustache-drupal.git mustache
@@ -0,0 +1,6 @@
+name = Hairy
+description = A demonstration theme for Mustache.
+package = Core
+version = VERSION
+core = 7.x
+engine = mustache
@@ -0,0 +1,56 @@
+{{%UNESCAPED}}
+{{%DOT-NOTATION}}
+ <div id="page-wrapper"><div id="page">
+
+ <div id="header"><div class="section clearfix">
+
+ {{#logo}}
+ <a href="{{front_page}}" title="{{#t}}Home{{/t}}" rel="home" id="logo">
+ <img src="{{logo}}" alt="{{#t}}Home{{/t}}" />
+ </a>
+ {{/logo}}
+
+ {{#drupal_render}}page.header{{/drupal_render}}
+
+ </div></div> <!-- /.section, /#header -->
+
+ {{#breadcrumb}}
+ <div id="breadcrumb">{{breadcrumb}}</div>
+ {{/breadcrumb}}
+
+ {{messages}}
+
+ <div id="main-wrapper"><div id="main" class="clearfix">
+
+ <div id="content" class="column"><div class="section">
+ {{#page.highlighted}}<div id="highlighted">{{#drupal_render}}page.highlighted{{/drupal_render}}</div>{{/page.highlighted}}
+ <a id="main-content"></a>
+ {{#drupal_render}}title_prefix{{/drupal_render}}
+ {{#title}}<h1 class="title" id="page-title">{{title}}</h1>{{/title}}
+ {{#drupal_render}}title_suffix{{/drupal_render}}
+ {{#tabs}}<div class="tabs">{{#drupal_render}}tabs{{/drupal_render}}</div>{{/tabs}}
+ {{#drupal_render}}page.help{{/drupal_render}}
+ {{#action_links}}<ul class="action-links">{{#drupal_render}}action_links{{/drupal_render}}</ul>{{/action_links}}
+ {{#drupal_render}}page.content{{/drupal_render}}
+ {{feed_icons}}
+ </div></div> <!-- /.section, /#content -->
+
+ {{#page.sidebar_first}}
+ <div id="sidebar-first" class="column sidebar"><div class="section">
+ {{#drupal_render}}page.sidebar_first{{/drupal_render}}
+ </div></div> <!-- /.section, /#sidebar-first -->
+ {{/page.sidebar_first}}
+
+ {{#page.sidebar_second}}
+ <div id="sidebar-second" class="column sidebar"><div class="section">
+ {{#drupal_render}}page.sidebar_second{{/drupal_render}}
+ </div></div> <!-- /.section, /#sidebar-second -->
+ {{/page.sidebar_second}}
+
+ </div></div> <!-- /#main, /#main-wrapper -->
+
+ <div id="footer"><div class="section">
+ {{#drupal_render}}page.footer{{/drupal_render}}
+ </div></div> <!-- /.section, /#footer -->
+
+ </div></div> <!-- /#page, /#page-wrapper -->
@@ -0,0 +1,99 @@
+<?php
+
+require_once dirname(__FILE__) . '/mustache.php/Mustache.php';
+
+/**
+ * Implements hook_init().
+ */
+function mustache_init($template) {
+ $file = dirname($template->filename) . '/template.php';
+ if (file_exists($file)) {
+ include_once DRUPAL_ROOT . '/' . $file;
+ }
+}
+
+/**
+ * Implements hook_theme().
+ */
+function mustache_theme($existing, $type, $theme, $path) {
+ // We still need those.
+ $templates = drupal_find_theme_functions($existing, array($theme));
+
+ // Discover mustache templates.
+ $templates += drupal_find_theme_templates($existing, '.mustache', $path);
+
+ return $templates;
+}
+
+/**
+ * Implements THEME_ENGINE_render_template().
+ */
+function mustache_render_template($template_file, $variables) {
+ $m = new DrupalMustache($variables);
+ return $m->render(file_get_contents($template_file));
+}
+
+/**
+ * Implements THEME_ENGINE_extension().
+ */
+function mustache_extension() {
+ return '.mustache';
+}
+
+/**
+ * Drupal-specific extensions to Mustache.
+ */
+class DrupalMustache extends Mustache {
+ var $variables = array();
+
+ public function __construct($variables) {
+ $this->variables = $variables;
+
+ // Shortcuts to Drupal rendering functions.
+ $this->variables['drupal_render'] = array($this, '_render');
+ $this->variables['show'] = array($this, '_show');
+ $this->variables['hide'] = array($this, '_hide');
+ $this->variables['t'] = array($this, 't');
+ }
+
+ public function __get($key) {
+ return isset($this->variables[$key]) ? $this->variables[$key] : NULL;
+ }
+
+ public function __isset($key) {
+ return isset($this->variables[$key]);
+ }
+
+ // Shortcuts to Drupal rendering functions.
+
+ // This is basically drupal_array_get_nested_value() returning a reference.
+ public function &_drupal_array_get_nested_value(array &$array, array $parents, &$key_exists = NULL) {
+ $ref = &$array;
+ foreach ($parents as $parent) {
+ if (is_array($ref) && array_key_exists($parent, $ref)) {
+ $ref = &$ref[$parent];
+ }
+ else {
+ $key_exists = FALSE;
+ return NULL;
+ }
+ }
+ $key_exists = TRUE;
+ return $ref;
+ }
+
+ public function _render($element) {
+ $array = &$this->_drupal_array_get_nested_value($this->variables, explode('.', $element));
+ return render($array);
+ }
+
+ public function _show($element) {
+ $array = &$this->_drupal_array_get_nested_value($this->variables, explode('.', $element));
+ return show($array);
+ }
+
+ public function _hide($element) {
+ $array = &$this->_drupal_array_get_nested_value($this->variables, explode('.', $element));
+ return hide($array);
+ }
+}
Submodule mustache.php added at d1aa0d

0 comments on commit 33601ef

Please sign in to comment.