Use metas to render title
authorThierry Florac <thierry.florac@onf.fr>
Thu, 05 Jul 2018 12:33:05 +0200
changeset 367 a211d8c17334
parent 366 2b5123202f9f
child 368 e1c1c57c2d4c
Use metas to render title
src/pyams_skin/metas.py
--- a/src/pyams_skin/metas.py	Thu Jul 05 09:16:19 2018 +0200
+++ b/src/pyams_skin/metas.py	Thu Jul 05 12:33:05 2018 +0200
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_i18n.interfaces import II18n
 
 __docformat__ = 'restructuredtext'
 
@@ -50,6 +51,22 @@
 #
 
 @implementer(IMetaHeader)
+class HTMLTagMeta(object):
+    """HTML tag meta header"""
+
+    def __init__(self, tag, content, **attrs):
+        self.tag = tag
+        self.content = content
+        self.attrs = attrs
+
+    def render(self):
+        return '''<{tag} {attrs}>{content}</{tag}>'''.format(tag=self.tag,
+                                                             attrs=' '.join(('{0}="{1}"'.format(*value) for value in
+                                                                             self.attrs.items())),
+                                                             content=self.content)
+
+
+@implementer(IMetaHeader)
 class HTTPEquivMeta(object):
     """HTTP-Equiv meta header"""
 
@@ -133,6 +150,17 @@
         yield ContentMeta('viewport', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no')
 
 
+@adapter_config(name='title', context=(Interface, Interface, Interface), provides=IHTMLContentMetas)
+class TitleMetasAdapter(ContextRequestViewAdapter):
+    """Title metas adapter"""
+
+    order = 1
+
+    def get_metas(self):
+        title = II18n(self.context).query_attribute('title', request=self.request)
+        yield HTMLTagMeta('title', title)
+
+
 @adapter_config(name='content-type', context=(Interface, Interface, Interface), provides=IHTMLContentMetas)
 class ContentTypeMetasAdapter(ContextRequestViewAdapter):
     """Content-type metas adapter"""