Using PHP Code In Templates
Question
Can I use PHP code in my templates?
Answer
Movable Type templates generate standard HTML pages; so any coding language which can be used in a standard web page can also be included in your templates.
Here are some important tips to remember about using PHP in your templates:
No shortcut tags in dynamic templates
If you are using PHP in a dynamic template, you should not use the "shortcut" opening tag:
<?
Instead, you should use the full version:
<?php
Template Tags are processed differently in static templates than they are in dynamic ones.
In a static page, Template Tags are translated into textual values which can be placed in strings. So, using this code with static publishing:
<?php include('<MTEntryAuthor dirify="1">.html'); ?>
would result in something like this:
<?php include('melody_nelson.html'); ?>
Dynamic publishing translates Template Tags into PHP code, using Smarty. So, if you use this code:
<?php include('<MTEntryAuthor dirify="1">.html'); ?>
it would translate to something like this:
<?php include('<?php echo
smarty_modifier_dirify(smarty_function_Author()), "1"); ?>.html'); ?>
which makes no sense and would not compile. To get what you want, you should invoke the functional way to retrieve tag content. For example:
<?php
$author = dirify($this->tag('MTEntryAuthor'));
?>
Or without dirify:
<?php
$author = $this->tag('MTEntryAuthor');
print ($author);
?>
Then you could use the $author variable in an include or whatever way you want.
You can pass a Template Tag value to your PHP code in static pages, but not the other way around.
Your custom PHP code is processed by the browser when viewing a static page, not by the Movable Type system when building it. When Movable Type builds a static page, it will process the Template Tags, but cannot first read a PHP variable you may be trying to pass to that particular tag.
You can pass a Template Tag value to your PHP code in dynamic pages using Smarty.
Assign the values you need into the Smarty variable's variable space:
<?php
$this->assign('category', 'Movable Type Plugins');
Or copy a PHP variable in there:
$this->assign('category', $cat);
?>
Then, just refer to that variable as $variable_name in the Movable Type tag:
<MTEntries category="$category">
...
</MTEntries>
Reference:
Important: This information is included in our Knowledge Base as a courtesy to our users who wish to employ PHP code in their Movable Type Templates. Support staff is unable to provide detailed assistance with custom template code, including PHP and Smarty.


