Declarations

From KynetxDocs

Jump to: navigation, search

The pre block of a rule allows variables to be declared from data sources and counters. Any valid KRL expression can appear on the right hand side of a declaration. You can also declared variables that contain HTML using an extended literal.

Here's an example with a referer data source and a HTML declaration:

pre {
  keywords = referer:search_terms();

  announcement_1 = << 
<div id="kobj_announcement_1">
<p class="announcement">
This is some text!!! It's cool.  You searched on <strong>#{keywords}</strong> 
</p>
</div> 
  >>;

}

The expressions in the pre declaration block are executed in the KRL engine. The results are stored in variables that are passed as variables inside the JavaScript sent to the browser. The results of any declaration are always available in the browser as a variable with the name on the left-hand side of the declaration.

Contents

Built-in Data Sources

Datasource declarations take the following form:

<var> = <source>:<function>(<args>);

The following describes data sources available in KRL.

Weather

The <source> for weather is weather.

The following functions are available:

  • curr_temp - current temperature
  • curr_cond - current conditions
  • curr_cond_code - the Yahoo! code for the current condition
  • tomorrow_low - tomorrow's forecast low temperature
  • tomorrow_high - tomorrow's forecast high temperature
  • tomorrow_cond - tomorrow's forecast condition
  • tomorrow_cond_code - tomorrow's forecast Yahoo! condition code

None of these take arguments.

Location

The <source> for location is location.

The following functions are available:

  • country_code
  • country_name
  • region - this is the state in the US
  • city
  • postal_code - this is the zip code in the US
  • latitude
  • longitude
  • dma_code
  • area_code

None of these functions takes arguments.

Referer

The <source> for referer is referer.

The following functions are available:

  • search_terms - returns the search terms if the referer was a search engine.

None of these functions takes arguments.

Media Market

The <source> for media market is mediamarket.

The following functions are available:

  • name - media market name.
  • rank - rank in the US of your media market.
  • household - number of households with TVs in your market.

None of these functions takes arguments.

Stocks

The <source> for stock is stocks.

The following functions are available:

  • last
  • change
  • open
  • high
  • low
  • volume
  • previous_close
  • name

The argument to each of these functions is the stock symbol.

Page

The <source> for page data sources is page.

The following functions are available:

  • var - retrieve the value of a variable set on the page (see Page Variables for more information)
  • env - retrieve the value of the page environment

The argument to each of these functions is the page or environement variable name as a string.

UserAgent

The <source> for page data sources is useragent.

The following functions are available:

  • language - natural language version of language
  • language_code - ISO code (e.g. "en" for English) of language
  • browser_name - name of the browser (e.g. "Internet Explorer", "Firefox", etc.)
  • browser_version - complete version string
  • browser_version_major - part preceding "." in version string
  • browser_version_minor - part following "." in version string
  • os - operating system
  • os_type - general type of OS ("Linux")
  • os_version - version of OS


The argument to each of these functions is the page or environment variable name as a string.

User Defined Data Sources

As described in the for the global declarations, users can define data sources. They are queried in the pre declarations. For example, if a data source named library_search had been declared, it could be queried like so:

pre {
  book_data = datasource:library_search("q="+isbn);
}

Each of the parameters are joined into a string using a & and attached to the datasource URL before it is queried. The results of that query are cached as specified in the data source declaration.

You can also use a hash to specify the parameters, where each name/value pair in the hash is added to the query string as a name/value pair:

pre {
  book_data = datasource:library_search({"q":isbn});
}

If you intend to use the results of the query in a Javascript expression to be executed on the server, note that the value of the right hand side is stored in KOBJ['data'][lhs] where lhs is the left hand side of the declaration. Note that because a ruleset might execute with any other ruleset, variable names should be chosen to avoid name clashes.

Complex data can be queried and used using the pick operator.

Counters

Counters can be declared and manipulated to control the action of rules

HTML Declarations

HTML declarations are performed using extended quotes (see Literals). The HTML is contained between double angle brackets (<<...>>)

pre {
   announcement_1 = << 
<div id="kobj_announcement_1">
<p class="announcement">
This is some text!!! It's cool.  You searched on <strong>#{keywords}</strong> 
</p>
</div> 
   >>;
}

This declares a single variable named announcement_1 that contains the HTML between the double brackets.

Page IDs

You can use the contents of elements on the page with a specific CSS ID using the page ID delclaration. For example, the following comment will make the contents of the element with the ID itemnum available as the variable item_no.

item_no = page:id("itemnum")

Note that page IDs cannot be used in predicates since the values are not available on the server, but only on the client. They can be used in declarations and as arguments to actions.

Personal tools