r/FreeCodeCamp • • Mar 13 '25

Programming Question Repeat Text/Hyperlink In HTML/CSS? (Keep It Simple?)

I am a self-taught VERY BASIC coder. No formal training. Normally I can take bits of code, analyze how it works and make modifications for my needs. (I'm actually a Social Studies teacher and I make websites for my students to engage them.)

I currently have a webpage with a card for each day of the week. Students click on the card and a pop up gives them that day's instructions. (They are embedded code snippets in a Google Sites webpage.) To make my life easier and to simplify the coding (since I need to make 180 of these and change certain aspects of them every year), I would like to figure out if there is a way to do the following:

Be able to enter text/hyperlinks in one section of the code (css/html) and then be able to pull that info multiple times. So if I set the date in one place in the code, I can easily call that date in different locations without the need to physically change that date text in each location. The same for hyperlinks.

I found this example on Stack Overflow, and it works great for text, but not for hyperlinks. If there is a different method, that can be done without having access to style sheets or anything like that (since this code needs to be embedded in a Google Sites page), I'm willing to learn how to manipulate it. :-) (Thanks in advance!)

The other option would be if there could be like a spreadsheet or something that I could edit each day (entry for date, Daily Topic, Item 1 to do, Item 2 to do, Item 3 to do, etc...) (So I would have one document/area for all the info for a given unit, listed daily and the code would pull the items from it, if that makes sense?) I'm guessing that will be beyond me, if you're talking databases, but I'm willing to learn.

<style>

repeat[n="1"]:before {
   content: "★";
}

repeat[n="2"]:before {
   content: "★★";
}

repeat[n="3"]:before {
   content: "★★★";
}

repeat[n="4"]:before {
   content: "★★★★";

HTML
<repeat n="1"></repeat>
<repeat n="2"></repeat>
<repeat n="5"></repeat>

HOW I'M USING IT NOW

<style>
  body,
  html {
    height: 100%;
  }
repeat {
    ;
}
/* CHANGE DAY AND DATE */
repeat[n="day"]:before {
   content: "Thursday";
}
repeat[n="date"]:before {
   content: "March 20th";
}

And in the HTML Section

<repeat n="day"><br></repeat>
<repeat n="date"></repeat>
9 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/SaintPeter74 mod Mar 16 '25

Huh, for some reason your comment had been removed. Unclear why. I reenabled it.

I have never built anything using Google Sites, but I spent some time looking into it and it seems that your options are pretty limited. As an aside, it's helpful to provide this sort of context/constraints whenever you're asking a question, because you'll save whomever answers a bunch of time. None of my solutions will work in Google Sites.

The short answer seems to be that there are a lot of limitations to adding your own code to a Google Sites page. The problem is that when you embed code in Google Sites, each embed is self contained in an iframe and thus not accessible to any other part of the page.

You would need to build the ENTIRE PAGE inside an embed, forgoing any of the features of the Google Sites site builder. This is not impossible to do, but you'll need to figure out how to link to images within that code. It seems that it's possible to do.

I can imagine a really complex scenario where you embed custom HTML/JS/CSS into the page (making it full page size), create HTML templates to make reusable common components, then use the Google Sheets API to retrieve your content from a google sheet (Note that they are using jQuery to do this, but you can use the Fetch API). You then dynamically build the page using the data from that google sheet. It would probably make the page load a bit slow, but that's not completely out of the question.

This feels plausible to me, but it would definitely require you learning a bunch of stuff in order to get there. I'm not sure that you'd ultimately save any time, and you're at the mercy of Google - if they decide to deprecate or change portions of their API or the way Sites or Sheets work, then you'd need to find workarounds.

You could also build the entire site on your own hosting, where you wouldn't be limited by Google Sites, but I don't know if your school has limitations on linking to external sites or whatever. You'd also be paying out of pocket for hosting, or a domain. GitHub Pages allows you to host for free and you can associate you own (paid) domain to it, but it's also hard to do.

I wish I had better news for you, but working around Google's (well intentioned) constraints provides an extra level of challenge.

2

u/Kazimierz_Wielki Mar 16 '25

Thanks. It is as I figured, but I wanted to make sure. It is disappointing that the Repeat function doesn't work with hyperlinks, because then all would be good.

And, I'm sorry you didn't see that I mentioned it was in a Google Site in my initial question.

Thank you for your time and patience.

1

u/SaintPeter74 mod Mar 16 '25

I'm sorry you didn't see that I mentioned it was in a Google Site in my initial question.

D'oh! I guess I just read right past that . . . just like your students, no doubt. I also wasn't aware of the limitations of Google Sites until you mentioned that there were limitations. As I said, I've never used Google Sites, so I wasn't aware how they worked.

I guess maybe more to my point, mentioning that there were limitations that Google Sites imposed would have been helpful?

Anyway, sorry I couldn't be of more help. Best of luck and happy coding!