简体   繁体   中英

twig: pass variables from view to controller

Setup:

  • Twig 1.13.1
  • PHP 5.4.3

Problem:

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.

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 %}

Here is my current files.

Controller

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

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

View/Template

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

Summary

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.

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 %}

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).

You want to use the slice filter I think this should work for you

http://twig.sensiolabs.org/doc/filters/slice.html

{% for story in story_list|slice(1,5) %}
    {{ story.title }}
{% endfor %}

should only return the elements 1 - > 5 of the loop then break loop. You can also do it like this

{% for story in story_list|[start:5] %}
    {{ story.title }}
{% endfor %}

Disclaimer: I've never actually used twig though this was just a quick browse through its docs

You can embedded controllers ( or render other urls ) from inside a twig template. This means you could have a main layout template for your site, and keep your storyList.html template very plain - just to iterate over the stories and any markup they might need.

In your main layout you would render the action for the news stories:

<div id="stories">
    {% render url('...') with { section: 'sports', limit: 5}, {'standalone': 'js'} %}
</div>

This way requires hindclude.js included on your page. Check these docs . If you are also using symfony ( you mention MVC but not the framework ) - even better. Scroll up a bit and look at Embedded controllers.

Otherwise, I believe this way essentially uses ajax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM