Skip to content

Instantly share code, notes, and snippets.

@mtdowling
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtdowling/19ed3b9d233609c9a3ed to your computer and use it in GitHub Desktop.
Save mtdowling/19ed3b9d233609c9a3ed to your computer and use it in GitHub Desktop.
Sami static templates
diff --git a/README.rst b/README.rst
index d7f7d17..cb1961d 100644
--- a/README.rst
+++ b/README.rst
@@ -200,6 +200,9 @@ the default theme:
'js/bootstrap.min.js': 'js/bootstrap.min.js'
'js/jquery-1.11.1.min.js': 'js/jquery-1.11.1.min.js'
+ static_templates:
+ 'api_tree': 'layout/api_tree.twig'
+
global:
'index.twig': 'index.html'
'doc-index.twig': 'doc-index.html'
@@ -216,7 +219,6 @@ the default theme:
class:
'class.twig': '%s.html'
-
Files are contained into sections, depending on how Sami needs to treat them:
* ``static``: Files are copied as is (for assets like images, stylesheets, or
@@ -228,6 +230,10 @@ Files are contained into sections, depending on how Sami needs to treat them:
* ``class``: Templates that should be generated for every class.
+* ``static_templates``: Templates that are rendered before other templates. The
+ key is used as a global Twig parameter and the value is the path to the
+ template. An ``api_tree`` key is required.
+
.. _Symfony API: http://api.symfony.com/
.. _phar file: http://get.sensiolabs.org/sami.phar
.. _Finder: http://symfony.com/doc/current/components/finder.html
diff --git a/Sami/Renderer/Renderer.php b/Sami/Renderer/Renderer.php
index 0131b83..594d65c 100644
--- a/Sami/Renderer/Renderer.php
+++ b/Sami/Renderer/Renderer.php
@@ -72,6 +72,7 @@ class Renderer
$this->twig->addGlobal('has_namespaces', $project->hasNamespaces());
$this->twig->addGlobal('project', $project);
+ $this->renderStatic($project, $callback);
$this->renderStaticTemplates($project, $callback);
$this->renderGlobalTemplates($project, $callback);
$this->renderNamespaceTemplates($diff->getModifiedNamespaces(), $project, $callback);
@@ -89,7 +90,7 @@ class Renderer
return $diff;
}
- protected function renderStaticTemplates(Project $project, $callback = null)
+ protected function renderStatic(Project $project, $callback = null)
{
if (null !== $callback) {
call_user_func($callback, Message::RENDER_PROGRESS, array('Static', 'Rendering files', $this->getProgression()));
@@ -109,14 +110,7 @@ class Renderer
protected function renderGlobalTemplates(Project $project, $callback = null)
{
- $variables = array(
- 'namespaces' => $project->getNamespaces(),
- 'interfaces' => $project->getProjectInterfaces(),
- 'classes' => $project->getProjectClasses(),
- 'items' => $this->getIndex($project),
- 'index' => $this->indexer->getIndex($project),
- 'tree' => $this->getTree($project),
- );
+ $variables = $this->getGlobalTemplateVariables($project);
foreach ($this->theme->getTemplates('global') as $template => $target) {
if (null !== $callback) {
@@ -229,4 +223,27 @@ class Renderer
return $this->cachedTree[$project];
}
+
+ private function getGlobalTemplateVariables(Project $project)
+ {
+ return array(
+ 'namespaces' => $project->getNamespaces(),
+ 'interfaces' => $project->getProjectInterfaces(),
+ 'classes' => $project->getProjectClasses(),
+ 'items' => $this->getIndex($project),
+ 'index' => $this->indexer->getIndex($project),
+ 'tree' => $this->getTree($project),
+ );
+ }
+
+ private function renderStaticTemplates(Project $project, callable $callback = null)
+ {
+ $variables = $this->getGlobalTemplateVariables($project);
+ $templates = $this->theme->getTemplates('static_templates');
+
+ foreach ($templates as $name => $template) {
+ $result = $this->twig->render($template, $variables);
+ $this->twig->addGlobal($name, $result);
+ }
+ }
}
diff --git a/Sami/Renderer/ThemeSet.php b/Sami/Renderer/ThemeSet.php
index 720930e..cd2326e 100644
--- a/Sami/Renderer/ThemeSet.php
+++ b/Sami/Renderer/ThemeSet.php
@@ -48,11 +48,17 @@ class ThemeSet
$parents[$config['name']] = $config['parent'];
}
- foreach (array('static', 'global', 'namespace', 'class') as $type) {
+ foreach (array('static', 'global', 'namespace', 'class', 'static_templates') as $type) {
if (isset($config[$type])) {
$theme->setTemplates($type, $config[$type]);
}
}
+
+ $staticTemplates = $theme->getTemplates('static_templates');
+
+ if (!isset($staticTemplates['api_tree'])) {
+ throw new \RuntimeException('A static_templates -> api_tree value must be configured');
+ }
}
// populate parent
diff --git a/Sami/Resources/themes/default/layout/api_tree.twig b/Sami/Resources/themes/default/layout/api_tree.twig
new file mode 100644
index 0000000..2112945
--- /dev/null
+++ b/Sami/Resources/themes/default/layout/api_tree.twig
@@ -0,0 +1,26 @@
+{% macro element(tree, opened, depth) %}
+ <ul>
+ {%- for element in tree -%}
+ {% if element[2] %}
+ <li data-name="namespace:{{ element[1]|replace({'\\': '_'}) }}" {% if depth < opened %}class="opened"{% endif %}>
+ <div style="padding-left:{{ depth * 18 }}px" class="hd">
+ <span class="glyphicon glyphicon-play"></span>{% if not project.config('simulate_namespaces') %}<a href="{{ na
+ </div>
+ <div class="bd">
+ {{ _self.element(element[2], opened, depth + 1) -}}
+ </div>
+ </li>
+ {% else %}
+ <li data-name="class:{{ (element[1].name)|replace({'\\': '_'}) }}" {% if depth < opened %}class="opened"{% endif %}>
+ <div style="padding-left:{{ 8 + (depth * 18) }}px" class="hd leaf">
+ <a href="{{ class_path(element[1]) }}">{{ element[0] }}</a>
+ </div>
+ </li>
+ {% endif %}
+ {%- endfor %}
+ </ul>
+{% endmacro %}
+
+<div id="api-tree">
+ {{ _self.element(tree, project.config('default_opened_level'), 0) }}
+</div>
diff --git a/Sami/Resources/themes/default/layout/layout.twig b/Sami/Resources/themes/default/layout/layout.twig
index bee54a7..5b01533 100644
--- a/Sami/Resources/themes/default/layout/layout.twig
+++ b/Sami/Resources/themes/default/layout/layout.twig
@@ -1,28 +1,5 @@
{% extends "layout/base.twig" %}
-{% macro element(tree, opened, depth) %}
- <ul>
- {%- for element in tree -%}
- {% if element[2] %}
- <li data-name="namespace:{{ element[1]|replace({'\\': '_'}) }}" {% if depth < opened %}class="opened"{% endif %}>
- <div style="padding-left:{{ depth * 18 }}px" class="hd">
- <span class="glyphicon glyphicon-play"></span>{% if not project.config('simulate_namespaces') %}<a href="{{ na
- </div>
- <div class="bd">
- {{ _self.element(element[2], opened, depth + 1) -}}
- </div>
- </li>
- {% else %}
- <li data-name="class:{{ (element[1].name)|replace({'\\': '_'}) }}" {% if depth < opened %}class="opened"{% endif %}>
- <div style="padding-left:{{ 8 + (depth * 18) }}px" class="hd leaf">
- <a href="{{ class_path(element[1]) }}">{{ element[0] }}</a>
- </div>
- </li>
- {% endif %}
- {%- endfor %}
- </ul>
-{% endmacro %}
-
{% block head %}
{{ parent() }}
{{ block('treejs') }}
@@ -73,9 +50,7 @@
{% endblock %}
{% block leftnav %}
- <div id="api-tree">
- {{ _self.element(tree, project.config('default_opened_level'), 0) }}
- </div>
+ {{ api_tree|raw }}
{% endblock %}
{% block control_panel %}
diff --git a/Sami/Resources/themes/default/manifest.yml b/Sami/Resources/themes/default/manifest.yml
index 01031f3..8049644 100644
--- a/Sami/Resources/themes/default/manifest.yml
+++ b/Sami/Resources/themes/default/manifest.yml
@@ -13,6 +13,9 @@ static:
'js/bootstrap.min.js': 'js/bootstrap.min.js'
'js/jquery-1.11.1.min.js': 'js/jquery-1.11.1.min.js'
+static_templates:
+ 'api_tree': 'layout/api_tree.twig'
+
global:
'index.twig': 'index.html'
'doc-index.twig': 'doc-index.html'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment