Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Loops for templating #251

Closed
aminya opened this issue Oct 5, 2019 · 3 comments
Closed

Using Loops for templating #251

aminya opened this issue Oct 5, 2019 · 3 comments

Comments

@aminya
Copy link

aminya commented Oct 5, 2019

Is there a way to use loops to generate multiple html parts?
For example:

<div class="col-md-2">
	<div class="myClass">
		<img src="images/{{image i}}.png" alt="" class="myImages">
		<h3 class="name">{{name i}}</h3>
		<p>{{text i}}</p>
	</div>
</div>

in which we can fill in {{image i}}, {{name i}}, and {{text i}}, by giving an structure or an array of strings to a loop.
If i is for example 10, it should create 10 of these blocks. Or it can detect i based on the size of input struct/array.

@aminya
Copy link
Author

aminya commented Oct 5, 2019

What I'm thinking, is that it might be easier to use Julia to do this.

mutable struct person
    name
    image
    description
end

peopleNumber = 2; # Change this if number of people changes

people = Array{person}(undef, peopleNumber)

# Change this by inserting name, image name, and description
people[1] = person("John", "John.jpg", "John description")
people[2] = person("Matt", "Matt.png", "Matt description")




for n = 1:peopleNumber
    print(
    "@def name$n = $(people[n].name)\n",
    "@def image$n = images/people/$(people[n].image)\n",
    "@def description$n = $(people[n].description)\n" )
end

This above code generates the markdown file needed, but I am not sure how to link it to the html file!

@tlienart
Copy link
Owner

tlienart commented Oct 7, 2019

Hello @aminya!

Ok so there's kind of two sides to your question (my interpretation):

first side (reasonable?)

Is there a for loop in JuDoc's templating --> No, not yet, but I think it could be supported, we'd have to be careful that it doesn't do infinite stuff but something like

{{for (i, e) in enumerate(collection)}}
...
{{end}}

where collection is defined on the page like `@def collection = ["a", "b", "c"].

I've been meaning to work and improve JuDoc's templating capacity but so far I've kind of been stuck with fixing other things 😄

second side (a bit wacky?)

This may actually be what you want 😋, you can use the code-evaluation feature of JuDoc to evaluate Julia code that itself generates JuDoc markdown that can then be processed (!); this is what the textoutput command does.

This may be a bit what you were thinking in your second post. Consider this example (simpler than yours but it should be straightforward to extend to your case)

In src/index.md:

```julia:./ex
#hideall
for name in ("Shinzo", "Donald", "Angela", "Christine")
    println("""
    @@card
    ### $name
    ![]("$(lowercase(name)).jpg")
    @@
    """)
end
```

\textoutput{./ex}

Notes:

  • hideall will hide the code block from showing in your HTML
  • \textoutput will read what was generated by the code block and interpret it as JuDoc markdown

This generates html the core part of which is:

<div class="card"><h3 id="shinzo"><a href="/index.html#shinzo">Shinzo</a></h3>  <img src="shinzo.jpg" alt="" /></div>
<div class="card"><h3 id="donald"><a href="/index.html#donald">Donald</a></h3>  <img src="donald.jpg" alt="" /></div>
<div class="card"><h3 id="angela"><a href="/index.html#angela">Angela</a></h3>  <img src="angela.jpg" alt="" /></div>
<div class="card"><h3 id="christine"><a href="/index.html#christine">Christine</a></h3>  <img src="christine.jpg" alt="" /></div>

What do you think? Let me know if you manage to get this to work

PS: please note that a rather big PR to improve general code evaluation etc (and fix a few issues with paths) is about to be merged soon; syntax stays the same but you might want to keep an eye out. ==> now merged

@tlienart
Copy link
Owner

tlienart commented Oct 9, 2019

@aminya I'll close this for now (I've marked the issue in the todo list to add in the docs) but feel free to reopen or open another issue if this didn't work or you got stuck somewhere 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants