【问题标题】:php code in movable type page templates活字页面模板中的php代码
【发布时间】:2013-12-12 07:08:34
【问题描述】:

我正在尝试为一家使用 Movable Type 平台的公司编写联系表格。我正在创建页面模板供他们使用。我 95% 确信我的 php 代码和我的 html 对于表格是正确的并且相互引用——这不是问题的根源。但是 MT 不会自动渲染 php。相反,我得到了一大块我所有的 php 文本。 html 渲染得很好。我不确定如何强制 MT 识别 php。我找不到直接解决此问题的设置或任何内容。我在 MT 帮助/常见问题解答区域中找到的最接近的是 here。但我已经尝试使用他们提供的代码<$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 
}
?>

【问题讨论】:

    标签: php movabletype


    【解决方案1】:

    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 页面的正文中,并在页面模板上使用像 &lt;$MTEntryText encode_php="here"$&gt; 这样的 MT 标签。您将代码描述为在输入时出现在结果页面上,这听起来符合我的预期。我猜您可能会在以.html 结尾的页面上输出此 PHP,因此 PHP 不会解析,但如果不知道您在哪里输入上述代码以及生成的模板的发布路径是什么,我无法确定结果文件是。

    如果是这种情况,您只需将模板发布路径的扩展名更改为 .php 即可解决您的问题。或者如果您的模板自动使用系统扩展名,您可能需要进入博客设置“常规设置”页面,将“存档设置”下的“文件扩展名”更改为php

    仅供参考,encode_php 修饰符旨在将数据插入 PHP 代码时使用,如文档示例所示:$the_title = '&lt;$MTEntryTitle encode_php="q"$&gt;';。这不适用于 PageBodyEntryBody 标记以用于您打算运行的代码的一般输出,因为它可能最终会转义您在页面上输入的代码中的各种内容。

    【讨论】:

    • 您猜对了我将 php 和 .html 文件扩展名放在哪里。我要下班了,但我明天会先试试你的解决方法,看看会发生什么。
    猜你喜欢
    • 1970-01-01
    • 2014-08-30
    • 2018-10-11
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多