[英]php code in movable type page templates
我正在嘗試為使用Movable Type平台的公司寫一份聯系表格。 我正在創建頁面模板供他們使用。 我95%確定我的PHP代碼和我的html對於表是正確的並且相互引用 - 這不是問題的根源。 但MT不會自動呈現php。 相反,我得到了我所有的PHP文本的巨大塊。 html呈現得很好。 我不確定如何強迫MT識別php。 我找不到直接解決此問題的設置或任何內容。 我可以在MT幫助/地區找到最接近的是在這里 。 但我嘗試使用他們提供的代碼<$MTEntryText encode_php="here"$>
但它完全沒有改變頁面呈現的方式。 下面是我試圖使用的php,但我認為這不是問題的根源。 我想我應該包括它以防萬一。 我只是忽略了我需要為MT標記東西的方法嗎? 我是第一次使用Movable Type平台,第三次使用php,所以請在解釋我缺少的東西時隨意跟我說話。
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "blank@mydomain.com";
$email_subject = "Web Contact Response";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Movable Type逐字輸出模板中的文本,但MT模板標簽除外,它們會相應地進行處理。
例如,如果您的索引模板僅包含代碼:
<?php
<a href="<$mt:BlogURL$>"><$mt:BlogName$></a>
?>
它將在為包含文本的索引模板設置的發布路徑中生成文件:
<?php
<a href="http://blogurl.com">My Blog</a>
?>
如果發布的文件是通常通過PHP解析器運行的類型(例如.php
文件),則只有在用戶請求文件時,Web服務器才會解析.php
。 Movable Type在發布時不會觸及任何PHP代碼,也不會觸及MT模板標簽以外的任何其他內容。 其余的只是按原樣輸出。
根據您的問題,您似乎可能嘗試將上述代碼放在MT頁面的正文中,並在頁面模板上使用類似<$MTEntryText encode_php="here"$>
的MT標記。 您將代碼描述為在輸入時顯示在結果頁面上,這聽起來像我預期的那樣。 我猜你可能會在一個以.html
結尾的頁面上輸出這個PHP,因此PHP沒有解析,但我不知道你不知道輸入上述代碼的確切位置以及生成的模板的發布路徑是什么結果文件是。
如果是這種情況,您可以通過將模板發布路徑的擴展名更改為.php
來解決您的問題。 或者,如果您的模板自動使用系統擴展,您可能需要轉到博客設置“常規設置”頁面並將“存檔設置”下的“文件擴展名”更改為php
。
僅供參考, encode_php
修飾符適用於將數據插入PHP代碼時,如文檔示例所示: $the_title = '<$MTEntryTitle encode_php="q"$>';
。 這不適用於PageBody
或EntryBody
標簽,用於您打算運行的代碼的一般輸出,因為它可能最終會逃避您在頁面上輸入的代碼中的各種內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.