douqian3712 2014-10-18 15:07
浏览 28
已采纳

Chunk看不到论点

I have this snippet (I want to get elements from .xml file):

$movies = simplexml_load_file('http://www.example.com/example.xml');
$out = "";

foreach ($movies as $movie) {
    $properties = array(
        'photo'   => $movie->image,
        'title'   => $movie->title,
        'desc'    => $movie->teaser,
        'channel' => $movie->channel,
        'date'    => $movie->date);

    $out .= $modx->getChunk('tpl_movies-item', $properties);
}

return $out;

And chunk tpl_movies-item:

<article>
    [[+photo]]
    [[+title]]
    [[+desc]]
    [[+date]]
    [[+channel]]
    aaa
</article>

It shows only "aaa" (but when I add "echo $properties['photo']" it prints right value), when I change values in array to strings, eg. 'desc' => "lololololol" it works right. Could u help me what should I do?

  • 写回答

1条回答 默认 最新

  • dt3674 2014-10-18 15:15
    关注

    The problem is probably in your foreach statement since simplexml_load_file() needs ->children() to loop through the different childs of the object at hand. Try:

    foreach ($movies->children() as $movie) {
        $properties = array(
            'photo'   => (string)$movie->image,
            'title'   => (string)$movie->title,
            'desc'    => (string)$movie->teaser,
            'channel' => (string)$movie->channel,
            'date'    => (string)$movie->date);
    
        $out .= $modx->getChunk('tpl_movies-item', $properties);
    }
    

    UPDATE

    Try casting the properties as string as in the example above since they could be returned as objects.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计