Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

can I replace a var with an array in html?

I have a ul in my html which is a menu. Some of the list items are brands, and every time I add a new brand I:

  1. add a new page

  2. need to add that brand to the menu that appears on every page.

What is the most efficient way to do this? At the moment I have created a bit of a mess where when I add a new brand I have to add the li item to each html page....

I thought maybe if I could list the brands as an array I could just push more items into the list? But I can't find a tutorial that tells me how to do it, does this mean arrays are just for js?

2 Answers

The easiest way would be to learn PHP. Unfortunately, there is no easy way of making changes to an entire Static html "template" without having to make the changes on all necessary pages. There are other libraries out there like Jade that can do this for you but I would strongly suggest php as it would be a native language rather than a preprocessor library.

Hopefully this helps.

Thanks, it's always good to know when you should stop trying to hack with one technique and just learn something else that fits properly.

This is quite simple to achieve using a PHP array.

You could save the following code to a .php file for example "brandslist.php".

<?php 
$brands = array("Team Treehouse", "Adobe", "Mozilla", "Google");    /* Create array $brands containing each brand */
?>

<ul>
    <?php
    foreach($brands as $brand) {                                    /* Loop through each value of $brands array as $brand */
        echo "<li>" . $brand . "</li>";                             /* Echo each value in $brands surrounded with html <li> tags */
    }
    ?>
</ul>

and then include where you need it on each page for example:

<html>
<head></head>
<body>
    <span> This is a list of Brands: </span></br>
    <?php include('./brandslist.php'); ?>
</body>
<html>

The files that you include the php file in will need to be .php files instead of .html or.htm for this to work (All you need to do is change the file type as HTML can be used as normal within a .php file)

By doing this whenever you need to update your brands you would only need to add the brand to the array in brandslist.php and it will be automatically included in your list on every page that includes the brandslist.php file.

This is covered quite early on in the PHP Development track here on Team Treehouse, it is well worth taking a look at the PHP track if you are looking to move away from static web design.

https://teamtreehouse.com/tracks/php-development