The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.7KB

  1. <?php
  2. /*
  3. * Copyright (c) 2010-2013 Tinyboard Development Group
  4. */
  5. defined('TINYBOARD') or exit;
  6. $twig = false;
  7. function load_twig() {
  8. global $twig, $config;
  9. require 'lib/Twig/Autoloader.php';
  10. Twig_Autoloader::register();
  11. Twig_Autoloader::autoload('Twig_Extensions_Node_Trans');
  12. Twig_Autoloader::autoload('Twig_Extensions_TokenParser_Trans');
  13. Twig_Autoloader::autoload('Twig_Extensions_Extension_I18n');
  14. Twig_Autoloader::autoload('Twig_Extensions_Extension_Tinyboard');
  15. $loader = new Twig_Loader_Filesystem($config['dir']['template']);
  16. $loader->setPaths($config['dir']['template']);
  17. $twig = new Twig_Environment($loader, array(
  18. 'autoescape' => false,
  19. 'cache' => is_writable('templates') || (is_dir('templates/cache') && is_writable('templates/cache')) ?
  20. "{$config['dir']['template']}/cache" : false,
  21. 'debug' => $config['debug']
  22. ));
  23. $twig->addExtension(new Twig_Extensions_Extension_Tinyboard());
  24. $twig->addExtension(new Twig_Extensions_Extension_I18n());
  25. }
  26. function Element($templateFile, array $options) {
  27. global $config, $debug, $twig, $build_pages;
  28. if (!$twig)
  29. load_twig();
  30. if (function_exists('create_pm_header') && ((isset($options['mod']) && $options['mod']) || isset($options['__mod'])) && !preg_match('!^mod/!', $templateFile)) {
  31. $options['pm'] = create_pm_header();
  32. }
  33. if (isset($options['body']) && $config['debug']) {
  34. $_debug = $debug;
  35. if (isset($debug['start'])) {
  36. $_debug['time']['total'] = '~' . round((microtime(true) - $_debug['start']) * 1000, 2) . 'ms';
  37. $_debug['time']['init'] = '~' . round(($_debug['start_debug'] - $_debug['start']) * 1000, 2) . 'ms';
  38. unset($_debug['start']);
  39. unset($_debug['start_debug']);
  40. }
  41. if ($config['try_smarter'] && isset($build_pages) && !empty($build_pages))
  42. $_debug['build_pages'] = $build_pages;
  43. $_debug['included'] = get_included_files();
  44. $_debug['memory'] = round(memory_get_usage(true) / (1024 * 1024), 2) . ' MiB';
  45. $_debug['time']['db_queries'] = '~' . round($_debug['time']['db_queries'] * 1000, 2) . 'ms';
  46. $_debug['time']['exec'] = '~' . round($_debug['time']['exec'] * 1000, 2) . 'ms';
  47. $options['body'] .=
  48. '<h3>Debug</h3><pre style="white-space: pre-wrap;font-size: 10px;">' .
  49. str_replace("\n", '<br/>', utf8tohtml(print_r($_debug, true))) .
  50. '</pre>';
  51. }
  52. // Read the template file
  53. if (@file_get_contents("{$config['dir']['template']}/${templateFile}")) {
  54. $body = $twig->render($templateFile, $options);
  55. if ($config['minify_html'] && preg_match('/\.html$/', $templateFile)) {
  56. $body = trim(preg_replace("/[\t\r\n]/", '', $body));
  57. }
  58. return $body;
  59. } else {
  60. throw new Exception("Template file '${templateFile}' does not exist or is empty in '{$config['dir']['template']}'!");
  61. }
  62. }