Skip to content

Commit

Permalink
Added twig for templating
Browse files Browse the repository at this point in the history
  • Loading branch information
xy2z committed Nov 16, 2017
1 parent c5bed05 commit ad46fc9
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 72 deletions.
4 changes: 3 additions & 1 deletion composer.json
Expand Up @@ -8,5 +8,7 @@
"name": "xy"
}
],
"require": {}
"require": {
"twig/twig": "^2.4"
}
}
144 changes: 144 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 24 additions & 11 deletions public/index.php
Expand Up @@ -11,6 +11,13 @@
exit('Error: Missing PHP YAML extension. Get it at https://pecl.php.net/package/yaml and https://github.com/xy2z/PineDocs/wiki/Install-YAML-extension-for-PHP-7.0-(Windows---Ubuntu)');
}

// Load composer
$composer_autoload_path = __DIR__ . '/../vendor/autoload.php';
if (!file_exists($composer_autoload_path)) {
exit('Error: Missing composer libraries. Try running "composer install" to complete the installation.');
}
require $composer_autoload_path;

// Autoloader
spl_autoload_register(function ($class_name) {
require_once '../src/' . $class_name . '.php';
Expand Down Expand Up @@ -85,25 +92,31 @@


// Prepare template.
$main = new xyTemplate('main');
$main->set_data(array(
'search_value' => '',
'search_placeholder' => 'Search here...',
));

$loader = new Twig_Loader_Filesystem('../src/templates');
$twig = new Twig_Environment($loader);
$template = $twig->load('main.html');

// Set HTML variables
$main->set_html(array(
// Set template replacements
$template_replacements = array(
'menu' => $tree->render_tree_return(),
'version' => PineDocs::version,
'search' => array(
'value' => '',
'placeholder' => 'Search here...',
),
'config' => array(
'title' => PineDocs::$config->title,
'code_transparent_bg' => PineDocs::$config->code_transparent_bg,
'open_dirs' => PineDocs::$config->open_dirs,
'theme' => basename(PineDocs::$config->theme),
'color_scheme' => basename(PineDocs::$config->color_scheme),
'highlight_theme' => PineDocs::$config->highlight_theme,
'index_data' => $index_data,
'logo' => PineDocs::$config->logo,
'render_footer' => PineDocs::$config->render_footer,
),
'errors' => PineDocs::$errors,
));

);

// Render main template.
$main->render();
echo $template->render($template_replacements);
24 changes: 12 additions & 12 deletions src/templates/main.php → src/templates/main.html
Expand Up @@ -4,11 +4,11 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= PineDocs::$config->title ?></title>
<title>{{ config.title }}</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="css/base.css">
<link rel="stylesheet" type="text/css" href="themes/<?= strtolower(basename(PineDocs::$config->theme)) ?>.css">
<link rel="stylesheet" type="text/css" href="color-schemes/<?= strtolower(basename(PineDocs::$config->color_scheme)) ?>.css">
<link rel="stylesheet" type="text/css" href="themes/{{ config.theme | lower }}.css">
<link rel="stylesheet" type="text/css" href="color-schemes/{{ config.color_scheme | lower }}.css">

<script src="//cdn.jsdelivr.net/npm/alertifyjs@1.11.0/build/alertify.min.js"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/alertifyjs@1.11.0/build/css/alertify.min.css"/>
Expand All @@ -21,16 +21,16 @@
<div id="menu_wrapper" class="">
<div id="menu" class="navbar-default">
<div id="menu_top">
<a href="."><img id="logo" src="<?= PineDocs::$config->logo ?>" /></a>
<input type="text" id="search" name="search" value="<?= $search_value ?>" placeholder="<?= $search_placeholder ?>" autofocus>
<a href="."><img id="logo" src="{{ config.logo }}" /></a>
<input type="text" id="search" name="search" value="{{ search.value }}" placeholder="{{ search.placeholder }}" autofocus>
</div>

<?= $menu ?>
<?php if (PineDocs::$config->render_footer): ?>
{{ menu | raw }}
{% if config.render_footer %}
<footer>
PineDocs <?= PineDocs::version ?> (<a target="_blank" href="https://github.com/xy2z/PineDocs/releases">Check for updates</a>)
PineDocs {{ version }} (<a target="_blank" href="https://github.com/xy2z/PineDocs/releases">Check for updates</a>)
</footer>
<?php endif ?>
{% endif %}
</div>
<button aria-label="Close navigation" type="button" id="menu_close">X</button>
</div>
Expand All @@ -49,11 +49,11 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.6/marked.min.js"></script>
<script src="js/PineDocs.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/styles/<?= PineDocs::$config->highlight_theme ?>.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/styles/{{ config.highlight_theme }}.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/highlight.min.js"></script>
<script>
var config = <?= json_encode($config) ?>;
var errors = <?= json_encode($errors) ?>;
var config = {{ config | json_encode() | raw }}
var errors = {{ errors | json_encode() | raw }}
</script>
</body>

Expand Down
48 changes: 0 additions & 48 deletions src/xyTemplate.php

This file was deleted.

0 comments on commit ad46fc9

Please sign in to comment.