From 2603c4f44f9f74d79ccc20a5f1ba3930849c6288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 29 Sep 2017 16:19:31 +0200 Subject: [PATCH] Deduplicated blog templates by using macros. --- pillar/web/nodes/custom/posts.py | 3 + src/templates/nodes/custom/blog/_macros.pug | 81 +++++++++++++++++++ src/templates/nodes/custom/blog/index.pug | 79 +----------------- .../nodes/custom/blog/index_main_project.pug | 80 +----------------- .../nodes/custom/post/view_embed.pug | 22 +---- .../nodes/custom/post/view_main_project.pug | 2 - 6 files changed, 90 insertions(+), 177 deletions(-) create mode 100644 src/templates/nodes/custom/blog/_macros.pug diff --git a/pillar/web/nodes/custom/posts.py b/pillar/web/nodes/custom/posts.py index f9b31eca..84cb8e9d 100644 --- a/pillar/web/nodes/custom/posts.py +++ b/pillar/web/nodes/custom/posts.py @@ -88,6 +88,8 @@ def posts_view(project_id=None, project_url=None, url=None): post['properties']['content'] = pillar.web.nodes.attachments.render_attachments( post, post_contents) + can_create_blog_posts = project.node_type_has_method('post', 'POST', api=api) + return render_template( template_path, blog=blog, @@ -98,6 +100,7 @@ def posts_view(project_id=None, project_url=None, url=None): project=project, title='blog', node_type_post=project.get_node_type('post'), + can_create_blog_posts=can_create_blog_posts, api=api) diff --git a/src/templates/nodes/custom/blog/_macros.pug b/src/templates/nodes/custom/blog/_macros.pug new file mode 100644 index 00000000..b81513b6 --- /dev/null +++ b/src/templates/nodes/custom/blog/_macros.pug @@ -0,0 +1,81 @@ +//- ******************************************************* -// +| {% macro render_blog_post(node) %} +| {% if node.picture %} +.blog_index-header + img(src="{{ node.picture.thumbnail('l', api=api) }}") +| {% endif %} +.blog_index-item + a.item-title( + href="{{ url_for_node(node=node) }}") + | {{node.name}} + + .item-info. + {{node._created | pretty_date }} + {% if node._created != node._updated %} + (updated {{node._updated | pretty_date }}) + {% endif %} + {% if node.properties.category %}| {{node.properties.category}}{% endif %} + | by {{node.user.full_name}} + | Leave a comment + {% if node.properties.status != 'published' %} | {{ node.properties.status}} {% endif %} + + .item-content + | {{node.properties.content}} + + .item-meta + a(href="{{ url_for_node(node=node) }}#comments") Leave a comment +| {% endmacro %} + +//- ******************************************************* -// +| {% macro render_blog_index(project, posts, can_create_blog_posts, api) %} +| {% if can_create_blog_posts %} +a.btn.btn-default.button-create(href="{{url_for('nodes.posts_create', project_id=project._id)}}") + i.pi-plus + | Create New Post +| {% endif %} + +| {% if posts %} + +| {% for node in posts %} + +| {% if loop.first %} +| {{ render_blog_post(node) }} + +| {% else %} + +| {% if loop.index == 2 %} +h4.blog_index-title Blasts from the past +| {% endif %} + +.blog_index-item.list + | {% if node.picture %} + .item-header + img.image(src="{{ node.picture.thumbnail('s', api=api) }}") + | {% else %} + .item-header.nothumb + i.pi-document-text + | {% endif %} + a.item-title( + href="{{ url_for_node(node=node) }}") + | {{node.name}} + + .item-info. + {{node._created | pretty_date }} + {% if node._created != node._updated %} + (updated {{node._updated | pretty_date }}) + {% endif %} + {% if node.properties.category %}| {{node.properties.category}}{% endif %} + | by {{node.user.full_name}} + {% if node.properties.status != 'published' %} | {{ node.properties.status}} {% endif %} + +| {% endif %} {# loop #} + +| {% endfor %} {# posts #} + +| {% else %} + +.blog_index-item + .item-content No posts yet. + +| {% endif %} {# posts #} +| {% endmacro %} diff --git a/src/templates/nodes/custom/blog/index.pug b/src/templates/nodes/custom/blog/index.pug index 35eb2aae..bb5d95d6 100644 --- a/src/templates/nodes/custom/blog/index.pug +++ b/src/templates/nodes/custom/blog/index.pug @@ -1,4 +1,5 @@ | {% extends 'projects/view.html' %} +| {% import 'nodes/custom/blog/_macros.html' as blogmacros %} | {% set title = 'blog' %} | {% block page_title %}Blog{% endblock%} @@ -8,85 +9,9 @@ link(href="{{ url_for('static_pillar', filename='assets/css/blog.css', v=1732017 | {% endblock %} | {% block project_context %} - #blog_container - #blog_index-container - - | {% if project.node_type_has_method('post', 'POST', api=api) %} - a.btn.btn-default.button-create(href="{{url_for('nodes.posts_create', project_id=project._id)}}") - i.pi-plus - | Create New Post - | {% endif %} - - | {% if posts %} - - | {% for node in posts %} - - | {% if loop.first %} - | {% if node.picture %} - .blog_index-header - img(src="{{ node.picture.thumbnail('l', api=api) }}") - | {% endif %} - .blog_index-item - a.item-title( - href="{{ url_for_node(node=node) }}") - | {{node.name}} - - .item-info. - {{node._created | pretty_date }} - {% if node._created != node._updated %} - (updated {{node._updated | pretty_date }}) - {% endif %} - {% if node.properties.category %}| {{node.properties.category}}{% endif %} - | by {{node.user.full_name}} - | Leave a comment - {% if node.properties.status != 'published' %} | {{ node.properties.status}} {% endif %} - - .item-content - | {{node.properties.content}} - - .item-meta - a(href="{{ url_for_node(node=node) }}#comments") Leave a comment - - | {% else %} - - | {% if loop.index == 2 %} - h4.blog_index-title Blasts from the past - | {% endif %} - - .blog_index-item.list - | {% if node.picture %} - .item-header - img.image(src="{{ node.picture.thumbnail('s', api=api) }}") - | {% else %} - .item-header.nothumb - i.pi-document-text - | {% endif %} - a.item-title( - href="{{ url_for_node(node=node) }}") - | {{node.name}} - - .item-info. - {{node._created | pretty_date }} - {% if node._created != node._updated %} - (updated {{node._updated | pretty_date }}) - {% endif %} - {% if node.properties.category %}| {{node.properties.category}}{% endif %} - | by {{node.user.full_name}} - {% if node.properties.status != 'published' %} | {{ node.properties.status}} {% endif %} - - | {% endif %} {# loop #} - - | {% endfor %} {# posts #} - - | {% else %} - - .blog_index-item - .item-content No posts yet. - - | {% endif %} {# posts #} - + | {{ blogmacros.render_blog_index(project, posts, can_create_blog_posts, api) }} | {% endblock %} | {% block project_tree %} diff --git a/src/templates/nodes/custom/blog/index_main_project.pug b/src/templates/nodes/custom/blog/index_main_project.pug index 7cd998a5..b7a32d12 100644 --- a/src/templates/nodes/custom/blog/index_main_project.pug +++ b/src/templates/nodes/custom/blog/index_main_project.pug @@ -1,90 +1,14 @@ | {% extends 'layout.html' %} - +| {% import 'nodes/custom/blog/_macros.html' as blogmacros %} | {% set title = 'blog' %} | {% block page_title %}Blog{% endblock%} | {% block body %} - .container #blog_container.cloud-blog - #blog_index-container - - | {% if project.node_type_has_method('post', 'POST', api=api) %} - a.btn.btn-default.button-create(href="{{url_for('nodes.posts_create', project_id=project._id)}}") - i.pi-plus - | Create New Post - | {% endif %} - - | {% if posts %} - - | {% for node in posts %} - - | {% if loop.first %} - | {% if node.picture %} - .blog_index-header - img(src="{{ node.picture.thumbnail('l', api=api) }}") - | {% endif %} - .blog_index-item - a.item-title( - href="{{ url_for_node(node=node) }}") - | {{node.name}} - - .item-info. - {{node._created | pretty_date }} - {% if node._created != node._updated %} - (updated {{node._updated | pretty_date }}) - {% endif %} - {% if node.properties.category %}| {{node.properties.category}}{% endif %} - | by {{node.user.full_name}} - | Leave a comment - {% if node.properties.status != 'published' %} | {{ node.properties.status}} {% endif %} - - .item-content - | {{node.properties.content}} - - .item-meta - a(href="{{ url_for_node(node=node) }}#comments") Leave a comment - - | {% else %} - - | {% if loop.index == 2 %} - h4.blog_index-title Blasts from the past - | {% endif %} - - .blog_index-item.list - | {% if node.picture %} - .item-header - img.image(src="{{ node.picture.thumbnail('s', api=api) }}") - | {% else %} - .item-header.nothumb - i.pi-document-text - | {% endif %} - a.item-title( - href="{{ url_for_node(node=node) }}") - | {{node.name}} - - .item-info. - {{node._created | pretty_date }} - {% if node._created != node._updated %} - (updated {{node._updated | pretty_date }}) - {% endif %} - {% if node.properties.category %}| {{node.properties.category}}{% endif %} - | by {{node.user.full_name}} - {% if node.properties.status != 'published' %} | {{ node.properties.status}} {% endif %} - - | {% endif %} {# loop #} - - | {% endfor %} {# posts #} - - | {% else %} - - .blog_index-item - .item-content No posts yet. - - | {% endif %} {# posts #} - + | {{ blogmacros.render_blog_index(project, posts, can_create_blog_posts, api) }} | {% endblock %} | {% block footer_scripts %} diff --git a/src/templates/nodes/custom/post/view_embed.pug b/src/templates/nodes/custom/post/view_embed.pug index 0aeb9740..589f68a5 100644 --- a/src/templates/nodes/custom/post/view_embed.pug +++ b/src/templates/nodes/custom/post/view_embed.pug @@ -1,3 +1,4 @@ +| {% import 'nodes/custom/blog/_macros.html' as blogmacros %} #blog_container(class="{% if project and project._id == config.MAIN_PROJECT_ID %}cloud-blog{% endif %}") @@ -15,26 +16,7 @@ .clearfix | {% endif %} - | {% if node.picture %} - .blog_index-header - img(src="{{ node.picture.thumbnail('l', api=api) }}") - | {% endif %} - .blog_index-item - - .item-title - | {{node.name}} - - .item-info. - {{node._created | pretty_date }} - {% if node._created != node._updated %} - (updated {{node._updated | pretty_date }}) - {% endif %} - {% if node.properties.category %}| {{node.properties.category}}{% endif %} - | by {{node.user.full_name}} - - .item-content - | {{ node.properties.content }} - + | {{ blogmacros.render_blog_post(node) }} #comments-embed .comments-list-loading diff --git a/src/templates/nodes/custom/post/view_main_project.pug b/src/templates/nodes/custom/post/view_main_project.pug index 541b2fee..a22fda95 100644 --- a/src/templates/nodes/custom/post/view_main_project.pug +++ b/src/templates/nodes/custom/post/view_main_project.pug @@ -21,10 +21,8 @@ meta(property="og:image", content="{{ node.picture.thumbnail('l', api=api) }}") | {% set title = 'blog' %} | {% block body %} - .container | {% include 'nodes/custom/post/view_embed.html' %} - | {% endblock %}