aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-07-27 20:04:31 -0400
committerPaul Duncan <pabs@pablotron.org>2016-07-27 20:04:31 -0400
commit066895cea97d0b730bc9b56054c9691e5181dd3f (patch)
tree592530f6c1e70cf97cded4e73317bc7433a4a384
parent8ffde2e23c21ccae81f34c34ef115993719d34eb (diff)
downloadguff-066895cea97d0b730bc9b56054c9691e5181dd3f.tar.bz2
guff-066895cea97d0b730bc9b56054c9691e5181dd3f.zip
use theme template to render pages
-rw-r--r--data/themes/blank/templates/page.html15
-rw-r--r--data/themes/default/templates/page.html24
-rw-r--r--src/guff/models/theme.cr25
-rw-r--r--src/guff/views/page.cr12
4 files changed, 63 insertions, 13 deletions
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 @@
+<!DOCTYPE html>
+<html lang='en-US'>
+ <head>
+ <meta charset="utf-8"/>
+ <title>%{name | h}</title>
+
+ %{styles}
+ </head>
+
+ <body>
+ %{body}
+ </body>
+
+ %{scripts}
+</html>
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 @@
+<!DOCTYPE html>
+<html lang='en-US'>
+ <head>
+ <meta charset="utf-8"/>
+ <title>%{
+ name | h
+ }</title>
+
+ %{styles}
+ </head>
+
+ <body>
+ <div class='post'>
+ <b>page: %{name | h}</b><br/>
+ <b>
+ by <span class='user'>%{user_name | h}</span>
+ on <span class='date'>%{posted_at_text | h}</span>
+ </b><br/><br/>
+ %{body}
+ </div>
+ </body>
+
+ %{scripts}
+</html>
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