【问题标题】:Templating a Snippet with $modx->getChunk returns nothing使用 $modx->getChunk 模板化片段不返回任何内容
【发布时间】:2014-07-22 09:11:28
【问题描述】:

您好,我对这个输出感到困惑,请提出问题所在:

$responsexml = @simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
foreach ($responsexml as $hotelresponse)
 {
    $hotelname = ($hotelresponse->HotelInfo->Name);
    $hoteldestination = ($hotelresponse->HotelInfo->Destination->Name);
    $hotelcategory = ($hotelresponse->HotelInfo->Category);
    $hotelimage = ($hotelresponse->HotelInfo->ImageList->Image[0]->Url);

    $returnedoutput .= $modx->getChunk('_testxmlchunk',array(
        'hotel' => $hotelname,
        'destination' => $hoteldestination,
        'category' => $hotelcategory,
        'image' => $hotelimage,
    ));
      }

return $returnedoutput;

_testxmlchunk 块模板:

<li>this is <b>[[+hotel]]</b> is in <b>[[+destination]]</b> category <b>[[+category]]</b> pic <img src="[[+image]]"/></li>

它只返回空白块模板

  • 这是类别图片
  • 但是当我执行以下操作时......它可以工作

    $responsexml = @simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
    foreach ($responsexml as $hotelresponse)
     {
        $hotelname = ($hotelresponse->HotelInfo->Name);
        $hoteldestination = ($hotelresponse->HotelInfo->Destination->Name);
        $hotelcategory = ($hotelresponse->HotelInfo->Category);
        $hotelimage = ($hotelresponse->HotelInfo->ImageList->Image[0]->Url);
    
        $returnedoutput .= $hotelname.' '.$hotelimage.' '. $hoteldestination.' '.$hotelcategory.';
         }
    
    return $returnedoutput;
    

    任何建议...我错过了什么吗?我想模板化我的 sn-p。

    我使用: MODX 革命 2.2.10-pl(高级) mysql 5.5.37-35.0 PHP 版本 5.4.29

    【问题讨论】:

      标签: php modx modx-revolution


      【解决方案1】:

      我找到了解决方案(已解决): 我应该在之前设置占位符

      $returnedoutput .= $modx->getChunk('_testxmlchunk',array(
              'hotel' => $hotelname,
              'destination' => $hoteldestination,
              'category' => $hotelcategory,
              'image' => $hotelimage,
          ));
      

      或者另一种方法是将对象变量类型转换为字符串而不设置占位符:

      $returnedoutput .= $modx->getChunk('_testxmlchunk',array(
      'hotel' => (string)$hotelname,
      'destination' => (string)$hoteldestination,
      'category' => (string)$hotelcategory,
      'image' => (string)$hotelimage,
      ));
      

      这是正确答案

      $responsexml = @simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
      foreach ($responsexml as $hotelresponse)
       {
          $hotelname = ($hotelresponse->HotelInfo->Name);
          $hoteldestination = ($hotelresponse->HotelInfo->Destination->Name);
          $hotelcategory = ($hotelresponse->HotelInfo->Category);
          $hotelimage = ($hotelresponse->HotelInfo->ImageList->Image[0]->Url);
      
      
          $modx->setPlaceholder('hotel',$hotelname);
          $modx->setPlaceholder('destination',$hoteldestination);
          $modx->setPlaceholder('category',$hotelcategory);
          $modx->setPlaceholder('image',$hotelimage);
      
          $returnedoutput .= $modx->getChunk('_testxmlchunk',array(
              'hotel' => $hotelname,
              'destination' => $hoteldestination,
              'category' => $hotelcategory,
              'image' => $hotelimage,
          ));
      
      
        }
      return $returnedoutput;
      

      这里是正确答案2(备选)

      $responsexml = @simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
      foreach ($responsexml as $hotelresponse)
       {
          $hotelname = ($hotelresponse->HotelInfo->Name);
          $hoteldestination = ($hotelresponse->HotelInfo->Destination->Name);
          $hotelcategory = ($hotelresponse->HotelInfo->Category);
          $hotelimage = ($hotelresponse->HotelInfo->ImageList->Image[0]->Url);
      
                  /***
                   cast the xml object into string, $hotelname variable type is object, you should convert it to string
                  ***/
          $returnedoutput .= $modx->getChunk('_testxmlchunk',array(
              'hotel' => (string)$hotelname,
              'destination' => (string)$hoteldestination,
              'category' => (string)$hotelcategory,
              'image' => (string)$hotelimage,
          ));
      
      
        }
      return $returnedoutput;
      

      【讨论】:

      • 这可能行得通,但这并不是真正的正确方法。正如您已经完成的那样,只需将占位符数组传递给getChunk() 就可以了。请参阅下面的答案。
      【解决方案2】:

      无需调用setPlaceholder,您应该可以将占位符直接传递给您已经尝试过的块。

      我不确定为什么要将 $hotelresponse 属性设置为变量,或者为什么将属性本身包含在括号中。但是您的代码可以简化为以下内容:

      $returnedoutput = '';
      
      $responsexml = @simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
      foreach ($responsexml as $hotelresponse) {
          $returnedoutput .= $modx->getChunk('_testxmlchunk', array(
              'hotel' => $hotelresponse->HotelInfo->Name,
              'destination' => $hotelresponse->HotelInfo->Destination->Name,
              'category' => $hotelresponse->HotelInfo->Category,
              'image' => $hotelresponse->HotelInfo->ImageList->Image[0]->Url,
          ));
      }
      
      return $returnedoutput;
      

      如果HotelInfo 对象有效,我希望它可以工作。

      【讨论】:

      • 不幸的是这不起作用..我发现我应该设置占位符或者我应该将对象转换为字符串,例如:'hotel' => $hotelresponse->(string)HotelInfo->Name ,.....
      • 我坚持这应该做的方式,不需要设置占位符。 var_dump() 您的 HotelInfo 对象以查看属性是如何转换的,如果需要,您可以像这样转换为字符串:'hotel' =&gt; (string) $hotelresponse-&gt;HotelInfo-&gt;Name
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2018-10-02
      • 2015-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多