What is the difference between raw.hbs handlerbar files and only .hbs handlerbar files?

AI-generated summary

In a Discourse development discussion, users explore the difference between .hbs and .raw.hbs (or .hbr) Handlebars templates. net_deamon notes that some plugin outlets (like topic-list-after-title) use {{raw-plugin-outlet}}, requiring .raw.hbs files, while others use {{plugin-outlet}} with standard .hbs files. sam clarifies that raw templates are a performance optimization—they lack full Ember lifecycle features like setupComponent, making them faster but less flexible. spirobel asks about alternatives to setupComponent and computed properties in raw templates; they later find a workaround by reopening models to add computed properties accessible in .raw.hbs. merefield questions whether raw templates can be backed by JavaScript components like regular ones. They discover that some components (e.g., topic-list-item) use .hbs templates that render .hbr templates via JavaScript, suggesting raw templates don’t directly support JS unless manually bridged. markvanlan recommends using helpers for JS logic in raw templates, citing an example from the discourse-category-experts plugin. merefield reports switching some .hbr to .hbs to enable closure actions for passing data up the component tree, despite performance trade-offs. markvanlan and merefield discuss whether helpers can handle upward data flow, with merefield noting the need to persist computed image properties from leaf components to grandparent for styling. In summary, raw templates offer speed but limited interactivity; JS logic must be added via helpers or model extensions, or avoided by using full .hbs components when complex data flow is needed.

I noticied that few plugin outlet like topic-list-after-title require raw.hbs file
while other require only hbs file.

I did search in the handlebars documentation, but I could see anything as raw hbs file.

I checked in the discourse code, plugin outlet like topic-list-after-title are defined as

{{raw-plugin-outlet name="topic-list-after-title"}}

while other plugins are defined as

{{plugin-outlet name="composer-fields-below" args=(hash model=model)}}

Q1. Is there any reason why some plugin plugin are defined in above way ( raw-plugin-outlet) ?

Also I noticied that in the raw.hbs file, the setupcomponent is not called as explained in this link

Q2. Is it so? I had also asked similar question regarding this.

Anch’io sarei interessato a questo. Hai già trovato la risposta? :grinning:

I template raw sono un’ottimizzazione delle prestazioni. Sono presenti solo alcune funzionalità parziali delle view di Ember; la rimozione delle funzionalità è ciò che le rende più veloci.

Pertanto, utilizziamo qui un modello diverso per l’estendibilità. Non abbiamo l’intero ciclo di vita di Ember.

Esiste qualcosa di simile a setupComponent(args, component) anche per gli outlet raw? E le proprietĂ  calcolate? Vorrei eseguire alcuni calcoli basati sui dati nel contesto. Come posso procedere? Non sono nemmeno sicuro di aver chiamato correttamente il mio file .js.es6 di accompagnamento. Dovrei chiamarlo .raw.js.es6?

SetUpComponent not being called for topic list tags plugin outlet - #3 by net_deamon
Ok, ho trovato la soluzione: riaprire il modello Topic e creare lĂŹ una proprietĂ  calcolata:

https://github.com/paviliondev/discourse-events/blob/master/assets/javascripts/discourse/initializers/event-edits.js.es6#L74
Successivamente può essere utilizzata nel template raw:
https://github.com/paviliondev/discourse-events/blob/master/assets/javascripts/discourse/connectors/topic-list-after-title/topic-list-event-rsvp.raw.hbs#L5

Ho la stessa domanda.

È possibile associare a un template raw il corrispondente JavaScript, come avviene per i Componenti normali, all’interno di un Componente di Tema?

Ho un caso d’uso piuttosto complesso e devo gestire le azioni per passare argomenti indietro lungo la catena dei componenti, partendo da un template profondamente annidato all’interno dell’elenco dei topic.

Ho notato che ci sono diversi file .hbr con apparentemente corrispondenti elementi Componente JavaScript nel codice sorgente di Discourse, ma ho osservato qualcosa di strano, ad esempio:

merefield@development:~/discourse$ find . -type f -name "flat-button.*"
./app/assets/javascripts/discourse/app/templates/flat-button.hbr
./app/assets/javascripts/discourse/app/templates/components/flat-button.hbs
./app/assets/javascripts/discourse/app/components/flat-button.js
merefield@development:~/discourse$ find . -type f -name "topic-list-item.*"
./app/assets/javascripts/discourse/app/templates/mobile/list/topic-list-item.hbr
./app/assets/javascripts/discourse/app/templates/components/topic-list-item.hbs
./app/assets/javascripts/discourse/app/templates/list/topic-list-item.hbr
./app/assets/javascripts/discourse/app/components/topic-list-item.js
merefield@development:~/discourse$ find . -type f -name "topic-post-badges.*"
./app/assets/javascripts/discourse/app/templates/components/topic-post-badges.hbs
./app/assets/javascripts/discourse/app/templates/topic-post-badges.hbr
./app/assets/javascripts/discourse/app/components/topic-post-badges.js

Sembra quindi esserci diverse istanze in cui esistono varianti .hbr e .hbs di uno stesso Componente?

Ah, almeno con topic-list-item vedo che lo switch viene fatto qui:

Contenuto del file hbs:

{{topicListItemContents}}

Javascript di supporto:

  @observes("topic.pinned")
  renderTopicListItem() {
    const template = findRawTemplate("list/topic-list-item");
    if (template) {
      this.set(
        "topicListItemContents",
        template(this, RUNTIME_OPTIONS).htmlSafe()
      );
      schedule("afterRender", () => {
        if (this.selected && this.selected.includes(this.topic)) {
          this.element.querySelector("input.bulk-select").checked = true;
        }
      });
    }
  },

Subdolo!

Quindi questo suggerisce che i file hbs non hanno file javascript sottostanti, a meno che non siano supportati da questo tipo di soluzione alternativa?

Sì, credo che sia così. Se esiste un altro modo, non l’ho mai visto!

Quell’observer non è ideale, dato che stiamo cercando di allontanarcene mentre continuiamo a migliorare Ember. Penso che un approccio migliore sia creare un helper e inserire lì il tuo JS. Ecco un esempio
https://github.com/discourse/discourse-category-experts/blob/main/assets/javascripts/discourse/connectors/topic-list-after-title/category-experts-indicator.hbr

In alcuni lavori recenti, ho avuto bisogno che parte dell’albero dei template potesse comunicare dati da un template foglia verso l’alto utilizzando le closure actions, quindi ho convertito alcuni file .hbr in .hbs per supportare questa funzionalità.

Il lavoro è sperimentale e so che avrà un impatto sulle prestazioni, ma dopo aver iterato più volte sul design non sono riuscito a trovare un’alternativa per farlo mantenendo tutto “all’interno del framework”.

Non puoi passare funzioni a un helper e chiamarle lĂŹ? Non credo di averlo mai provato, ma immagino sia possibile farlo.

Nello specifico, sto determinando le proprietà di un’immagine in un componente foglia, per poi memorizzarle come proprietà del componente genitore per influenzare lo stile che deve persistere oltre il rendering corrente dell’elenco. I dati devono assolutamente essere passati verso l’alto. Se un helper può raggiungere questo obiettivo, sembra un’ottima opzione nel caso in cui mi trovassi in difficoltà con l’approccio attuale, grazie!