# Best practice for streaming a template file with replaced strings

**URL:** https://discourse.julialang.org/t/best-practice-for-streaming-a-template-file-with-replaced-strings/6469
**Category:** Data
**Tags:** question
**Created:** [October 16, 2017, 1:06pm UTC](https://discourse.julialang.org/t/best-practice-for-streaming-a-template-file-with-replaced-strings/6469 "2017-10-16T13:06:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [October 16, 2017, 1:06pm UTC](https://discourse.julialang.org/t/best-practice-for-streaming-a-template-file-with-replaced-strings/6469/1 "2017-10-16T13:06:12Z")

</div>

I have a template file with a few placeholders. I want to replace the placeholders with actual values and pipe the result forward.  
Currently I read the whole file, find all occurrences of the placeholders and replace them with their respective (pre-calculated) values, and then push that into some pipe (see example bellow).

Is there some better way of doing this (like connecting the pipe with some filter/switch in between)?

## Example

```julia
open("file.txt", "w") do o
    txt = readstring("template_file.txt")
    for (k, v) in Dict("PLACEHOLDER1" => 3, "PLACEHOLDER2" => "this")
        txt = replace(txt, k, v)
    end
    print(o, txt)
end

```

where `template_file.txt` is:

```julia-auto
apples: PLACEHOLDER1
and then: PLACEHOLDER2

```

This results in `text.txt`:

```julia-auto
apples: 3
and then: this

```

Thanks!

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 16, 2017, 2:31pm UTC](https://discourse.julialang.org/t/best-practice-for-streaming-a-template-file-with-replaced-strings/6469/2 "2017-10-16T14:31:46Z")

</div>

Perhaps you may be able to find matches with a regex that has alternation (“or”), and then replace the right one. But it may be more trouble than it is worth, and you have to think about placeholders that contain each other if that is relevant, so the order may matter.

If you have control over the placeholder format, choosing something like `<id>` may be feasible (or the zillion other syntaxes, eg `{{ id }}`), you just match the delimiters, extract `id`, and emit the output as concatenation or streaming into an `IOBuffer()`. This may be significantly faster.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [October 16, 2017, 4:08pm UTC](https://discourse.julialang.org/t/best-practice-for-streaming-a-template-file-with-replaced-strings/6469/3 "2017-10-16T16:08:39Z")

</div>

I like the Python solution to the template problem where you can use the `format` method of a string to replace fields with values in the correct format:

```python
"""
Some long string with {value} in it.
""".format(value=2.3)

```

Your Julia solution is already pretty close, but we could do better and get inspiration from the Python interface.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [October 16, 2017, 7:22pm UTC](https://discourse.julialang.org/t/best-practice-for-streaming-a-template-file-with-replaced-strings/6469/4 "2017-10-16T19:22:10Z")

</div>

I’d like that.

I guess many templates are existing files. It would therefore make sense to be able to do this kind of “formating” on streams of text. Aside from @Tamas_Papp’s inception-style placeholders (i.e. placeholders within placeholders), maybe something simple would be feasible?

---

<div class="post-metadata">

### Author: ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)
#### Post date: [October 16, 2017, 7:40pm UTC](https://discourse.julialang.org/t/best-practice-for-streaming-a-template-file-with-replaced-strings/6469/5 "2017-10-16T19:40:38Z")

</div>

I’ve used mustache before for similar purposes:

[https://github.com/jverzani/Mustache.jl](https://github.com/jverzani/Mustache.jl)

// mustache is what some javascript frameworks use to render pages ( i.e. if you’ve ever seen {{ user.name }} )

* * *

some example code that uses it:

- [https://github.com/djsegal/Julz.jl/blob/9e9760724ea1adc88f4cead8cb5edbafcc1f309a/src/functions/utils/generate\_file\_template.jl](https://github.com/djsegal/Julz.jl/blob/9e9760724ea1adc88f4cead8cb5edbafcc1f309a/src/functions/utils/generate_file_template.jl)

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [October 16, 2017, 9:05pm UTC](https://discourse.julialang.org/t/best-practice-for-streaming-a-template-file-with-replaced-strings/6469/6 "2017-10-16T21:05:00Z")

</div>

Wow !!! That’s great! I’ll use this instead. From my quick check it seems to satisfy everything we want (even IO streams).

---

<div class="post-metadata">

### Author: ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)
#### Post date: [October 17, 2017, 5:29pm UTC](https://discourse.julialang.org/t/best-practice-for-streaming-a-template-file-with-replaced-strings/6469/7 "2017-10-17T17:29:43Z")

</div>

Mustache.jl is good. You also might look at Formatting.jl, which does  
python-style formatting.

Cheers, Kevin
