This article details some ideas about how to use Smarty when you are working with a Filemaker Pro (FMPro) database.

(News: 04-09-04 Filemaker has announced that they will no longer support CDML in their product line. However, this information still applies and can be used, but it is an evolutionary dead end. My advice is not to use the CDML plugin. All development has ceased. The other filemaker information here still applies. So read on.)

(News: 01-16-03 I've just finished a Smarty plugin TheCDMLPlugin that lets you put CDML directly in your smarty template pages.)

Filemaker Pro is a database program that already has a template language called CDML (Claris Data Markup Language - named for the old name of the company). This article talks about general Filemaker-Smarty stuff and details how to incorporate CDML directly into Smarty.

Now, some people might tell me to use a database abstraction layer like ADODB, and they are right, but no one has written an extension to account for Filmaker Pro. The closest thing is FX.php (see below for link) as it has some abstraction functions that help development.

Also, I should note that much of my learning about how to talk to Filemaker over the web is based on a great book from The Moyer Group: Bowers and Lane's 'Advanced Filemaker Pro 6 Web Development' by Wordware pub ( http://www.moyergroup.com/publish_book.php ). The book covers a lot of ground with a ton of useful examples: IWP, CDML, Lasso, PHP, FX.php, XML/XSLT, XML-RPC, SOAP.

So you want to publish data from a Filemaker ( http://www.filemaker.com ) database on the web. The following information will help you streamline your efforts to publish data but will not help immensely if you also want to be able to edit your Filemaker Pro data. Either way, the choice is yours.

Here’s the choices...

Instant Web Publishing: You tried Filemaker’s Instant Web Publishing but it was too limited and sometimes the graphics didn’t work out the way they were arranged in the layout mode.

CDML: You built a site or two using CDML template files. It’s pretty comprehensive template language custom-made for FMPro and it’s easy to understand. But, the limitations of CDML are pretty severe, specifically: only 9 tokens to use as variables, no math support, no repeat loops, no debugger, all db queries passed in the browser addressbar (passwords too!) in GET requests, etc... CDMLFormatFiles

PHP ( http://www.php.net ): But, PHP itself is pretty hard to handle raw after coming from a nice, clean template language like CDML and a friendly database like FilemakerPro?. You have to learn curl (or sockets) in order to even get data out of FMPro.

FX.php ( http://www.iviking.org ): After playing around with PHP, you stumble on FX.php. FX.php is a class that allows you to get a handle on Filemaker’s XML exported data. It makes creating database queries a lot more straightforward and it turns the inscrutable FMP_XML output into PHP arrays. But, the arrays it outputs are a little strange so you spend a lot of time twisting the arrays inside out and all around using PHP.

XSL: not even going to try to come to grips with this chimera. Its strange syntax throws me off.

Smarty ( http://smarty.php.net ): Eventually, after looking into a few PHP template engines, you somehow stumble upon Smarty. Smarty makes a great companion to FX.php because you can pass Smarty variable single values and variable arrays and Smarty helps you do all the great stuff that Smarty is great at (the best in my view is the very flexible caching system). FX.php grabs data from Filemaker, puts it into a PHP array, you massage the arrays and assign the data to Smarty template variables and viola! You’re done.

So, that pretty much sums up what my journey was like. Let me stress here that my application called for only display, not editing. FX.php will be invaluable if you have to edit the data in FMPro databases.

Now, if you are just transitioning from CDML to PHP and Smarty and you don’t need to edit records via a web interface, don’t try to use FX.php, just use Smarty and CDML together. It took me a few weeks and one application to figure this out. I realize there are a lot of good things in FX.php and eventually I intend to write a Smarty plugin that will use FX.php but, for an easy transition from CDML, my advice is to leave FX.php out of the loop.

Using Smarty and CDML together:

Use Smarty’s fetch function to call in CDML format files. Yup, that’s it! That’s the big insight this month. Oh yes, there’s one other thing, the fetch template function has an undocumented set of parameters (as of 01-08-2004), and you will need two of them: user and pass.

Here’s the syntax for the fetch command when you are asking for a protected resource (this example shows a simple find request and uses undocumented user and pass params):

{* fetch file=”http://127.0.0.1:591/FMPro?-db=dbname.fp5&-format=CDMLformatfile.html&-lay=yourlayout&fieldToSearch=`$smarty.get.x`&-max=1&-find” user=”who” pass=”blur” *}

You can also do this directly in PHP itself by calling the file_get_contents() function and embedding the user and password in the URL.

// first dynamically write your CDML snippet to the folder where FMP expects it.
// then call the format file using this style URL
echo file_get_contents(”http://user:pass@127.0.0.1:591/FMPro?-db=dbname.fp5&-format=CDMLformatfile.html&-lay=yourlayout&fieldToSearch=dataToSearchOn&-max=1&-find”);

This example assumes your FMPro database is running locally to the php/smarty script ( that is, localhost or 127.0.0.1 ) and that the web companion is set up to listen for requests on port 591.

To use it, replace “dbname.fp5” with the name of your database. Replace “CDMLformatfile.html” with the name of your CDML file (and make sure that Filemaker can see it – version 6 of FMPro uses a directory called cdml_format_files by default). Replace “yourlayout” with the name of your layout and make sure it is where the field you are searching on shows. Replace “fieldToSearch” with the field you want to search via a GET query. Finally, create your query. It should have the following syntax:

http://www.domain.com/path/to/index.php?x=datatofind

the x=datatofind is the GET query that will be passed to the smarty template as the smarty variable $smarty.get.x Note that the smarty.get.x is enclosed in backticks ``. The backticks tell smarty that you want it to treat the thing as a template variable. Replace datatofind with some data that will actually be in your database in the field you put in “fieldToSearch”. Finally, the max=1 in the fetch file parameter indicates that we only want to see one record returned from FMPro. If you want to see more records, make sure your CDML file is set up to handle multiple records or grab the data from a portal and CDML format file set up to handle that portal (see below).

The way I think about using CDML format files and fetch is that the CDML files are “blocks” and I am combining the blocks to make a page. So, none of the CDML format files has an <html> tag, that and the head and body tags belong in the Smarty .tpl file.

CDML format files are a great way to present “portal” data (portal in the FMPro sense of data from a related file). Just make a CDML format file that looks like this:

[FMP-Portal: relationshipname] 
        [FMP-field: relationshipname::relatedfield]
        [FMP-field: relationshipname::anotherrelatedfield]
[/FMP-Portal]

Now, when you fetch this file in a Smarty template, you will get the data from the two fields in the portal. It’s a pretty easy way to get pre-formatted data without too much PHP involved. Of course you still need the initial PHP file (index.php for example) that creates the smarty object and calls the .tpl file to display, but all your finding, formatting and field accessing happens in CDML or the CDML query string you pass to FMPro via Smarty.

I suggest this technique for Smarty-FMPro developers who:

have reached the limits of CDML and don’t want to buy into Lasso (http://www.blueworld.com ) but who also dread the prospect of entirely switching from CDML to PHP

have no need to edit data via a browser but simply want to display it.