树枝:将变量从视图传递到控制器 [英] twig: pass variables from view to controller

查看:27
本文介绍了树枝:将变量从视图传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:

  • 树枝 1.13.1
  • PHP 5.4.3

问题:

我的数据库中有 10,000 篇文章.我需要一种方法来只允许 X 数量的故事显示在页面上.我知道我可以在调用模板之前限制控制器中的数量,但该数量会根据所使用的模板而有所不同.我将有一个控制器来处理所有文章.我需要一种方法将数字从模板传递到控制器以限制数组.我不想拉下所有 10,000 篇文章然后使用树枝切片"过滤器/功能.

I have 10,000 articles in my DB. I need a way to only allow X amount of stories to display on the page. I know I can limit the number in the controller before i call the template but that number will be different depending on the template that is used. I will have one controller to handles all the articles. I need a way to pass the number from the template to the controller to limit the array. I don't want to pull down all 10,000 articles then use twig "slice" filter/func.

我知道在 django 中你可以使用下面的.这只会加载前 3 个故事.

I know in django you can use the below. That will only load the top 3 stories.

{% get_latest_stories 3 sports as story_list %}
{% for story in story_list %}
    {{ story.title }}
{% endfor %}

这是我当前的文件.

控制器

<?php
$stories = news_stories::getStories("sports",5); //getStories(section,limit);
?>

<?=$twig->render("storyList.html", array('stories' => $stories))?>

视图/模板

{% for story in story_list %}
    {{ story.title }}
{% endfor %}

<小时>

总结

我想要一种将数字从模板传递给控制器​​的方法,以便我可以限制从数据库返回的行数

I would like a way to pass a number from the template to the controller so that i can limit the about of rows returned from the DB

推荐答案

从逻辑上讲,视图不可能将某些东西传递给控制器​​,因为视图是在堆栈末尾处理的,在其他所有事情之后.

Logically speaking, it would be impossible for the view to pass something to controller since the view is being processed at the end of the stack, after everything else.

但是,您可以将函数传递到视图中.您可能想要创建某种可以从树枝模板访问的 getViewStories 函数.由于您的控制器中已经有了它:

You can however, pass a function into the view. You would want to create some sort of getViewStories function that you can access from your twig template. Since you have this already in your controller:

<?php
$stories = news_stories::getStories("sports",5); //getStories(section,limit);
?>

<?=$twig->render("storyList.html", array('stories' => $stories))?>

你需要做的就是稍微改变一下,像这样:

All you would need to do is change it around a bit, like this:

<?php
$function = new Twig_SimpleFunction('getViewStories', function (section, limit) {
    return news_stories::getStories(section,limit);
});
$twig->addFunction($function);
?>

<?=$twig->render("storyList.html")?>

现在,您可以从模板内部调用此函数,如下所示:

Now, from inside your template, you can call this function, like so:

{% set story_list = getViewStories('sports',5) %}

{% for story in story_list %}
    {{ story.title }}
{% endfor %}

并更改每个模板中的 getViewStories 参数.

And change the getViewStories parameters around in each template.

虽然您可以使用切片过滤器,但我建议您不要使用它,因为它会导致不必要的长时间数据库调用.这是最优化的方法(我知道).

And while you can use the slice filter, I would recommend against it in your case, as it makes for unnecessarily long database calls. This is the most optimized method (that I'm aware of).

这篇关于树枝:将变量从视图传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆