douhuangjie4503 2013-07-30 15:29
浏览 396
已采纳

twig:创建调用函数的自定义标记

SETUP:

  • Twig 1.13.1
  • PHP 5.4.3

PROBLEM:

I am needing help setting up a custom tag that calls a function that i have already built...


Current Code:

Template Code

{% set stories = get_latest_stories(2, sports) %}

{% for story in stories %}
    {{ story.headline }} <br>
{% endfor %}

Controller

$function = new Twig_SimpleFunction('getViewStories', function (section, limit) {
    return news_stories::getStories(section,limit);
});
$twig->addFunction($function);
$twig->render("storyList.html");

GOAL:

No with that said I would like to use a custom tag like

{% get_latest_stories 2 sports %}

to call the same function as above. The new way looks nicer and is easier to follow

  • 写回答

2条回答 默认 最新

  • dongwei1895 2013-07-30 20:45
    关注

    Why not fetch your stories in the controller instead of the template? This does not seem like a job for the view layer...

    So, something like this:

    $twig->render("storyList.html", array(
        'stories' => news_stories::getStories($section, $limit)
    ));
    

    Then, you'll have a stories variable available in your template.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?