treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Dynamic Content

  • I'm trying to figure out a way not to use wordpress for this because of the bulk files.

    Is there possibly a way to create page titles and body ID's dynamically as I create links in the navigation?

    example:

    As I create:
    <li><a href="index.php">Home</a></li>

    I get:
    <title>John Doe</title>
    <body id="home">

    or

    As I create:
    <li><a href="/news/index.php">News</a></li>

    I get:
    <title>John Doe | News</title>
    <body id="news">


    Any ideas?

  • Not sure if it will work but can't you use $_SERVER['REQUEST_URI']; which should return everything after index, so if in the url you have www.site.com it will have a value of / or if the url is www.site.com/news/ it would return /news/ etc.
    then do something like this,,,

    $urlValue = $_SERVER['REQUEST_URI'];
    if($urlValue == "/") {
    $titleValue = "";
    } else {
    $urlValue = ltrim($urlValue = "/");
    $urlValue = rtrim($urlValue = "/");
    $titleValue = " | " . $urlValue;
    }


    //////Then in the Title Tag
    <?php echo "John Doe" . $titleValue ?>


    I can't see why it wouldn't work but i havn't tested it and it's probably not the best way to go about it....
  • I want to say Chris made a screencast which showcased this but I can't remember. Your code somewhat works but if I have a subdomain the title displays:

    John Doe | /
  • Fixed it by changing out the = and adding commas for $urlValue.
    $urlValue = $_SERVER['REQUEST_URI'];
    if($urlValue == "/") {
    $titleValue = "";
    } else {
    $urlValue = ltrim($urlValue, "/");
    $urlValue = rtrim($urlValue, "/");
    $titleValue = " | " . $urlValue;
    }


    Another question is how to get the title to be capitalized?
  • For anyone wanting to know:
    <?php
    $urlValue = $_SERVER['REQUEST_URI'];
    if($urlValue == "/") {
    $titleValue = "";
    } else {
    $urlValue = Ltrim($urlValue, "/");
    $urlValue = Rtrim($urlValue, "/");
    $titleValue = " | " . ucfirst($urlValue);
    }
    ?>


    Title
    <title><?php echo "John Doe" . $titleValue ?></title>


    Thanks @chrisbull for getting me started
  • whats adding the dynamic body ID? I've been using
    <body id="<?= basename($_SERVER['PHP_SELF'], ".php")?>">
    to make the filename the body id.
  • @whiteinkdesign i'm confused by your response
  • I figured it out, had a blonde moment.
  • @whiteinkdesign Oh, sorry. I thought you were questioning my code, not asking a question yourself.