You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Where < represents the less than or < symbol etc.
Rather than encoding your hard code and pasting it in statically, how can I dynamically encode my regular code into HTML entities so that I don't have to keep on updating/changing it as my code changes? I want to create a function that will read the contents of a file and then encode its contents so that this will update whenever the file is updated.
Encoding your code
At first I considered regex or some other form of replacement function however this would be complicated to cover all possibilities. So instead I came across the hex package:
To install the hex package add {:html_entities, "~> 0.3"} under deps to your mix.exs file and run mix deps.get.
Reading the contents of a file
Now that I can encode my code, I now need to be able to grab the code from within a given file in order to encode it. To do this I can use the File module like this:
This only works with the trailing bang ! otherwise it errors. This is because we want it to return the code to us, without the ! it will return a tuple instead.
So this function is now called where I want the code snippets to appear in the html file like this:
The function contains:
And on the browser we see:
Despite the availability of the hex package, it turns out that using File.read! I don't even need it.
Cleop
changed the title
How to display code snippets in your elixir code without it rendering
How to display HTML code snippets in your elixir code without it rendering
May 10, 2017
Cleop
removed
the
in-progress
An issue or pull request that is being worked on by the assigned person
label
May 11, 2017
HTML entities can be used to help you display code snippets without them rendering as the code they represent. For example:
Is actually written in the code as:
Where
<
represents the less than or<
symbol etc.Rather than encoding your hard code and pasting it in statically, how can I dynamically encode my regular code into HTML entities so that I don't have to keep on updating/changing it as my code changes? I want to create a function that will read the contents of a file and then encode its contents so that this will update whenever the file is updated.
Encoding your code
At first I considered regex or some other form of replacement function however this would be complicated to cover all possibilities. So instead I came across the hex package:
https://hex.pm/packages/html_entities
To install the hex package add
{:html_entities, "~> 0.3"}
under deps to yourmix.exs
file and runmix deps.get
.Reading the contents of a file
Now that I can encode my code, I now need to be able to grab the code from within a given file in order to encode it. To do this I can use the
File
module like this:File.read!("./web/templates/component/primary_button_example.html.eex")
This only works with the trailing bang
!
otherwise it errors. This is because we want it to return the code to us, without the!
it will return a tuple instead.So this function is now called where I want the code snippets to appear in the html file like this:
![](https://cloud.githubusercontent.com/assets/16775804/25897131/bd05aab4-357e-11e7-9f74-f66283eca305.png)
![](https://cloud.githubusercontent.com/assets/16775804/25897707/d3aa6bb8-3580-11e7-9808-3ca862fcf8b2.png)
![](https://cloud.githubusercontent.com/assets/16775804/25893814/45baa1d8-3571-11e7-8370-a94992dbb3ea.png)
The function contains:
And on the browser we see:
Despite the availability of the hex package, it turns out that using
File.read!
I don't even need it.😀
Relates to mindwaveventures/good-thinking-phoenix#115
The text was updated successfully, but these errors were encountered: