tera: Allow block to be used multiple times

It should be possible to use a block multiple times in the template. This is possible in jinja2, because a block can be accessed through the special self variable as described for example here:

https://stackoverflow.com/questions/20929241/how-to-repeat-a-block-in-a-jinja2-template

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 10
  • Comments: 22 (11 by maintainers)

Most upvoted comments

Thanks @LunNova , I finally handle all these cases on base.html

 {%- block social_meta -%} 
  {%- if page.title -%}
    {%- set_global title = page.title -%}
  {%- elif term.name -%}
    {%- set titled_type_name = taxonomy.name | title -%}
    {%- set_global title = titled_type_name ~ " | " ~ term.name  -%}
  {%- elif taxonomy.name  -%}
    {%- set_global title = taxonomy.name | title -%}
  {%- elif section.title -%}
    {%- set_global title = section.title -%}
  {%- endif -%}
  {%- if title -%}
    {%- set_global full_title = title ~ " - " ~ config.title -%}
  {%- elif is_index == true -%}
    {%- set_global title = config.title -%}
    {%- set_global full_title = config.title -%}
  {%- endif -%}
  {%- if page.description -%}
    {%- set_global description =  page.description -%}
  {%- elif page.summary -%}
    {%- set_global description =  page.summary | spaceless | striptags -%}
  {%- elif page.content -%}
    {%- set_global description = page.content | spaceless | striptags | truncate(length=200) -%}
  {%- elif is_index -%}
    {%- set_global description = config.description -%}
  {%- elif section.content -%}
    {%- set_global description = section.content | spaceless | striptags | truncate(length=200) -%}
  {%- else -%}
    {%- set_global description = config.description -%}
  {%- endif -%}
  {%- if page.extra.image -%}
    {%- set_global image = page.extra.image -%}
  {%- elif is_index and config.extra.image -%}
    {%- set_global image = config.extra.image -%}
  {%- elif page.assets -%}
    {%- for asset in page.assets | sort -%}
      {%- if asset is matching("[.](jpg|png|jpeg)$") -%}
          {%- set_global image=get_url(path=asset) -%} 
          {%- break -%}
      {%- endif -%}
    {%- endfor -%}
  {%- endif -%}
<title>{{full_title}}</title>
  <meta property="og:title" content="{{title}}"/>
  <meta itemprop="headline" content="{{title}}"/>
  <meta property="og:description" content="{{description}}"/>
  <meta name="description" content="{{description}}"/>
<meta property="og:url" content="{{current_url | safe}}"/>
  {%- if image -%}
  <meta name="og:type" content="summary_large_image"/>
  <meta property="twitter:card" content="summary_large_image"/>
  <meta property="og:image" content="{{ image }}"/>
  <meta property="og:image:alt" content="{{ title }}"/>
  {%- else -%}
  <meta name="og:type" content="summary"/>
  {%- endif -%}
{%- endblock social_meta -%}

Probably the most common use case is for the page title as seen in the stackoverflow thread. Of course one can easily work around this, but I thought it would be nice, if I (and others) don’t have to.