How to define area widgets from a macro in Apostrophe CMS

454 views Asked by At

I want to define the widgets for all of my columns and layouts in one place.

This code would be in views/macros/columnWidgets.html

 {% macro columnWidgets(data, option) %}
{{ apos.area(data.widget, '{{option}}', {
blockLevelControls: true,
widgets: {
'apostrophe-rich-text': {
   toolbar: [ 'Styles', 'Bold', 'Italic', 'Blockquote', 'Link', 'Anchor', 'Unlink', 'BulletedList' ],
   styles: [
    { name: 'Paragraph',  element: 'p'  },
    { name: 'Quote / Section Descriptor', element: 'h3' },
    { name: 'Main Heading', element: 'h1',attributes: { 'class': 'main-heading'}  }
   ]
 },
 'apostrophe-html': {
   toolbar: [ 'Styles', 'Bold', 'Italic', 'Link', 'Unlink', 'Anchor', 'Table', 'BulletedList', 'Blockquote', 'Strike', 'Subscript',  'Superscript','Image','slideshow' ],
   styles: [
     { name: 'Marker: Yellow', element: 'span', styles: { 'background-color': 'Yellow' } }
   ]
 },
 'apostrophe-images': {
   minSize: [ 700, 350 ],
   aspectRatio: [ 2, 1 ],
   size: 'full'
 }
}
}) }}
{% endmacro %}

Then in my 4Column Layout Widget I would include something along these lines.

{% import 'macros/columnWidgets.html' as columnwidgets %}

{{ apps.columnWidgets(data,'column1') }}

1

There are 1 answers

5
Tom Boutell On BEST ANSWER

Sure, we do this all the time in our own projects.

// in app.js

modules: {
  'apostrophe-templates': {
    viewsFolderFallback: __dirname + '/views'
  }, ...
}

{# In views/macros/areas.html %}

{% macro content(context, name) %}
  {{ apos.area(context, name, {
    widgets: {
      'apostrophe-rich-text': {
        toolbar: [ 'Styles', 'Bold', 'Italic', 'Link', 'Anchor', 'Unlink', 'BulletedList' ]
      },
      'apostrophe-images': {},
      'apostrophe-files': {},
      'apostrophe-video': {}
    }
  }) }}
{% endmacro %}

{# In any other template in any module #}

{% import "macros/areas.html" as areas %}
{{ areas.content(data.page, 'body') }}

The use of viewsFolderFallback is a nice convenience for allowing a shared top-level folder to become a fallback location for template files imported, included, extended, etc. from any module.