是否可以用锚点导航一页?

时间:2018-10-12 14:29:41

标签: apostrophe-cms

不幸的是,我坚持为我的整个页面部分小部件构建导航。

我将apostrophe-pieces扩展为我所在的部分

lib/modules/sections/index.js


  module.exports = {
  extend: 'apostrophe-pieces',
  name: 'section',
  label: 'Section',
  pluralLabel: 'Sections',
  contextualOnly: false,
  addFields: [
    // Main Fields
    { name: 'title', label: 'Section Title', type: 'string', help: 'Select Title' },
    { name: 'color', label: 'Section Color', type: 'color', help: 'Select color' },
    // Backgrounds
    {
      name: '_image',
      type: 'joinByOne',
      withType: 'apostrophe-image',
      label: 'First Section Image',
      filters: {
       projection: {
         attachment: true,
         description: true,
         title: true
        }
      }
    }
  ],
  // Fields Arrangement
    arrangeFields: [
    { name: 'basics', label: 'Basics', fields: [ 'title', 'color' , '_image' ] },
  ]
};

并使用apostrophe-pieces-widgetshome.html上显示部分

lib/modules/sections-widgets/index.js

module.exports = {
  extend: 'apostrophe-pieces-widgets',
  name: 'sections',
  label: 'Sections Widget',
};

这是小部件本身:

lib/modules/sections-widgets/views/widget.html

{% for piece in data.widget._pieces %}
  <div class="section" id="{{ piece.title | lower }}" style="background-color:{{ piece.color }};
    {%- if data.page._image.attachment -%}
      background-image: url({{ apos.attachments.url(data.page._image.attachment, { size: data.options.size or 'full' }) }})
    {% endif %}
  ">
    <div class="main-content container">
      {%- if piece.title -%}
        <div class="header"><h3>{{ piece.title }}</h3></div>
      {%- endif -%}

      {{ apos.area(piece, 'a', {
        limit: 2,
        widgets: {
          'double': {
            controls: {
              movable: false,
              removable: true,
              position: 'top-right'
            }
          }
        }
      }) }}
    </div>
  </div>
{% endfor %}

到目前为止,一切工作正常,但是我想显示一个导航,该导航显示每个部分在单独的位置,例如在home.html

{% block beforeMain %}
{% include "sections-widgets:nav.html" %}
{% endblock %}

这是导航html文件:

lib/modules/sections-widgets/views/nav.html

<div class="nav">
  <nav style="background-color:{{ data.page.nav_color }};">
    <div class="nav-wrapper z-depth-2">
      <a href="{{ data.page._url }}" class="brand-logo"><h1>{{ data.page.title }}</h1></a>
      <a href="#" data-target="mobile" class="sidenav-trigger"><i class="material-icons">menu</i></a>
      <ul class="nav-activator right hide-on-med-and-down">

        // NOT WORKING PART
        {% for piece in data.widget._pieces %}
          <li><a href="#{{ data.piece.title | lower }}">{{ data.piece.title }}</a></li>
        {% endfor %}
        // END OF NOT WORKING PART

        <li><a href="#contact">Contact</a></li>
      </ul>
    </div>

我已标记出无法正常工作的部分,并且我不明白为什么{% for piece in data.widget._pieces %}在widget.html中却不能在nav.html中工作。我真的呆在那里,我不知道我的逻辑错误在哪里。我非常感谢您提供有关如何实现title之类的和平价值观的提示 在其他页面(例如home)中,以及如何构建一个数组来显示所有节标题及其链接。

最佳问候 费利克斯

1 个答案:

答案 0 :(得分:0)

困难之处在于您试图使用仅存在于data.widget中的widget.html

如果要构建导航小部件并在许多页面上使用该小部件,则应在适当的时候将其写在layout.html

{{ apos.singleton(data.global, 'nav', 'sections', {}) }}

现在您有了一个小部件,该小部件是共享global文档的一部分,而不是附加到特定页面上。无需进行任何include操作,因为您已经将代码放入了所有页面模板都可以扩展的layout.html模板中。

有关更多信息,请参见global doc tutorialusing blocks to override parts of the layout