Showing posts with label slots. Show all posts
Showing posts with label slots. Show all posts

Wednesday, November 12, 2008

Adding some text when no block/slot exists

Sometimes you need to add some text to a certain template, but Xataface doesn't have the specific block or slot to use. For example, say you wanted to include some text underneath the password field in the login page for Xataface. You can't do this with blocks or slots since nothing exists underneath that field. You can however do a workaround by using javascript and the DOM concept.

For example:


function block__after_login_form(){
<script language="javascript"><!--
var div = document.createElement('div');
div.className = 'formHelp';
var text = document.createTextNode('Please enter the correct password');
div.appendChild(text);

document.getElementById('Login-Password').appendChild(div);
//--></script>
}


First thing to notice is we are using the block__after_login_form() which simply a block on that template. We are using it to include our javascript of interest. We could have actually used any block.

var div = document.createElement('div');


The first thing we do is create a div element.

div.className = 'formHelp';


Here are just giving it the css class name of formHelp which gives it a nice styling feel. This is the same style used for widget:description values.

var text = document.createTextNode('Please enter the correct password');


And then we create a textnode which contains the text of interest.

div.appendChild(text);


With out div element, we are appending the text node we just created to it.

document.getElementById('Login-Password').appendChild(div);


And finally we add our div element to our element of interest which happens to have the id of "Login-Password" This id has been assigned by Xataface itself.

And if everything works out fine, then you should see the text appear below the password field!

You can find more information about how to manipulate DOM structure using javascript here.

Tuesday, July 8, 2008

Using smarty tags in a template

This is a tutorial to show how to specify some variables in a php class and then use them in the smarty template. Here is the situation: There is a below_header.html file needs to have a drop down menu which lists a bunch of offering_types as a classification of offerings.

The Dataface_Main_Template.html contains this code:


{block name="before_below_header"}
{define_slot name="below_header"}{/define_slot}
{block name="after_below_header"}


Notice the below_header slot. Since we want this slot to be replaced on every page of Dataface, we will use the function block__below_header() in the ApplicationDelegate.php file to replace this slot.

So the next step is to retrieve the variables that we will use to display in our below_header slot.

ApplicationDelegate.php


function block__below_header(){

//only broker dealers and broker customers should be able to see offerings
if (isBrokerDealer() or isBrokerCustomer()){
$offering_types = df_get_valuelist("offerings", "offering_types");
}

//any users can see the strategy types
$strategy_types = df_get_valuelist("strategies", "strategy_types");

$context = array('offering_types' => $offering_types, 'strategy_types' => $strategy_types);
df_display($context, 'below_header.html');
}


The if statements are just some fancy permission features where we only want like the offering_types to show up for broker dealers and customers. But the important lines are:


$offering_types = df_get_valuelist("offerings", "offering_types");

$strategy_types = df_get_valuelist("strategies", "strategy_types");

$context = array('offering_types' => $offering_types, 'strategy_types' => $strategy_types);
df_display($context, 'below_header.html');


Here we are using the df_get_valuelist() function in the dataface_public_api.php file which takes two parameters (tablename, valuelistname). And the valuelists are found in the valuelist.ini of the delegate classes:

valuelist.ini of offering/strategy tables
[strategy_types]
Managed Stocks = "Managed Stocks"
Managed Funds = "Managed Funds"
Managed ETFs = "Managed ETFs"
Managed Blend = "Managed Blend"
Managed Annuity = "Managed Annuity"
Currency = "Currency"
Derivatives = "Derivatives"
Commodities = "Commodities"
Unknown = "Unknown"

Nothing special here. Just a list of the strategy types. I didn't show the offering_types which is in the valuelists.ini of the offering table. But it's the same thing.

And so after we have retrieve the valuelist as variables, we place them into this context variable which we then send in as a parameter to the df_display function. The second parameter below_header.html is the file to display which will use the variables from the context variable.

below_header.html
The important code:


<select name="Offerings" onchange="window.location=this.options[this.selectedIndex].value">
{foreach from=$offering_types key=okey item=otype}
<option value="{$ENV.DATAFACE_SITE_HREF}?-table=offerings&offering_type={$otype}">{$okey}</option>
{/foreach}
</select>


The smarty tag foreach from is retrieving the offering_types variable which is an array (remember from the ApplicationDelegate.php file) and so we are getting the key and value (as item) and using them as the option value and as the labels.

One interesting thing is the onchange javascript event which automatically changes the page to the value of the option.

That's it!