1

I want to add a widget that can't be removed from the page and contain some default text when it is not defined explicitly. I thought it should work something like this:

{{ 
   apos.singleton(data.page, 'headerTitle', 'apostrophe-rich-text', {
       def: 'Default Title'
   }) 
}}

Is there any way to do this with apostrophe widgets or I should create custom one?

antonys
  • 45
  • 4

1 Answers1

1

Creating your own widgets is pretty standard practice in Apostrophe but it doesn't address what to do if there is no widget yet in a singleton.

You can disable the removal of the singleton like this:

{{
  apos.singleton(data.page, 'headerTitle', 'apostrophe-rich-text', {
    controls: {
      removable: false,
      movable: false
    }
  })
}}

However an editor still has to click to initially add the widget to the page for each page.

So use this technique to provide default markup:

{% if apos.areas.isEmpty(data.page, 'headerTitle') %}
  <h4>Default Title</h4>
{% endif %}
{{
  apos.singleton(data.page, 'headerTitle', 'apostrophe-rich-text', {
    controls: {
      removable: false,
      movable: false
    }
  })
}}
Tom Boutell
  • 7,281
  • 1
  • 26
  • 23
  • antonys brings up a good point and I find myself trying to do the same thing. Maybe a widget isn't the correct construct. But, I want to build a global nav which populates itself automatically based on pages that have been created. The only way that I know of to get data into a view is to create a widget and implement `construct()`. But as you mentioned, when the site is deployed, someone still has to 'click' to add the nav...which seems odd. Is there a way to pull page data into the default layout? Extend apostrophe-templates or something? – stowns Jan 05 '21 at 18:04
  • welp, answered my own comment. Added an `index.js` to the apostrophe-templates module and queried for page data on `apostrophe-pages:beforeSend` and was able to reference the data in the `/views/layout.html` `beforeMain` block. dope. – stowns Jan 05 '21 at 18:19
  • In A3 you will often prefer the new async components for tasks like this. It's quite far along already, you might want to start evaluating. – Tom Boutell Jan 06 '21 at 22:47