Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example TF file is not rendered in index.md with custom template #191

Closed
arxeiss opened this issue Oct 19, 2022 · 4 comments · Fixed by #300
Closed

Example TF file is not rendered in index.md with custom template #191

arxeiss opened this issue Oct 19, 2022 · 4 comments · Fixed by #300

Comments

@arxeiss
Copy link

arxeiss commented Oct 19, 2022

I have file examples/provider/provider.tf and I run tfplugindocs generate. It works great and in file docs/index.md I see the content of provider.tf file.

However, when I create custom index template in file templates/index.md.tmpl with following content, it does not work. I see just path to the file instead of content of the file.

Just to be clear, content of my template is the same as the default one here https://github.com/hashicorp/terraform-plugin-docs/blob/main/internal/provider/template.go#L246 so it is not issue with my template. Especially because it works well for resources.md.tmpl and data-sources.md.tmpl

Source codes

---
page_title: "{{.ProviderShortName}} Provider"
subcategory: ""
description: |-
{{ .Description | plainmarkdown | trimspace | prefixlines "  " }}
---

# {{.ProviderShortName}} Provider

{{ .Description | trimspace }}

{{ if .HasExample -}}
## Example Usage

{{ printf "{{tffile %q}}" .ExampleFile }}
{{- end }}

{{ .SchemaMarkdown | trimspace }}
<!-- ... not printing everything, affected part -->

## Example Usage

{{tffile "examples/provider/provider.tf"}}

<!-- ... continue with schema -->
@jacobbednarz
Copy link
Contributor

using the latest version, a custom template is working fine for us.

you might need to incrementally add the parts and see what is not working. we don't use .HasExample in the index template for instance.

@arxeiss
Copy link
Author

arxeiss commented Oct 20, 2022

I cloned your repo and I tried it out. I did make docs and it worked well without issues. No changes to docs.
Then I modified index.md.tmpl like this:

-{{ tffile "examples/provider/provider.tf" }}
+{{ printf "{{tffile %q}}" .ExampleFile }}

and in generated file docs/index.md, instead of content of provider.tf I see {{tffile "examples/provider/provider.tf"}}.

So this code {{ printf "{{tffile %q}}" .ExampleFile }} works when it is in resources.md.tmpl but not in index.md.tmpl. And here comes my confusion, because if you check source code of this tool, the mentioned code is in default templates for Resource but also Provider. https://github.com/hashicorp/terraform-plugin-docs/blob/main/internal/provider/template.go#L261

So when I copied default ones, it wasn't working for me.

@antoineco
Copy link

antoineco commented Oct 17, 2023

The reason why it doesn't work is because of the level of indirection caused by the usage of printf:

{{ printf "{{tffile %q}}" .ExampleFile }}

The result of this function is in fact expected:

  1. the text {{tffile (literally)
  2. followed by the value of .ExampleFile in quotes
  3. followed by }} (also literally)

There is no need to use printf inside template files:

-{{ printf "{{tffile %q}}" .ExampleFile }}
+{{ tffile .ExampleFile }}

Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants