CiTi · Computer Coding · Year One · Unit 3
A lesson within “My First Website & Server-Side Management”
includes/vars.inc)How do I manage large CSS projects? Can CSS be dynamic?
Last lesson, shared code moved into include files. Today the shared values move too. The full site path is typed once, as pathvar, in one settings file. The CSS, includes, and images paths are built from it, and every page and include reads them from there.
Demonstrates understanding in:
Reusable Code Assets Code Type via Syntax Large Web Application Planning Dynamic CSS ManagementHands-On:
#3 Build and deliver a website from a client brief
#7 Validate and debug my own work
Professional:
#7 Treat a mistake as data, not failure
#9 Document as you go
Variables as named placeholders, the same idea as “let x = 5” in algebra. The difference between assigning a value and using it, and why order matters: a value must be assigned before any line that uses it.
Every professional project keeps its settings in one configuration file so a change happens in one place. Commenting that file so a teammate (or future you) knows what each setting controls.
Creating includes/vars.inc with WebDNA [text] variables, including it first on every page, replacing hard-coded values in header.inc, nav.inc, footer.inc, and the include lines with variables, giving each page its own title through a page-level variable, and debugging the common variable mistakes.
Students will understand that a variable stores a value under a name so it can be reused anywhere: and will build a vars.inc settings file for MySite so the site path, site name, and author live in one place, and every page shows its own title.
vars.inc with [text] variables for my paths (pathvar, cssvar, incvar, imgvar), site name, and author, and include it on the first line of every page.header.inc, nav.inc, footer.inc, and my include lines.titlevar before the header include.=, a misspelled name, a tag name used as a variable name, and stray spaces in a value.Students convert their own working site to a settings file, then run a “rebrand”: change the site name in one place and confirm it on every page. Push further: a classmate plays a client who requests a new site name and a new author credit by message; the developer makes the change in vars.inc only and replies with the live URL.
header.inc, nav.inc, and footer.inc working on every page, using full site paths (from the previous lesson)/citi/coding/2027/1/YOURINTS/mysite/ with their initials. It becomes the value of pathvar.A WebDNA text variable has two parts: one tag that sets it and a second tag that uses it. Setting prints nothing. Using prints the stored value.
=.
[text]sitenamevar=Jordan's Lego Site[/text]
The site path is the biggest win. In the last lesson students typed /citi/coding/2027/1/YOURINTS/mysite/ into every link, image, CSS link, and include line, often 20 or more times. After today it is typed once, as pathvar.
A value can use a variable that is already set. [text]cssvar=[pathvar]css/[/text] stores /citi/coding/2027/1/YOURINTS/mysite/css/. So pathvar is the base, and the CSS, includes, and images folders each get their own variable built from it. Two rules, both tested:
pathvar must be set above the lines that use it. If cssvar comes first, it stores the plain text [pathvar]css/ and the stylesheet link breaks.pathvar further down does not update cssvar. To move the site, edit the pathvar line itself.varThe class convention is lowercase names ending in var. It makes variables easy to spot in code ([cssvar] is clearly yours, [date] is clearly WebDNA’s), and it can never collide with a real WebDNA tag (see Tricky Part 4).
| Variable | Value (example) | Used in |
|---|---|---|
| [pathvar] | /citi/coding/2027/1/YOURINTS/mysite/ | Nav links; the base for the three below |
| [cssvar] | [pathvar]css/ | The stylesheet link in header.inc |
| [incvar] | [pathvar]includes/ | Every include line except vars.inc |
| [imgvar] | [pathvar]images/ | Every image |
| [sitenamevar] | Jordan’s Lego Site | <title>, header <h1>, footer, image alt text |
| [authorvar] | Jordan Diaz | Footer copyright line |
| [titlevar] | About Me | Set on each page; used in the <title> inside header.inc |
One variable per line, pathvar first. Students only ever type their own path once, on the pathvar line; the three lines under it are the same for everyone. [!]...[/!] is a WebDNA comment: the server skips it, so it never shows up, not even in View Source.
Line order matters. vars.inc comes first and is the only line that still spells out the full path: [incvar] does not exist until that file has been read. titlevar comes next, before the header include that uses it.
Every path variable already ends with a /, so it is followed directly by the rest of the path: [cssvar]style.css, not [cssvar]/style.css.
Every result below was tested on our WebDNA server. Each has a one-line experiment for the “Break It” phase: predict, test, then undo.
WebDNA reads a page top to bottom. A variable used before its [text] line prints as plain text, brackets and all: [titlevar] shows up in the browser tab. This is also why the vars.inc include line must spell out the full path: [incvar] is not set yet on that line.
Try it: move the [text]titlevar=...[/text] line below the header include. The tab shows [titlevar] | Jordan's Lego Site. Demo #1 ↗
[text]sitenamevar = Jordan's Lego Site[/text] looks fine but the space becomes part of the name, so [sitenamevar] was never set and prints as plain text. No error message appears. The rule: no spaces on either side of =.
Try it: add a space before the = on the sitenamevar line in vars.inc. Every page header shows [sitenamevar]. Demo #2 ↗
A typo like [sitnamevar] prints as plain text, exactly like any WebDNA tag the server does not recognize. Capital letters do not matter ([SiteNameVar] works), but the class convention is all lowercase so names are easy to match.
Try it: in header.inc, change [sitenamevar] to [sitnamevar]. Demo #3 ↗
A variable can take over a real tag. After [text]date=hello[/text], every [date] on that page prints hello instead of today’s date. A formatted tag breaks too: the footer’s [date format=%Y] prints helloformat=%Y]. This is why the class convention ends every variable name in var: a name like datevar can never match a WebDNA tag (tested: [datevar] and [date] both work side by side).
Try it: add [text]date=hello[/text] to the bottom of vars.inc and look at the footer. Demo #4 ↗
Everything between = and [/text] is the value, including a space or an Enter. A space at the end of pathvar is copied into cssvar, incvar, and imgvar too, so every include turns into Error: template '/citi/coding/2027/1/jdm/mysite/ includes/header.inc' not found and the page loses its styling. The space before includes in the error message is the clue.
Try it: add one space before [/text] on the pathvar line. Read the error, then remove the space. Demo #5 ↗
Variables are set fresh each time a page is built. A value set on the homepage does not exist on the About page. That is why vars.inc is included at the top of every page: a page that skips it gets Error: template '[incvar]header.inc' not found for every include. Demo #6 ↗
Inside vars.inc, order matters too. If the cssvar line sits above the pathvar line, cssvar is built while pathvar does not exist yet, so it stores the plain text [pathvar]css/. The stylesheet link becomes href="[pathvar]css/style.css" and the page loses all its styling, while images and links still work.
Try it: move the cssvar line above the pathvar line in vars.inc, then open View Source and find the stylesheet link. Demo #8 ↗
/citi/coding/... paths, usually images in the bodycopy. In Sublime, Find (Cmd/Ctrl+F) for /citi/ in each file; the only match left should be the vars.inc include line.[pathvar]css/ there means cssvar is above pathvar in vars.inc (Tricky Part 7). A short path like css/style.css means a leftover from before includes (Break It Demo #7).=. Check in that order.vars.inc. They are harmless; browsers ignore whitespace before <!DOCTYPE html>.[date] from the last lesson.<title>. A name with " inside an attribute like alt="[sitenamevar] logo" will cut the attribute short; suggest avoiding double quotes in the site name.| Time | Phase | What happens |
|---|---|---|
| 5 min | Warm-Up / Hook | “Your site just moved to a new folder. How many places would you have to fix?” Students count every appearance of their full site path across their includes and pages (walkthrough, Part A). Typical answer: 15 to 30. |
| 10 min | Direct Instruction | Set vs. use. Algebra connection (“let x = 5”). Live demo: build vars.inc on a page, starting with pathvar and then cssvar built from it; include it first, swap the CSS link to [cssvar]style.css, reload. Nothing looks different, and that is the point. Show the titlevar pattern and the browser tab changing per page. |
| 25 min | Guided Practice: Walkthrough | Students follow the walkthrough steps: create vars.inc, add it to the homepage, convert the three includes, convert the homepage, test, then convert the remaining pages and add a titlevar to each. Final check: Find /citi/ in every file. |
| 10 min | Break It | Pairs run Tricky Parts 1 through 4 on their own sites: predict, test, record, undo. Project the matching Break It demos to confirm results. Debrief the date takeover as a class; it surprises everyone. |
| 5 min | Rebrand & Exit Ticket | Students change sitenamevar in vars.inc only and confirm the new name on every page and in every browser tab. Exit ticket on the walkthrough. |
Formative: Live MySite check: vars.inc exists and is the first line of every page; the only hard-coded full path left is the vars.inc include line; every link, image, CSS, and include path uses pathvar, imgvar, cssvar, or incvar; the site name and author come from variables; every page has its own browser-tab title; no bracketed variable names appear on any page. Walkthrough Break It entries reviewed for reasoning.
Exit ticket (completion-graded): “Why must the vars.inc include line use the full path instead of [incvar]? Name one other change that now takes one edit.”
Extension: Give every page a fallback title so a forgotten titlevar never shows brackets. Put this in header.inc just above <!DOCTYPE html>: [showif [titlevar]=[raw][titlevar][/raw]][text]titlevar=Home[/text][/showif]. It checks whether [titlevar] is still plain text (never set) and, if so, sets it to Home. (Preview of [showif].)