If you’re talking about JavaScript code, then yes. Template literals allow you to add substitutions directly in JavaScript string literals when using back ticks (`
). But now you’re writing HTML in JavaScript which can get confusing.
Given:
it sounded like you wanted to keep things simple for people who don’t know HTML very well. And if they wanted to edit an <img>
tag, having that inside the JavaScript code could make that difficult.
Now if this “someone” is only interested in the variables and nothing else, then it doesn’t really matter. You can take any number of approaches to convert the variables into image paths. I think that’s up to you and whatever you feel comfortable with.
I, personally, like keeping everything together to help prevent surprises. For example, upon seeing
<div id="content"></div>
I’m immediately going to think, “Oh nice, an empty div. How uninteresting!” Little do I know that div is going to get populated with image elements after some script later down the road eventually runs.
With my previous example there’s image tags where there’s going to be image tags, and with a little guessing you can probably equate data-src
attributes to src
attributes thanks to a little hidden magic somewhere, but at least you know the images are there.
Once you add code, you can select it and click on the code icon in the editor:
You can also format code manually by using a single back tick for inline code `such as this`
. Or for block code indenting by 4 spaces
such
as
this
For block code, you can also use 3 back ticks along with an optional language to help with color coding. If you don’t specify a language one will be inferred.
```
like
this
```
or
```javascript
like
this
```
The code icon will default to 4 spaces for multiline code, but if you want more control over formatting you might want to force it with 3 back ticks.
… and yes, having the code as text in posts is better than screenshots