You are here: Home / Documentation / How To's / Using Macros: A Use Case

Using Macros: A Use Case

by klotzj27 — published Sep 23, 2009 01:18 PM, last modified Aug 16, 2016 10:50 AM
A simple example of using macros.

Macros are very useful for Plone developers because they promote code re-use which obviously makes for cleaner, more efficient code.

In this use case we are going to modify a page template to embed another page template using a macro. The initial page template renders a collective.easyslider item and collections using the standard view by default. We want it to use our custom view for the collections(to include body text) while rendering the easyslider in the same way. We may use macros to pull in parts of pages or whole pages and will do so here.

To recap we are trying to make:

null

 

Look like this:

null

 

 Notice how the collection items now have body text.

 

To do this we go to sliderview.pt(the template we want to pull a macro in to):

-We find the section(<metal:body define-macro="folderlisting_macro">) that renders collections currently, delete its contents, and replace them with a reference to our macro. 

<metal:body define-macro="folderlisting_macro">
<div metal:use-macro="here/collection_with_body_view/macros/main">Use the macro</div>
</metal:body>

Note:

-'collection_with_body_view' is the page template where we define our macro

-'macros' refers to the macros template attribute which holds information about all macros defined in that page template

-'main' refers to the name of the macro

 

The main macro is defined as such in collection_with_body_view:

<metal:main-macro define-macro="main"
tal:define="kssClassesView context/@@kss_field_decorator_view;
getKssClasses nocall:kssClassesView/getKssClassesInlineEditable;
templateId template/getId">

... A bunch on template code is here including our customization ...

</metal:main-macro>

 

Now, using macros, we have pulled the contents of one page template into another. Hooray!

Navigation