أضف محتوى مخصص يظهر فقط على صفحتك الرئيسية

A very common situation you’ll find yourself in as a theme developer is the need to create content that only shows on the homepage of your community.

You might add some HTML to the “After Header” section of your theme, which will then appear on every page. You can jump through some hoops in CSS to hide this everywhere except the homepage… but instead let’s use a Discourse theme to create a component with content that is only visible on your homepage.

If you’re unfamiliar with Discourse themes check out Beginner's guide to using Discourse Themes and Structure of themes and theme components

In your Discourse theme you’ll need to setup the following directory structure:

:file_folder: javascripts/discourse/components/
:file_folder: javascripts/discourse/connectors/

From here we’re going to create an Ember component. You can find more about Ember components from their documentation: Ember.js Guides - Guides and Tutorials - Ember Guides

But for now this will be a simple component. The component will consist of two files, one containing the logic and another the template.

:page_facing_up: javascripts/discourse/components/custom-homepage-content.js

import Component from "@glimmer/component";
import { service } from "@ember/service";
import { defaultHomepage } from "discourse/lib/utilities";

export default class CustomHomepageContent extends Component {
  @service router;

  get isHomepage() {
    const { currentRouteName } = this.router;
    return currentRouteName === `discovery.${defaultHomepage()}`;
  }
}

This creates a isHomepage getter, which checks the router service for the currentRouteName — if the route name matches your homepage (as dictated by site settings) then it will return true

Now we need our template

:page_facing_up: javascripts/discourse/components/custom-homepage-content.hbs

{{#if this.isHomepage}}
  <h1>This is my homepage HTML content</h1>
{{/if}}

The template checks the isHomepage getter, and will display your content if it’s true. You can add any HTML you want between the {{#if}} blocks.

Now that our component is created, we need to add it to Discourse somewhere. For this step you’ll need to decide which plugin outlet to utilize. These are areas throughout Discourse where we’ve added a little code for developers to hook into. You can search Discourse for these on Github, or browse for them using the (deprecated) Plugin outlet locations theme component.

For custom homepages, above-main-container is a common choice, so let’s use that.

We need to create our connector file in the correct directory:

:page_facing_up: javascripts/discourse/connectors/above-main-container/custom-homepage-connector.hbs

<CustomHomepageContent />

:point_up: and that’s all, it just needs a single line calling for your component :tada:


This document is version controlled - suggest changes on github.

47 إعجابًا

مرحباً @awesomerobot،

شكراً على الشرح. لقد جربت الخطوات التي اقترحتها، لكن after_header الذي طبقته لا يزال يظهر في صفحات تفاصيل المنشور. هل يمكنك أن توصي بكيفية إصلاح هذا ليظهر فقط في صفحتي الرئيسية؟

إعجاب واحد (1)

مرحباً @Cornelius، هل من الممكن الاطلاع على الكود الخاص بك؟

إعجابَين (2)

سيكون من الرائع إعادة كتابة هذا للاستخدام الحديث لنظام الملفات بدلاً من وضع كل شيء في علامات الرأس.

معظم هذه الأدلة القديمة للسمات قديمة بالنسبة للطريقة التي تتم بها الأمور الآن.

7 إعجابات

نعم، كان هذا قديماً جداً! لقد قمت بتحديثه ليعكس بنية السمات البعيدة ومكونات Ember الحديثة لدينا.

6 إعجابات

هذا رائع!

وهل هذا الـ getter هو نفس الشيء مثل هذا http://ember-cli-page-object.js.org/docs/v1.11.x/api/getter.html؟ أنا أعرف القليل بما يكفي لأكون خطيرًا. إذا كان هو نفسه، فسأقوم بتحرير OP لربطه.

3 إعجابات

@awesomerobot مرحباً كريس، لقد قمت بتثبيت discourse محلياً على نظامي. ما هو المسار الصحيح لإضافة هذه الملفات في نسختي المحلية من discourse. أردت إضافة مكون سمة جديد في نسختي المحلية من discourse.

إعجاب واحد (1)

ستقوم بإنشاء مكون السمة الخاص بك وتثبيته من خلال تجربة المستخدم.

تثبيت سمة أو مكون سمة

تثبيت تطبيق سطر أوامر Discourse Theme CLI للمساعدة في بناء السمات

4 إعجابات

إذا قمت ببناء صفحتي الرئيسية فوق عرض الفئة، فما زلت أحصل على المحتوى المخصص الخاص بي حتى لو انتقلت إلى /categories، وهو ليس عنوان URL الرئيسي. أريد أن أقتصر هذا على عنوان URL الجذر / فقط، والذي أعتقد أن هذا ما كان يفعله الرمز السابق، ولكني أتساءل عما إذا كان defaultHomepage() يجب أن يفعل ذلك.

3 إعجابات

discovery.${defaultHomepage()} ستطابق المسار المحدد كمسار الهبوط بواسطة إعداد top-menu. ستطابق كل من عنوان URL الجذر / والمسار المحدد، مثل /categories.

في تجربتي، هناك تعقيدان عند بناء صفحة رئيسية مخصصة بناءً على defaultHomepage():

  • المسار الذي تم بناؤه عليه لم يعد متاحًا كعرض قائمة بسيط.
  • يمكن للأعضاء تعيين صفحتهم الرئيسية الافتراضية في إعدادات واجهتهم. لذا، يحتاج المرء إما إلى تعطيل هذه الميزة أو بالفعل وجود مفهوم صفحة رئيسية يعمل على أي من مسارات القائمة العلوية.

لبناء صفحة رئيسية مخصصة فقط على عنوان URL الجذر، يمكن التحقق من router.currentURL === '/'. افتراضيًا، يطابق هذا فقط عنوان URL الجذر / وليس مسار الهبوط المحدد بواسطة إعداد القائمة العلوية. ومع ذلك، هناك منطق الآن على روابط الشريط الجانبي يهدف أيضًا إلى مطابقة عنوان URL معين بمسار. لذلك لن يعمل على روابط الشريط الجانبي افتراضيًا. لقد نشرت للتو موضوعًا حول هذا: هل يمكنني الحصول على روابط شريط جانبي لا تحل عنوان URL إلى مسار؟

في فهمي، لا توجد حاليًا طريقة افتراضية لبناء صفحة رئيسية مخصصة على عنوان URL الجذر دون استهداف مسار من القائمة العلوية أيضًا أو مواجهة مشكلات مع الشريط الجانبي. سيكون من الرائع توفير هذا الخيار.

4 إعجابات

صحيح، إنها خدعة قديمة إلى حد ما أن / و /route المقابل يمكن أن يعرضا محتوى مختلفًا. لدينا مهمة للقيام بها

  1. السماح بتعيين الصفحة الرئيسية بشكل مستقل عن إعداد top_menu
  2. إضافة قالب صفحة رئيسية جديد قائم بذاته يمكن تخصيصه دون الاستيلاء على مسار موجود

الصفحات الرئيسية المخصصة هي طلب شائع جدًا في هذه المرحلة، لذا يمكننا بالتأكيد استخدام المزيد من المرونة هنا.

6 إعجابات