How to define widgets with default values in Apostrophe CMS

别说谁变了你拦得住时间么 提交于 2019-12-11 04:47:05

问题


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?


回答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
    }
  })
}}


来源:https://stackoverflow.com/questions/46673815/how-to-define-widgets-with-default-values-in-apostrophe-cms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!