Skip to content

Automatically adding "raw" tags to your code in Hubspot

Barry Grennan
This will be fairly niche. I'm mostly publishing it to impress Stephanie.

It's been a constant frustration when writing about hubl that every time you need to mention a variable ( {{ variable }} ), tag ( {% tag %} ), or any other element within a hubl delimiter, it must be manually enclosed in "raw" tags.


When my code is wrapped in raw tags you can see it like this:

{{ group.name }}
 
If I forget to wrap the code in raw tags it will show like this:
 
SEO Anseo Articles

It get's very tedious wrapping each piece of code in these because hubl is basically all we write about.

So every time we include a piece of hubl code that is intended to be visible in the article it needs to start with and end with

To work around this I've placed some filters on my blog template.

{% set modified_content = content.post_body|replace('<pre>', '<pre>{% raw %}')|replace('</pre>', '{% endraw %}</pre>') %}

{% if request.postDict.inpageEditorUI %}

{{ content.post_body }}  

{% else %}  

{{ modified_content }}

{% endif %}
 
Using this method, I can then use the <pre> tags which are built into the blog editor to add my code as usual, but now those <pre> tags automatically include raw tags like
<pre></pre>
 

Nerdy PostScript

Just in case you need to know, to be able to include and in the text of this post I also needed to create some filters for the {{ content.post_body }}    tag.

So my complete filter looks like

{% set modified_content = content.post_body|replace('<pre>', '<pre>{% raw %}')|replace('</pre>', '{% endraw %}</pre>')|replace('&lt;raw&gt;', '&#123;&#x25; raw &#x25;&#125;')|replace('&lt;endraw&gt;', '&#123;&#x25; endraw &#x25;&#125;') %}

That way when I need to talk about a opening tag I can type and when I need to talk about a closing tag, I can type <endraw>