How to use php code in a template in MovableType 5?
How do I embed/execute php开发者_JS百科 code in a MovableType 5 template?
Note that the following only applies to blogs using static publishing.
Anything you write within an MT template, other than template tags themselves, will be published to the static file it creates. Based on that we can establish that if your template contains <?php echo 'Hello World'; ?>
, that exact code will be placed in the final file that is accessed by the end user. If the file extension is .php
or your server has been configured to allow the file to execute PHP (such as with .html
), when the user visits the page the PHP code will be evaluated and the results returned to the user as part of the page.
The exception to this is when using dynamic response templates, such as "Search Results" or "Comment Response". These templates are rendered directly from a Perl .cgi
script (mt-search.cgi
and mt-comment.cgi
, respectively) and therefore will not interpret PHP at all. The link in Pekka's answer shows a method you can use to get around this (Using PHP inside Movable Type's Search Template, for your convenience).
Furthermore, because MT builds files statically and processes the template tags when creating the file, you can mix the template tags in with your PHP code to dynamically change the end PHP code.
<?php
$blogURL = '<$mt:BlogURL$>';
$entryTitles = array();
<mt:Entries lastn="4">
$entryTitles[] = '<$mt:EntryTitle encode_php="q"$>';
</mt:Entries>
?>
May output the following PHP code:
<?php
$blogURL = 'http://example.com/';
$entryTitles = array();
$entryTitles[] = 'Title 1';
$entryTitles[] = 'Title 2';
$entryTitles[] = 'Title 3';
$entryTitles[] = 'Title 4';
?>
I recommend you read up on the encode_php template tag modifier before you attempt to mix your MT and PHP codebases.
One approach is here:
Using PHP inside Movable Type's Search Template
精彩评论