ローカル環境でa-blog cmsのテンプレートを見るためのJavaScript

a-blog cmsのテンプレートは、a-blog cmsをインストールした環境でないと閲覧できません。このJavaScriptはページに読み込むだけで、a-blog cmsを入れていないローカル環境(XAMPPやMAMPなどのサーバ環境、PreprosやGulpなどのビルド環境)でa-blog cmsのテンプレートが閲覧できるようになります。 2021年1月18日、@extends、@sectionにも対応しました。


ダウンロード


使い方

ローカルで確認したいページに、jQueryとJavaScriptを読み込みます。

@extendsを使用しない場合

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. @include("/include/head/ga.html")
  5. @include("/include/head/meta.html")
  6. @include("/include/head/link.html")
  7. @include("/include/head/js.html")
  8. <!-- BEGIN_IF [%{BCD}/eq/0] -->
  9. <script src="../js/jquery-3.4.1.min.js"></script>
  10. <script src="../template.js"></script>
  11. <!-- END_IF -->
  12. </head>
  13. <body id="top" class="">
  14. @include("include/header.html")
  15. <main class="site-content">
  16. Here's Content
  17. </main>
  18. @include("include/footer.html")
  19. @include("/include/foot/js.html")
  20. </body>
  21. </html>

@extendsを使用する場合

/include/layouts/base.html で @section("header") で定義したヘッダーを上書きする例です。

  1. <!-- BEGIN_IF [%{BCD}/eq/0] -->
  2. <script src="../js/jquery-3.4.1.min.js"></script>
  3. <script src="../template.js"></script>
  4. <!-- END_IF -->
  5. @extends("/include/layouts/base.html")
  6. @section(header)
  7. <div class="site-header">
  8. Override Header
  9. </div>
  10. @endsection

中身の解説

@extends、@include、@sectionの内容をAjaxで読み込んでいます。必要に環境に合わせて調整してください。

  1. $(document).ready(function(){
  2. /**
  3. * テンプレートを解決
  4. */
  5. function resolveTemplate(){
  6. var deferred = (new $.Deferred()).resolve();
  7. // @extendsを解決
  8. deferred.then(resolveExtends);
  9. // @includeを解決
  10. deferred.then(resolveIncludes);
  11. // @sectionを解決
  12. deferred.then(resolveSections);
  13. // 未解決のテンプレートを処理しHTMLに追加
  14. deferred.then(function(){
  15. bodyHtml = bodyHtml.replace(/\@extends\([\s\S]*?\)/g, "");
  16. bodyHtml = bodyHtml.replace(/\@include\([\s\S]*?\)/g, "");
  17. bodyHtml = bodyHtml.replace(/\@section\(.*?\)/g, '').replace(/@endsection/g, '');
  18. $('body').html(bodyHtml);
  19. console.info('Done');
  20. });
  21. }
  22. /**
  23. * @extendsを解決
  24. */
  25. function resolveExtends(){
  26. console.info('Resolve Extends');
  27. // @extendsのテンプレートを取得
  28. var acms_extends = bodyHtml.match(/\@extends\([\s\S]*?\)/g);
  29. if( acms_extends ){
  30. // テンプレート内に書かれた@sectionを削除
  31. bodyHtml = bodyHtml.replace(/\@section\(([\s\S]*?)\)([\s\S]*?)\@endsection/mg, "");
  32. for (var i = 0; i < acms_extends.length; i++) {
  33. // インクルード先を取得する
  34. var path = acms_extends[i].match(/\@extends\(([\s\S]*?)\)/)[1];
  35. // インクルード先を読み込む
  36. loadTemplate(path, '@extends('+path+')');
  37. }
  38. }
  39. }
  40. /**
  41. * @includeを解決
  42. */
  43. function resolveIncludes(){
  44. console.info('Resolve Includes');
  45. var acms_includes = bodyHtml.match(/\@include\([\s\S]*?\)/g);
  46. if( acms_includes ){
  47. for (var i = 0; i < acms_includes.length; i++) {
  48. // インクルード先を取得する
  49. var path = acms_includes[i].match(/\@include\(([\s\S]*?)\)/)[1];
  50. // インクルード先を読み込む
  51. loadTemplate(path, '@include('+path+')');
  52. }
  53. }
  54. }
  55. /**
  56. * @sectionを解決
  57. */
  58. function resolveSections(){
  59. console.info('Resolve Section');
  60. bodyHtml = bodyHtml.replace(/\@section\(["|'| ]([\s\S]*?)["|'| ]\)/, "@section($1)");
  61. var acms_sections = sourceHtml.match(/\@section\(([\s\S]*?)\)([\s\S]*?)\@endsection/g);
  62. if( acms_sections ){
  63. for (var i = 0; i < acms_sections.length; i++) {
  64. // セクション先を取得する
  65. var section = acms_sections[i].match(/\@section\(([\s\S]*?)\)([\s\S]*?)\@endsection/);
  66. var target = section[1].replace(/["|'| ]/g, "");
  67. var content = section[2];
  68. console.info('@section('+target+')');
  69. var reg = new RegExp('\\@section\\('+target+'\\)[\\s\\S]*?\\@endsection', 'g');
  70. if( bodyHtml.match(reg) ){
  71. bodyHtml = bodyHtml.replace(reg, content);
  72. }
  73. }
  74. }
  75. }
  76. /**
  77. * テンプレートを読み込み
  78. */
  79. function loadTemplate(path, directive){
  80. // インクルード先から不要なものを削除する
  81. var includeTmp = path.replace(/["|'| ]/g, "");
  82. // ルートパスならテーマディレクトリを参照する
  83. var prefixTmp = path.search(/^["|']?\//) !== -1 ? currentScriptDir : "";
  84. $.ajax({
  85. async : false,
  86. url : prefixTmp + includeTmp,
  87. success: function(response){
  88. // ルートパスを相対パスに変更
  89. response = response.replace(/=\"\//g, '="'+currentScriptDir);
  90. // 該当の@includeを消す
  91. console.info(directive);
  92. bodyHtml = bodyHtml.replace(directive, response);
  93. }
  94. });
  95. }
  96. currentScriptDir = $('script[src*="template.js"]:first').attr('src').replace('template.js', '');
  97. $('script[src*="template.js"]:first').remove();
  98. sourceHtml = $('body').html();
  99. bodyHtml = $('body').html();
  100. // サーバなら処理しない
  101. if( location.pathname.search(/\/themes\//) === -1 ){
  102. return false;
  103. }
  104. // テンプレートを解決
  105. resolveTemplate();
  106. });

Todo

将来的には a-template.js を参考にEntry_Summaryであればダミーの内容を出すなどの実装を行いたいです。

  • スタンドアローンで動くように調整
  • ブロックを解決
  • 変数を解決

コメント

お名前 必須

名前を入力してください。

メールアドレス

正しいメールアドレスを入力してください。

URL

正しいURLを入力してください。

タイトル

タイトルを入力してください。

タイトルに不適切な言葉が含まれています。

コメント必須

コメントを入力してください。

コメントに不適切な言葉が含まれています

パスワード必須

パスワードを入力してください。

パスワードは半角小文字英数字で入力してください。

Cookie

関連記事

この記事のハッシュタグに関連する記事が見つかりませんでした。