[Grok-dev] Site layout
Sebastian Ware
sebastian at urbantalk.se
Tue Feb 24 07:00:46 EST 2009
I am just sending you some pointers in case you find it helpful. I use
macros for site wide design:
http://grok.zope.org/documentation/tutorial/macros-with-grok/tutorial-all-pages
I also use macros in lists such as search results to get content
specific rendering:
<tal:block repeat="searchResultItem view/searchResult">
<div>
<tal:block metal:use-macro="searchResultItem/@@searchresult/
macros/swedish_template" />
</div>
</tal:block>
and that searchresult.pt for a simple article could look something
like this:
class SearchResult(grok.View):
grok.context(IArticle)
[searchresult.pt]
<tal:block metal:define-macro="swedish_template">
<a tal:content="searchResultItem/title" tal:attributes="href
python:view.url(searchResultItem)">menu item</a>
<span class="author" tal:content="string:(Av ${searchResultItem/
author})">Content</span>
<div>
<tal:block replace="searchResultItem/preamble">Content</
tal:block>
</div>
</tal:block>
Note how "searchResultItem" is used to refernce the search result item
in both places.
I also call views on objects:
<tal:block condition="view/edit_object" replace="structure view/
edit_object/@@subeditform" />
to render an edit form in a typical list/detail view. Here my
subeditform.pt in turn references macros:
[subeditform.pt]
<div id="inline_edit_form">
<tal:block tal:condition="python:view.selectedForm == 'edit'">
<tal:block metal:use-macro="context/@@editformtemplate/macros/
form_template" />
</tal:block>
<tal:block tal:condition="python:view.selectedForm == 'history'">
<tal:block metal:use-macro="context/@@historyformtemplate/
macros/form_template" />
</tal:block>
</div>
Here is an example of a couple of widgets that I have put in
widgetstemplate.pt. It doesn't require much python:
class WidgetsTemplate(grok.View):
grok.context(Interface) # works with any object that has an
interface.
[widgetstemplate.pt]
<tal:block metal:define-macro="tabs_widget">
<tal:block repeat="tab view/tabs">
<a class="tab" tal:condition="not:tab/selected"
tal:attributes="href string:${tab/contextURL}/${tab/
viewName}">
<div class="inactive_tab" tal:content="tab/viewTitle">Tab
Label</div>
</a>
<div class="active_tab" tal:condition="tab/selected"
tal:content="tab/viewTitle">Tab Label</div>
</tal:block>
<tal:block condition="python:hasattr(view, 'translations')">
<div id="multilingual_selector" style="float:right; margin:
3px">
<select size="1" name="language_selector" >
<tal:block repeat="item view/translations">
<option tal:condition="python:view._selected_obj
== item[1]"
selected="selected"
tal:attributes="value python:item[1]"
tal:content="python:item[0]"> Name </
option>
<option tal:condition="python:view._selected_obj !
= item[1]"
tal:attributes="value python:item[1]"
tal:content="python:item[0]"> Name </
option>
</tal:block>
</select>
</div>
</tal:block>
</tal:block>
<tal:block metal:define-macro="object_path_widget">
<tal:block repeat="obj view/object_path">
<a tal:condition="python:obj.has_key('objURL')"
tal:attributes="href obj/objURL"
tal:content="obj/title">The title</a>
<tal:block condition="python:not obj.has_key('objURL')"
content="obj/title">The title</tal:block>
<tal:block condition="not:repeat/obj/end"> / </tal:block>
</tal:block>
</tal:block>
They are called this way (context being any object with an interface):
<tal:block metal:use-macro="context/@@widgetstemplate/macros/
object_path_widget" />
<tal:block metal:use-macro="context/@@widgetstemplate/macros/
tabs_widget" />
Mvh Sebastian
23 feb 2009 kl. 22.51 skrev Brian Wolf:
> All:
>
> I'm still new to Grok. Yes, I've read much of the tutorials, but
> still am not clear on some issues which you may deem basic. Try not
> to flame me.
>
> To maintain a consistent layout throughout the site, most template
> systems have a concept of breaking the page into parts, and
> assigning rendering of the part to another (sub-template). Very
> easy in Cheetah, for example. This seems like a more-or-less
> universal issue; most probably many of you have already tackled it.
>
> What is the best way to achieve that in Grok?
> Using macros? If so, how do you set up the macros/templates?
>
> Thanks.
> Brian
>
> --
> Brian Wolf
> brian at activustech.com
> Activus Technologies
> http://www.activustech.com
> 410.367-2958
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev
More information about the Grok-dev
mailing list