From 066895cea97d0b730bc9b56054c9691e5181dd3f Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Wed, 27 Jul 2016 20:04:31 -0400 Subject: use theme template to render pages --- data/themes/blank/templates/page.html | 15 +++++++++++++++ data/themes/default/templates/page.html | 24 ++++++++++++++++++++++++ src/guff/models/theme.cr | 25 ++++++++++++++++--------- src/guff/views/page.cr | 12 ++++++++---- 4 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 data/themes/blank/templates/page.html create mode 100644 data/themes/default/templates/page.html diff --git a/data/themes/blank/templates/page.html b/data/themes/blank/templates/page.html new file mode 100644 index 0000000..78737aa --- /dev/null +++ b/data/themes/blank/templates/page.html @@ -0,0 +1,15 @@ + + + + + %{name | h} + + %{styles} + + + + %{body} + + + %{scripts} + diff --git a/data/themes/default/templates/page.html b/data/themes/default/templates/page.html new file mode 100644 index 0000000..5b64509 --- /dev/null +++ b/data/themes/default/templates/page.html @@ -0,0 +1,24 @@ + + + + + %{ + name | h + } + + %{styles} + + + +
+ page: %{name | h}
+ + by %{user_name | h} + on %{posted_at_text | h} +

+ %{body} +
+ + + %{scripts} + diff --git a/src/guff/models/theme.cr b/src/guff/models/theme.cr index 275ddff..76d9ea6 100644 --- a/src/guff/models/theme.cr +++ b/src/guff/models/theme.cr @@ -183,12 +183,12 @@ class Guff::Models::ThemeModel < Guff::Models::Model row = get(theme_id: theme_id) # load templates - @cache[:templates][theme_id] = Template::Cache.new(if row["is_system"] == "1" + @cache[:templates][theme_id] = Template::Cache.new(if is_system?(row) # read templates from templates dir read_templates(File.join( - @context.system_dir, + @context.config.system_dir, "themes", - row["theme_slug"], + row["theme_slug"].to_s, "templates" )) else @@ -206,7 +206,7 @@ class Guff::Models::ThemeModel < Guff::Models::Model row = get(theme_id: theme_id) # load templates - @cache[:metadata][theme_id] = (if row["is_system"] == "1" + @cache[:metadata][theme_id] = (if is_system?(row) # system themes have no metadata (for now) {} of String => String else @@ -219,16 +219,19 @@ class Guff::Models::ThemeModel < Guff::Models::Model end private def read_templates(dir_path : String) : Hash(String, String) - Dir.glob(File.join(templates_dir, "**/*")).reduce( + Dir.glob(File.join(dir_path, "*")).select { |path| + # skip hidden files + File.basename(path) =~ /\A[^\.]/m + }.reduce( {} of String => String ) do |r, path| - r[path.gsub(templates_dir + "/", "")] = File.read(path) + r[path.gsub(dir_path + "/", "")] = File.read(path, "utf8") r end end private def get_data(theme_id : Int32, type : String) - r = {} of String => Hash(String, String) + r = {} of String => String @context.dbs.ro.all(SQL[:get_data], [theme_id.to_s, type]) do |row| r[row["data_key"].to_s] = row["data_val"].to_s @@ -238,6 +241,10 @@ class Guff::Models::ThemeModel < Guff::Models::Model r end + private def is_system?(row) : Bool + (row["is_system"] as Int64) == 1 + end + ############### # add methods # ############### @@ -310,7 +317,7 @@ class Guff::Models::ThemeModel < Guff::Models::Model theme = get(slug: slug) return nil unless theme - if theme["is_system"].to_s == "1" + if is_system?(theme) File.join( @context.config.system_dir, "themes", @@ -362,7 +369,7 @@ class Guff::Models::ThemeModel < Guff::Models::Model # get theme theme = get(theme_id: theme_id) - @cache[:assets][theme_id] = (if theme["is_system"].to_s == "1" + @cache[:assets][theme_id] = (if is_system?(theme) # load manifest for system theme Theme::Packer::Manifest.load(File.join( @context.config.system_dir, diff --git a/src/guff/views/page.cr b/src/guff/views/page.cr index 95f91bd..dc628b3 100644 --- a/src/guff/views/page.cr +++ b/src/guff/views/page.cr @@ -7,11 +7,10 @@ class Guff::Views::PageView < Guff::Views::HTMLView @item[k]? && @item[k].size > 0 }].not_nil!.to_i32 as Int32 - # get theme slug + # get theme slug, assets, and templates @theme_slug = @context.models.theme.get(@theme_id)["theme_slug"] as String - - # get theme assets @theme_assets = @context.models.theme.assets(@theme_id) as Theme::Assets + @theme_templates = @context.models.theme.templates(@theme_id) as Template::Cache # get page assets @page_assets = @context.models.page.assets(@item["post_id"].to_i64) as Theme::Assets @@ -35,5 +34,10 @@ class Guff::Views::PageView < Guff::Views::HTMLView THEME_ASSET_PATH % [@theme_slug, asset_path] end - ECR.def_to_s("src/views/page.ecr") + def to_s(io) + @theme_templates["page.html"].run(io, @item.merge({ + "scripts" => scripts, + "styles" => styles, + })) + end end -- cgit v1.2.3