cchost
[ class tree: cchost ] [ index: cchost ] [ all elements ]
Prev
Advanced Topics
Next
Reference
XHTML in ccHost

XHTML in ccHost

This documenation refers to ccHost 4.5. There are serious changes coming in 5.0 Make sure to keep tabs at the ccHost Wiki

Table of Contents

Rules for Editing XHTML

The template engine used by ccHost is extremely sensitive to the strict rules of XML so consider this section a kind of Do's and Don'ts. But keep in mind these are not merely suggestions, if you don't follow these rules your pages will not display and the site will be broken until you fix these issues.

  • The Entire Document Must Be Enclosed

    Since your XHTML is going in the middle of the page you do not want any <html> or <body> tags, but the document must be enclosed in something so you might as well put a <div> tag at the very top and a </div> at the very bottom. If you object to having a "visible" tag, you can use a <tal:block> </tal:block> pair.

  • All Tags Must Be 'Closed'

    A tag like <hr> is considered to be 'open' and must be closed. To close a tag you must include a forward slash before the end: <hr/>, this goes for <br /> <img src="" />

  • All Tags Must Be Matched

    A tag like <p> must have a matching </p>. Same goes for all the table tags you've been lax about <td>, <th>, <tr> on down the line.

  • All Tags Must Be Symmetrical

    You can be not sloppy about when you close a tag, they have to been closed in the exact reverse order they opened,

    <b> <i> Hello World </b> </i>

    will break your site because the 'b' tag can't close until the 'i' tag is closed.

  • All Attributes Must Be Quoted

    <table colspan=3>

    The above line will cause an error and break your site. You must put quotes around all attributes.

    <table colspan="3">
  • All Attributes Must Have Values

    <option selected >

    The above line will cause an error and break your site. Every attribute must have a value, even it's "". The accepted way to handle the case above is:

    <option selected="selected">
  • Variables Available in XHTML

    You can individualize your pages by using variables that are recalculated every time the page is rendered. For example, if the user is logged you can personalize the page based on if they have uploaded anything to your site.

    Rather than list the possible variables here, you can employ the same method used by skin writers as described at the ccHost Wiki to print out all the variables available in your system using the ?dump_page=1 query.

    You can access these variables on your page with this syntax:

    ${variable_name}

    For example:

    <h1>Welcome to ${site-title}</h1>
    <h2>${site-description}</h2>

    To make certain blocks conditional, use the tal:condition attribute on any XHTML tag. In the following example the DIV will only appear if the system detects the presence of the 'logged_in_as' variable. If that variable is there we assume the entire 'user_*' record is available to us:

    <div tal:condition="logged_in_as" >Hello ${user_real_name}</div>

    Note that in the tal:condition attribute you do not use the ${} syntax, just the variable name.

    Alternately, you can use the not: syntax (do not forget the ':') to show something else:

    <div tal:condition="not: logged_in_as" >
       Hey you should <a href="${home-url}register">register with us!</a>
    </div>

    ${home-url} vs. ${root-url} and ${q}

    There are two variables that represent the site's URL in the XHTML templates:

    home-url includes the current virtual root and accounts for pretty URLs while root-url does neither. In both cases they including the trailing backslash.

    Typical home-url expands to something like http://myserver.com/cchost?ccm=/media/, typical root-url for the same page is http://myserver.com/cchost/.

    Always use the home-url whenever you can. The one exception is physical files on your server like images, style sheets, etc. in which case you want to start with root-url

    The variable simply called q (lower case 'q') which stands for 'question mark.' It maps to either '?' or '&' depending on whether you are configured to use pretty-urls or not. You should use it instead of the actual '?' character whenever you are building urls in templates.

    <a href="${home-url}api/query${q}tags=remix">See remixes...</a>

    Working With Dynamic Data in XHTML

    You can work with dynamic data retrieved from the ccHost system in two steps:

    1. Get the data from ccHost into a template variable using a tal:block tag.
    2. Display the template variable using XHTML tags using the tal:repeat for arrays and lists.

    Get the Dynamic Data

    For this step there are several PHP functions in ccHost for retrieving dynamic data that are defined in cc-custom.php. The official developer documenation for these functions can be found here.

    In versions previous to ccHost 4.0 this section referred to the CC_tag_query(). This still works but has been deprecated in favor cc_query_fmt() which is based on the Query/Formatter Engine


    Custom Dynamic Data

    If the functions provided in ccHost aren't doing it for you and you don't mind writing a little PHP script then it's very easy to get custom dynamic information onto one of your pages.

    Let's take a scenario where you want to display the newest members of you community on your home page. To start, create a file called mylib.php in the ccextras directory. That has the following code in it:

    1. <?
    2.  
    3. // return the newest five users...
    4. function my_get_users()
    5. {
    6. $users =& CCUsers::GetTable();
    7. $users->SetOffsetAndLimit(0,5);
    8. return $users->GetRecords('');
    9. }
    10.  
    11. ?>

    Now the function my_get_users() is available to any template in the system but you'll want to be editing home.xml (which of course is the copy in your private files directory, right??).

    Getting and displaying the data is exactly the same idea as the standard way, this time we'll check if they have an avatar and display it:

    <tal:block define="records php:my_get_users()" /> 
    
    <h2>Our newest users are:</h2>
    
    <div tal:repeat="record records">
      <div tal:condition="record/user_avatar_url">
         <img src="${record/user_avatar_url" />
      </div>
      ${record/user_real_name}
      <p>
        Check 'em out 
        <a href="${record/artist_page_url}">here</a>!
      </p>
    </div>

    Of course this is a very simple example and the PHP script can get very complicated, very quickly but it does prove the point.

    Prev Up Next
    Advanced Topics Advanced Topics Reference

    Documentation generated on Sat, 17 Nov 2007 01:03:01 +0000 by phpDocumentor 1.3.0RC4