You are here: Home / Documentation / How To's / More Macro Magic

More Macro Magic

by klotzj27 — published Dec 08, 2009 03:57 PM, last modified Aug 16, 2016 10:50 AM
How to do some more stuff with macros

Fill a macro slot:

-in order to reuse your template code

1) In a template named program_view(a custom view template for a program content type)... Define the macro where the slots are going to be... Also define anything in the macro that you want displayed when you fill the slots

<div metal:define-macro="program_macro">
<label class="formQuestion"><span>Program Contact Information</span></label><br/>
<p metal:define-slot="contactName" tal:replace="structure here/contactName|nothing">Contact Name</p><br />
<p metal:define-slot="contactTitle" tal:replace="structure here/contactTitle|nothing">Contact Title</p><br />
</div>

2) Use the macro where the slots are located in the calling page template... Fill the slots as shown below... Now when you change any boilerplate text in the original program macro it will be reflected in the calling page template... Note that you merely have to define a slot in the first template... Replacing the content with here/contactName is optional.

<div metal:use-macro="here/program_view/macros/program_macro">
   <div metal:fill-slot="contactName" tal:replace="structure itemObj/contactName|nothing">Contact Name</div>
   <div metal:fill-slot="contactTitle" tal:replace="structure itemObj/contactTitle|nothing">Contact Title</div>
</div>

Use a certain macro based on the id of a field:  

-comes in handy when not using a base view tempalte for a content type

<div tal:condition="python: here.widget('assocFaculty')">
     <div metal:use-macro="here/liveReference/macros/view"/>
</div> 

Create a custom widget:

-Widgets must be made up of a view, edit, and search macro... here is how they are defined

    <metal:view_macro define-macro="view">
    <!--Your widget view template-->
    </metal:view_macro>

    <metal:edit_macro define-macro="edit">
    <!--Your widget edit template-->
    </metal:edit_macro>

    <div metal:define-macro="search">
    <!--Your widget search template-->
    </div>

Call a macro using python:

-don't know why you would do this rather than using a tales statement but in case you need to

<metal:field use-macro="python:here.widget('title', mode='view')">
</metal:field>
Navigation