Help with global_header

A place for users and developers of the Xataface to discuss and receive support.

Help with global_header

Postby r0n2990 » Wed Dec 19, 2007 5:52 am

Hey guys,

How does the smarty templates take information from the database?

I am tring to get in the header the year and the name of the auction but it is not showing it to me.

Code: Select all
{foreach from=$fundraiser_informations item=fundraiser_information name=fundraiser_information}
   

   

{$fundraiser_informations ->display('year')


   

{$fundraiser_informations ->display('slogan')


   

{/foreach}


I am tring to get into the table fundraiser_information and use the values year and slogan.

There will only be one entry in this table at one time so it is ok to do the for loop but I am being told that the for look is not being closed?

Anyone have a simpiler way that would be much appricated.

-Cam
r0n2990
 
Posts: 9
Joined: Thu Dec 13, 2007 5:02 am

Postby shannah » Wed Dec 19, 2007 12:30 pm

There are many ways to pass database information into smarty templates. But in all cases you must explicitly do *something* to get the data.

1. If you just need to get a single record you can use the {load_record} tag.
e.g.

Code: Select all
{load_record table="fundraiser_information" id="10" var="info"}

{$info->display('slogan')}


...


The {load_record} tag must include a "table" attribute to specify the table from which the record should be loaded, and a "var" attribute to specify the name of the variable where the resulting record will be stored. You may also pass additional parameters named after columns in the table to specify query parameters. In the above example it loaded a record from the fundraiser_information table whose "id" column = 10, and stored the record in the $info variable. Hence the $info variable would store a Dataface_Record object.

2. If you want to have a little more control over what you load, you could load the data in a controller, and the pass the data to your template.

e.g. in actions/my_action.php
Code: Select all
class actions_my_action {
    function handle(&$params){
        $res = mysql_query("select * from fundraiser_information", df_db());
        $info = array();
        while ( $row = mysql_fetch_assoc($res) ) $info[] = $row;
        @mysql_free_result($res);
        df_display( array('fundraiser_information' => & $info), 'my_template.html');
    }
}

and in templates/my_template.html:
Code: Select all
{foreach from=$fundraiser_information item=info}
   

{$info.slogan}


{/foreach}
...


Notice in this example that each record is an array rather than a Dataface_Record object. This is because we obtained the records using mysql_query(), and why we use the notation {$info.slogan} rather than {$info->display('slogan')}.

If we had used df_get_records_array() to obtain our records instead, then they would have been Dataface_Record objects.

You would access this page using the url index.php?-action=my_action


3. If already have a record accessible in your template you can use that record to obtain related records (e.g. using the getRelatedRecordObjects() method). You can also define custom fields in the delegate class that returns an array of records.... I'll save that for another tutorial...

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby r0n2990 » Thu Dec 20, 2007 5:09 pm

Thanks for the help.

I tried using the load record but it does not show the record it reads NO ACCESS.

Any suggestions?

-Cam

Edit #1
Nevermind... I added a new record to the table and then it allowed me to have it put up onto the header.

Edit #2
Disregard the last statement it seems that i have to be logged in as admin to be able to view the record within fundraiser_information.

Any way around this?
r0n2990
 
Posts: 9
Joined: Thu Dec 13, 2007 5:02 am

Postby shannah » Thu Dec 20, 2007 10:45 pm

Ah.. yes. The display() method respects permissions. So if the current user doesn't have view permission for that field/record, then it will say NO ACCESS.

The work around is that you can tell the permissions tool to ignore permissions for your template by adding the following to the beginning of the template.

Code: Select all
{php}
   $pt =& Dataface_PermissionsTool::getInstance();
   $pt->setContext($pt->PUBLIC_CONTEXT());
{/php}


The next version of Dataface allows you to instruct each individual record to ignore these restrictions, but for now, this workaround will work.

Best regards

Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 16 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved