Template to display title or h1 paragraph or page name

Where I’m wrong with this template?

My Goal is to display, in order of descending priority:

  • title (from frontmatter if exist)
  • first H1 header (if exist)
  • the page name in both the preceding are non present.
{{#if title}}
* title: [[{{name}}|{{title}}]]
 {{else}}
{{#if header}}
* h1: [[{{name}}]]
{{else}}
* page: [[{{name}}]]
{{/if}}
{{/if}}

Hi, do you have the code in ```template ``` block?

This mostly works for me:

```template
{{#each {page}}}
{{#if title}}
* title: [[{{name}}|{{title}}]]
{{else}}
{{#if header}}
* h1: [[{{name}}]]
{{else}}
* page: [[{{name}}]]
{{/if}}
{{/if}}
{{/each}}
```

(Sorry for not using code formatting but it’s not usable since the template starts with three `.

The code above prints "page: " followed by names of my pages, but for those that I add title: some different string here into the frontmatter, the printed line is “title: some different string here”.

What I don’t know is how to get the first H1. I see a need for some additional logic to print only the 1st H1 in case more of them are in the page. Maybe using Templates might help here?