SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    page template, js function based

    Hi,

    After searching and trying some with javascript function I didnŽt get what IŽd like. May be you can help me.

    For my basketball site IŽd like to have an infobox for a player. Because there are a lot of players IŽd like to have a template for an infobox to avoid dozens of the same tags and to simply edit the layout.

    My idea was to generate an infobox for John whose position in the field is center with a function like "infobox(John, Center)". The function generates the following tags:
    Code:
    <table>
      <tr><td>Name:</td><td>John</td></tr>
      <tr><td>Position:</td><td>Center</td></tr>
    </table>
    The final template would be a slightly more complicated, but I think I can extend the script if someone made a begin.

    I failed to make the template function based, can you help me?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,061
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Code JavaScript:
    function infobox(playerName, position)
    {
      document.write('<table>');
      document.write('<tr><td>Name:</td><td>' + playerName + '</td></tr>');
      document.write('<tr><td>Position:</td><td>' + position +'</td></tr>');
      document.write('</table>');
    }

    Now you call infobox('John','Center') (you need to use the quotes! If you need a ' in the name litteraly, use \'. For example 'John\'s house') to apply the template.

    On a sidenote, your site will not work for users who have javascript disabled. Although not many users have it disabled, it's still something to remember.

    A possible sollution to avoid this is to use some server side scripting like PHP or ASP (which also has the advantage that they can be connected to a database, so you can just use the database -possibly using a CMS- to add/remove/change players, instead of hardcoding them in the HTML each time)
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Considering this doesn't really enhance the user experience, you shouldn't use javascript for this. Even though you get a lot of repetative html markup, that's how you should do it. Gzip works very well for repetative html if you're trying to keep the filesize down.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    I suggest that you head over the the css forum. They have techniques that can make a page dramatically easier to edit and maintain.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thnx! If it is possible to do this kind of thing in php than that would be very nice. I will try somewhere else.

  6. #6
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to do Smarty-like templates in JavaScript, try JavaScript Templates (JST).

    http://code.google.com/p/trimpath/wi...criptTemplates


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •