# Conditional Render Hook Code (CodeBlock)

**URL:** <https://discourse.gohugo.io/t/conditional-render-hook-code-codeblock/41834>\
**Category:** support\
**Created:** [December 9, 2022, 3:45pm UTC](https://discourse.gohugo.io/t/conditional-render-hook-code-codeblock/41834 "2022-12-09T15:45:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![colin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/colin/32/3223_2.png) [@colin](https://discourse.gohugo.io/u/colin)\
**Post date:** [December 9, 2022, 3:45pm UTC](https://discourse.gohugo.io/t/conditional-render-hook-code-codeblock/41834/1 "2022-12-09T15:45:04Z")

</div>

I’m attempting to add [Prism](https://prismjs.com) syntax highlighting as an option for a theme I’m building. But how would I go about making the **layouts/\_default/\_markup/render-codeblock.html** optional?

My first thought was to enclose my custom code in the `render-codeblock.html` file within a conditional `if` e.g.:

```auto
{{ if eq .Site.Params.prism true -}}
    custom Prism code
{{ end -}}

```

My (incorrect) assumption was that, if ‘empty’, Hugo would revert to its default highlighting.

But I see now that Hugo still looks to the file for a highlighting config even if empty. How do I replicate the default Chroma configuration inside my custom `render-codeblock.html` for when Prism is disabled?

My next guess it to use a form of the following:

```auto
...
{{ else }}
{{ $result := transform.HighlightCodeBlock . }}
Inner: |{{ $result.Inner | safeHTML }}
{{ end }}

```

But some advice on whether or not that’s a good idea would be helpful.

Thanks.

---

<div class="post-metadata">

**Author:** ![ju52](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ju52/32/6749_2.png) [@ju52](https://discourse.gohugo.io/u/ju52)\
**Post date:** [December 9, 2022, 4:29pm UTC](https://discourse.gohugo.io/t/conditional-render-hook-code-codeblock/41834/2 "2022-12-09T16:29:25Z")

</div>

HI,  
I used the render-codeblock mechanics to do some renderings

ex: for MathJax

````auto
```latex
\begin{align}{}_{n}A_{m}&=\int^{\beta}_\alpha (x-\alpha)^n(\beta-x)^mdx\\&=\frac{n}{m+1}\int^{\beta}_\alpha (x-\alpha)^{n-1}(\beta-x)^{m+1}dx\\&=\frac{n}{m+1}{}_{n-1}A_{m+1} \end{align}
```

````

ànd render-codeblock-latex.html

```auto
{{ $class := .Attributes.class | default "" }}
<div class="latex {{ $class }}">
  $${{.Inner}}$$
</div>

-- add here all scripts

```

Should world with prism too

---

<div class="post-metadata">

**Author:** ![colin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/colin/32/3223_2.png) [@colin](https://discourse.gohugo.io/u/colin)\
**Post date:** [December 9, 2022, 4:39pm UTC](https://discourse.gohugo.io/t/conditional-render-hook-code-codeblock/41834/3 "2022-12-09T16:39:46Z")

</div>

Thank you @ju52, but I’m looking more at how best to use Hugo’s default highlighting when I disable my own custom **render-codeblock.html**.

**EDIT** : I edit the question to hopefully make that clearer.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 9, 2022, 5:29pm UTC](https://discourse.gohugo.io/t/conditional-render-hook-code-codeblock/41834/4 "2022-12-09T17:29:06Z")

</div>

This is identical to the default behavior:

layouts/\_default/\_markup/render-codeblock.html

```nohighlight
{{ $result := transform.HighlightCodeBlock . }}
{{ $result.Wrapped }}

```

---

<div class="post-metadata">

**Author:** ![colin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/colin/32/3223_2.png) [@colin](https://discourse.gohugo.io/u/colin)\
**Post date:** [December 9, 2022, 6:09pm UTC](https://discourse.gohugo.io/t/conditional-render-hook-code-codeblock/41834/5 "2022-12-09T18:09:04Z")

</div>

@jmooring Awesome!

🙏 A special thank you since this is the umpteenth odd time you’ve helped me solve an issue.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [December 11, 2022, 6:09pm UTC](https://discourse.gohugo.io/t/conditional-render-hook-code-codeblock/41834/6 "2022-12-11T18:09:48Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
