CMS Development

peesen
Mitwirkender/Mitwirkende

Get content of Blog-Article rich-text module in custom related blog posts

lösung

Hi!

 

I have created a custom related blog posts section in my HubL-Template:

 

  {% macro blog_post_formatter(post) %}
  <div class="related-post">
    <div class="related-post-inner">
      <div class="image">
        {% if post.featured_image %}
          <img src="{{ post.featured_image }}" alt="{{ post.featured_image_alt_text|escape }}">
        {% endif %}
      </div>
      <div class="content">
        <h4>{{ post.name }}</h4>
        <p class="intro-text">{{widget_data.module_1587387572139353.body.default}}</p>
        <a href="{{ post.absolute_url }}">Mehr lesen</a>
      </div>
    </div>
  </div>
  {% endmacro %}

  <div class="related-posts-carousel">
    {% related_blog_posts limit=3, post_formatter="blog_post_formatter" %}  
  </div>

This works like a charm but there is a problem i cant solve. All blog posts have an additional rich-text module (module_1587387572139353). I need to separate the intro text from the blog content because of design reasons. Now i need to access this rich text field in my custom related posts section and create the output of the field inside the p.intro-text field. As you can see i tried different solutions but nothing is working.

 

The related blog posts section is inside my Hubl Blog-Post theme at the bottom of the current blog post.

 

Thank you so much for your help and best regards

0 Upvotes
1 Akzeptierte Lösung
BenSBM
Lösung
Mitwirkender/Mitwirkende | Elite Partner
Mitwirkender/Mitwirkende | Elite Partner

Get content of Blog-Article rich-text module in custom related blog posts

lösung

Hi @peesen 

 

I did some quick testing with your rich text module. Using your rich text module as follows:

 

{% module_block module "module_1587387572139353" module_id=1155639, overrideable=True, path='@hubspot/rich_text', per_widget_wrapper_html='', widget_name='Rich Text', wrapping_html='', label='Intro-Text', export_to_template_context=True %}
{% module_attribute "html" %}<p>Default-Text</p>{% end_module_attribute %}
{% end_module_block %}

Outputting the field's HTML to the related posts macro:

 

{{post.widgets.module_1587387572139353.body.html}}

And the following for on the post itself:

 

{{ widget_data.module_1587387572139353.html }}

The output of this field will be HTML, including the tags that are present in the rich text module. Based on your module's default text, this will have a <p> tag. If you don't want that, you could either edit the HTML of the module in the post to have no HTML tags, you could change the module to a regular text field instead of rich text, or you could use the striptags filter to remove the HTML from the macro output:

 

{{ post.widgets.module_1587387572139353.body.html|striptags }}

Depending on what you intend the final output to contain and look like, I might recommend changing to a regular text field, or updating the macro to remove the wrapping <p> tag around the module output.

Lösung in ursprünglichem Beitrag anzeigen

6 Antworten
BenSBM
Mitwirkender/Mitwirkende | Elite Partner
Mitwirkender/Mitwirkende | Elite Partner

Get content of Blog-Article rich-text module in custom related blog posts

lösung

Hi @peesen 

 

I think this should just require one minor udate to work. Calling the rich text module in the post formatter will need to change slightly, I believe the following should work:

 

 

{{post.widgets.module_1587387572139353.html}}

The changes here include putting "post." at the front, changing "widget_data" to "widgets", and "default" to "html". Updating to the above snippet should work for you.

 

0 Upvotes
peesen
Mitwirkender/Mitwirkende

Get content of Blog-Article rich-text module in custom related blog posts

lösung

Hi BenSBM

 

Tank you so much for your help!

 

I tried yous snippet but there is still no output. Maybe you need to have a look at my module. I created it by creating a HubL template from a drag and drop template

 

{% module_block module "module_1587387572139353" module_id=1155639, overrideable=True, path='@hubspot/rich_text', per_widget_wrapper_html='', widget_name='Rich Text', wrapping_html='', label='Intro-Text' %}
{% module_attribute "html" %}<p>Default-Text</p>{% end_module_attribute %}
{% end_module_block %}

 

Do you have an idea how i can get the data out of this?

 

Thank you and best regards

Pascal

 

0 Upvotes
BenSBM
Mitwirkender/Mitwirkende | Elite Partner
Mitwirkender/Mitwirkende | Elite Partner

Get content of Blog-Article rich-text module in custom related blog posts

lösung

Based on your module code snippet, you will need to add the "export_to_template_context" attribute to the module:

 

{% module_block module "module_1587387572139353" module_id=1155639, overrideable=True, path='@hubspot/rich_text', per_widget_wrapper_html='', widget_name='Rich Text', wrapping_html='', label='Intro-Text', export_to_template_context=True %}
{% module_attribute "html" %}<p>Default-Text</p>{% end_module_attribute %}
{% end_module_block %}

This will allow the module to be referenced elsewhere, but will also affect the regular blog post output as well, and would require updating the post template to be able to output the module content:

 

{{ widget_data.module_1587387572139353.value }}

 

0 Upvotes
peesen
Mitwirkender/Mitwirkende

Get content of Blog-Article rich-text module in custom related blog posts

lösung

Thank you again.

 

The output is still not working in the macro and now the field itself is not generating an html-output.

 

Should i generate the module in a different way and not as a module block?

0 Upvotes
BenSBM
Lösung
Mitwirkender/Mitwirkende | Elite Partner
Mitwirkender/Mitwirkende | Elite Partner

Get content of Blog-Article rich-text module in custom related blog posts

lösung

Hi @peesen 

 

I did some quick testing with your rich text module. Using your rich text module as follows:

 

{% module_block module "module_1587387572139353" module_id=1155639, overrideable=True, path='@hubspot/rich_text', per_widget_wrapper_html='', widget_name='Rich Text', wrapping_html='', label='Intro-Text', export_to_template_context=True %}
{% module_attribute "html" %}<p>Default-Text</p>{% end_module_attribute %}
{% end_module_block %}

Outputting the field's HTML to the related posts macro:

 

{{post.widgets.module_1587387572139353.body.html}}

And the following for on the post itself:

 

{{ widget_data.module_1587387572139353.html }}

The output of this field will be HTML, including the tags that are present in the rich text module. Based on your module's default text, this will have a <p> tag. If you don't want that, you could either edit the HTML of the module in the post to have no HTML tags, you could change the module to a regular text field instead of rich text, or you could use the striptags filter to remove the HTML from the macro output:

 

{{ post.widgets.module_1587387572139353.body.html|striptags }}

Depending on what you intend the final output to contain and look like, I might recommend changing to a regular text field, or updating the macro to remove the wrapping <p> tag around the module output.

peesen
Mitwirkender/Mitwirkende

Get content of Blog-Article rich-text module in custom related blog posts

lösung

This is great! Thank you so much 🙂

0 Upvotes