Skip to content

Commit b0fd24e

Browse files
committed
sponsors: dynamically render the page
1 parent 03c6daa commit b0fd24e

File tree

4 files changed

+97
-32
lines changed

4 files changed

+97
-32
lines changed

src/data/sponsors.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
3+
# Content of the /sponsors page.
4+
#
5+
# The name of the sponsor is defined here and left untranslated in the page.
6+
# The other parts of the page are derived from the sponsor's ID:
7+
# - The logo is located in static/images/sponsor-logos/<id>.png
8+
# - The alt of the image is the Fluent translation "sponsors-<id>-alt"
9+
# - The description paragraph is the Fluent translation "sponsors-<id>"
10+
11+
- id: aws
12+
name: AWS
13+
14+
- id: microsoft
15+
name: Microsoft Azure
16+
17+
- id: mozilla
18+
name: Mozilla

src/main.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod i18n;
3030
mod production;
3131
mod redirect;
3232
mod rust_version;
33+
mod sponsors;
3334
mod teams;
3435

3536
use production::User;
@@ -228,6 +229,16 @@ fn production_locale(locale: SupportedLocale) -> Template {
228229
render_production(locale.0)
229230
}
230231

232+
#[get("/sponsors")]
233+
fn sponsors() -> Template {
234+
render_sponsors(ENGLISH.into())
235+
}
236+
237+
#[get("/<locale>/sponsors", rank = 10)]
238+
fn sponsors_locale(locale: SupportedLocale) -> Template {
239+
render_sponsors(locale.0)
240+
}
241+
231242
#[get("/<category>/<subject>", rank = 4)]
232243
fn subject(category: Category, subject: String) -> Template {
233244
render_subject(category, subject, ENGLISH.into())
@@ -395,6 +406,20 @@ fn render_production(lang: String) -> Template {
395406
Template::render(page, &context)
396407
}
397408

409+
fn render_sponsors(lang: String) -> Template {
410+
println!("foo");
411+
let page = "sponsors/index".to_string();
412+
let context = Context::new(
413+
page.clone(),
414+
"sponsors-page-title",
415+
false,
416+
sponsors::render_data(&lang),
417+
lang,
418+
);
419+
420+
Template::render(page, &context)
421+
}
422+
398423
fn render_governance(lang: String) -> Result<Template, Status> {
399424
match teams::index_data() {
400425
Ok(data) => {
@@ -468,6 +493,7 @@ fn main() {
468493
governance,
469494
team,
470495
production,
496+
sponsors,
471497
subject,
472498
files,
473499
favicon,
@@ -478,6 +504,7 @@ fn main() {
478504
governance_locale,
479505
team_locale,
480506
production_locale,
507+
sponsors_locale,
481508
subject_locale,
482509
components_locale,
483510
redirect,

src/sponsors.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crate::i18n::I18NHelper;
2+
use std::error::Error;
3+
4+
static SPONSORS_YML_PATH: &str = "src/data/sponsors.yml";
5+
6+
lazy_static! {
7+
static ref SPONSORS: Vec<Sponsor> = load_sponsors(SPONSORS_YML_PATH).unwrap();
8+
}
9+
10+
#[derive(Deserialize)]
11+
struct Sponsor {
12+
id: String,
13+
name: String,
14+
}
15+
16+
fn load_sponsors(path: &str) -> Result<Vec<Sponsor>, Box<dyn Error>> {
17+
Ok(serde_yaml::from_str(&std::fs::read_to_string(path)?)?)
18+
}
19+
20+
#[derive(Serialize)]
21+
pub(crate) struct RenderSponsor {
22+
name: &'static str,
23+
is_not_first: bool,
24+
logo_path: String,
25+
logo_alt_i18n: String,
26+
description_i18n: String,
27+
}
28+
29+
pub(crate) fn render_data(lang: &str) -> Vec<RenderSponsor> {
30+
let i18n = I18NHelper::new();
31+
32+
let sponsors = SPONSORS
33+
.iter()
34+
.enumerate()
35+
.map(|(i, s)| RenderSponsor {
36+
name: &s.name,
37+
is_not_first: i != 0,
38+
logo_path: format!("/static/images/sponsor-logos/{}.png", s.id),
39+
logo_alt_i18n: i18n.lookup(lang, &format!("sponsors-{}-alt", s.id), None),
40+
description_i18n: i18n.lookup(lang, &format!("sponsors-{}", s.id), None),
41+
})
42+
.collect::<Vec<_>>();
43+
sponsors
44+
}

templates/sponsors/index.hbs

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,22 @@
1717
{{fluent "sponsors-caveat"}}
1818
</p>
1919

20+
{{#each data as |sponsor|}}
21+
{{#if sponsor.is_not_first}}
22+
<hr size="1" noshade>
23+
{{/if}}
2024
<div class="flex-none flex-l mt5-l">
2125
<div class="w-100 w-30-l tc">
22-
<img alt="{{fluent "sponsors-microsoft-alt"}}" src="/static/images/sponsor-logos/microsoft.png" />
26+
<img alt="{{sponsor.logo_alt_i18n}}" src="{{sponsor.logo_path}}" />
2327
</div>
2428
<div class="w-100 w-70-l">
25-
<h3>Microsoft Azure</h3>
29+
<h3>{{sponsor.name}}</h3>
2630
<p>
27-
{{fluent "sponsors-microsoft"}}
31+
{{sponsor.description_i18n}}
2832
</p>
2933
</div>
3034
</div>
31-
32-
<hr noshade size="1"/>
33-
34-
<div class="flex-none flex-l mt5-l">
35-
<div class="w-100 w-30-l tc">
36-
<img alt="{{fluent "sponsors-aws-alt"}}" src="/static/images/sponsor-logos/aws.png" />
37-
</div>
38-
<div class="w-100 w-70-l">
39-
<h3>AWS</h3>
40-
<p>
41-
{{fluent "sponsors-aws"}}
42-
</p>
43-
</div>
44-
</div>
45-
46-
<hr noshade size="1"/>
47-
48-
<div class="flex-none flex-l mt5-l">
49-
<div class="w-100 w-30-l tc">
50-
<img alt="{{fluent "sponsors-mozilla-alt"}}" src="/static/images/sponsor-logos/mozilla.png" />
51-
</div>
52-
<div class="w-100 w-70-l">
53-
<h3>Mozilla</h3>
54-
<p>
55-
{{fluent "sponsors-mozilla"}}
56-
</p>
57-
</div>
58-
</div>
59-
35+
{{/each}}
6036
</div>
6137
</section>
6238

0 commit comments

Comments
 (0)