WP XML Template
This plugin offers simple and advanced mechanisms for embedding parameterised post snippets into posts. For the purposes of this plug-in a Wordpress Post-Template is simply a Wordpress post (or page) which embeds the values of its own custom variables within its html content by means of simple mark-up. The Wordpress post-template can then be embedded within another post, also by means of simple markup, with the option to override the custom variables differently each time the post template is embedded. Additionally custom variables, global variables (defined on the plug-in settings page) and external files can contain XML which can be iterated by the post-template. CSV files are also supported - they are simply converted to <row> and <col> tags and after that it is business as usual. Internally the post-template engine deals with XML (addressable by the power of XQuery). It's most important aspect is that it is hierarchical. Read on to see why this is such a useful feature. With this plugin you could create a post-template describing an embedded video or a mobile phone deal "box". As you will see below you can then repeatedly render it on some page (optionally) based on variables from an xml file. If you later decide to change the way it looks or you want to render it differently on another site then you only have to change the post-template instead of updating all instances of it on every page it occurs on. Likewise if you want to add more similar content you just have to update the xml file.
Example: Embedding the "Showcase" Post-Template
The "showcase" shown below is rendered using the following directives: {rewrite}[call(@showcase,class{Mobile Phones},brand{Apple})]{/rewrite} where the following parameters are passed to the "call" method,- one anonymous parameter which is being set to "@showcase" which is shorthand for "globals/showcase".The global variables are specified in the settings page of the plugin. On that page "showcase" contains the number of the post-template to embed - "55".
- The class of the devices to show. We are overriding "class" which is defined as a custom variable for post 55.
- The brand of the devices to show. We are overriding "brand" which is defined as a custom variable for post 55.
What's Inside the "Showcase" Post-Template?
The "showcase" post-template has four custom variables; style (a style sheet), devices (main xml data), class (device class) and brand (device brand). The post-template is defined as follows (html or text view):The "DealPost" Post-Template (Individual Deal Boxes)
<div class="child"> {rewrite} <div style="height: 44px;"><img style="float: left;" title="3 Store" src="http://three-stores.co.uk/wp-content/themes/iMobile/images/quickpick/3.jpg" alt="3 Store" width="16" height="16" border="0" /><strong><span style="font-family: Arial; color: navy; font-size: 15px;">[title]</span></strong></div> <div style="height: 86px;"><a style="text-decoration: none;" href="[link]" rel="nofollow" target="_blank"><img style="float: right;" title="3 Store [title]. [specs]" src="[image]" alt="" border="0" /></a>{rewrite:line lines/line}- [line] {/rewrite:line}</div> <div style="height: 38px;"> <span style="color: #00adef;"><strong>Only <span style="background: yellow;">£[monthly]</span> a month (x[months])</strong></span> <br /> <div style="background: #F7FFF7; color: #ff7f00; margin: 0px;"><center><a href="[link]" rel="nofollow">More Deals >></a></center></div> </div> {/rewrite} </div>
The best way to work with template designs in Wordpress' would be a combination of visual and html modes. You can also use an external html editor to design your deal box and then copy the html into your post in html mode.
The "dealpost" post-template has the following custom variables:- "image" - A link to the image to display.
- "lines" - The lines to display in the deal box.
- "link" - A link to the deal.
- "title" - The title of the deal.
- "monthly" - The monthly amount to pay.
- "months" - The number of months to pay.
- "specs" - The device's specifications.
Complete Syntax and Usage
The following definitions will give a clear understanding of the information that follows:- Definitions
- source text - Text that may include embedded template tags.
- variable - The name of a variable and optional xpath suffix.
- expression - Can be one of the following:
- a variable - e.g. "model"
- a number - e.g. "123"
- a method - e.g. "call(expression/assignment,assignment,...)"
- source text surrounded by "{" and "}"- e.g. "{Hello World}"
- template tag - Mostly [expression] but can also be {source text} or [assignment] (no output)
- assignment - A variable followed by template tag - e.g. variable[expression] or variable{source text}
{Rewrite} Directives
Everything between the {rewrite} and {/rewrite} directives is rewritten by the plugin, but a {rewrite} directive can also specify which portion of an xml document it is rendering when using the extended syntax {rewrite:var2 var1:xpath} for which the corresponding end-tag is {/rewrite:var2}. This indicates to the plugin that you want to iterate all the node identified by xpath, assuming var1 contains the xml document that you loaded previously. For each node found (var2) the template repeats the template replacements found between the two rewrite:var2 tags. See how this works in the examples above.Pre-Defined Methods Available
The following methods are available which deal with posts:- postraw(expression) - The raw post content of the post identified by expression (post id) is returned.
- rewrite(expression, assignment, assignment, ...) - The (result of) expression is rewritten using the inputs.
- call(expression, assignment, assignment, ...) - The post identified by expression is rewritten using the inputs.
- ucwords(expression) - Upper cases the first letter of every word in a string.
- ucfirst(expression) - Upper cases the first letter of a string.
- setcookies(expression, assignment, ...) - Sets cookies according to assignments. Expression is the expiry (ms).
- loadxml(expression) - Loads XML from file/url/string. Assumed to be string if starts with "<".
- savexml(expression[, file]) - Outputs expression as xml text. Optional file{filename} or file[expression] parameter.
- loadcsv(expression) - Similar to "loadxml" but makes csv rows and column into xml <row> and <col> elements.
- replace(expression,search,replace) - Searches expression for search expr and replaces it with replace expr.
Pre-defined Global Values
The following variables deal with variables on the plugin settings page, cookies, request and referrer arguments- globals - This is an XML variable that can be used with an xpath suffix. It contains:
- All the variables defined on the plugins settings page (accessible as "globals/variable" or "@variable")
- @request - The page arguments - e.g. for "?x=1", @request/@x would return "1".
- @referrer - The referrer arguments - e.g. for "?q=mobile", @referrer/@q would return "mobile".
- @cookies - The http cookies as elements - e.g. "@cookies/tid" gives the value of cookie named "tid".
- @single = Has a value of "1" for single-post pages and "0" otherwise - e.g. "1"
- @url - The page URL (permalink) - e.g. "http://www.wpgallery.co.uk/wordpress-post-template/?"
- @ip - The client's IP address - e.g. "54.224.79.93"
- @cbip - IP address with "." replaced with "o" - e.g. "54o224o79o93"
- @id - The post's id - e.g. "4"
- lcrl = "{"
- rcrl = "}"
- lsqb = "["
- rsqb = "]"
- pipe = "|"
- amps = "&"
User-Defined Variables and Functions
You can define your own PHP function for use in the templates. Simply add a file called 'xmlt-custom.php' to the plugin's folder. Here is a simple example of what would go in the file:<?php
function xml_template_myfunc(&$vars)
{
return "This is what myfunc is returning";
}
function xml_template_customfunctions(&$vars)
{
$vars['myvar'] = "This is the value of myvar";
$vars['myfunc()'] = "xml_template_myfunc";
}
?>
How do I install the Wordpress Post-Template plugin?
- Unzip the zip file into your Wordpress ".../wp-content/plugins" folder.
- "Activate" the plugin on the dashboard "Plugins" page.
