I want to use few php string functions in twig. For e.g.
How to write below code in twing?
if (!empty($message) && substr_count($message, 'blabla')) {
....
....
}
You can't, and that's the whole point of twig.
Template should only take care of displaying data.
In that specific case, you could create a TWIG variable, and simply pass TRUE or FALSE, so that you twig code would look like:
{% if display_message %}
...
{% enfif %}
You can look at the list of expressions that are available in the twig template, strstr_count is not part of them :( http://twig.sensiolabs.org/doc/templates.html#expressions
Use filters:
{% if message is not empty and ... %}
...
{% endif %}
I think there is not a substr_count equivalent for twig, you can either make the test and pass the result to the template or create a twig extension and implement it yourself
http://twig.sensiolabs.org/documentation
Related
I need help in including in a twig file, another twig file
For example, I have:
_cdn/widgets/filter/filter.php
_cdn/widgets/filter/view/filter.twig
And:
themes/tema02/view/search.twig
themes/tema02/search.php
I need to include the filter.twig in the search.twig
But when I use include, it does not render the filter.php
And it gets a lot of 'Undefined variable:'
So how do I do this?
First of all show your code . It's always easier to give right anwser when you see the code.
Use {% include %} for including twig in twig
https://twig.symfony.com/doc/2.x/tags/include.html
Based on your description you need to pass requred variables to included twig , something like this
{% include 'filter.twig' with {'foo': 'bar'} %}
OR
In filter twig you can put if ... is defined where you try to access variable
{% if foo is defined %}
code where you doing something with foo variable
{% endif %}
I would like to return a specific string each time a string is used in {% trans %}.
Exemple :
{% trans from 'base' %}home.title{% endtrans %}
Returns : "Home"
I would like it to return "XXX" and to automatically do it.
I used to do this in some pure PHP projects but I just don't know how to do it in Symfony.
Its unclear how you decide XXX. Whatever you did with pure PHP is doable with twig by means for twig filters. you can do something like home.title|trans_base and implement trans_base filter. See document under Filters section here
I need to load a page, that will be "inserted" in a template - as I read it, Volt's Template Inheritance should do the trick and it does... kinda. Hardcoded values, as shown in the examples, work fine - the following example works:
<!-- Template -->
<div id="site_content">
{% block test %}
{% endblock %}
</div>
and the page, that inherits the template:
{% extends "../../templates/de/index.volt" %}
{% block test %}
{{ content() }} {# this is a registered volt function that outputs the generated content #}
{% endblock %}
However, the same page might need to inherit a different template and that must be decided on runtime, so the name of the template must be generated dynamically. Two options occurred to me:
Set the template name to a variable and use it when extending - the problem here is that I don't see a way to use it afterwards. That guy seems to have had the same problem, but there is neither an answer of how to do it, nor a confirmation that it isn't possible at all.
Register another function to generate the complete string (e.g. {% extends "../../templates/de/index.volt" %}) and then compile it, e.g.
$compiler->addFunction('get_template',
function ($resolvedArgs, $exprArgs) use ($volt) {
return $volt->getCompiler()
->compileString('{% extends "../../templates/de/index.volt" %}');
});
and then use that function in the page, e.g.
{{ get_template() }}
{% block test %}
{{ content() }}
{% endblock %}
However, using that approach does not parse the page content (e.g. the content returned by the registered content() function is not shown). I'm also open to other solutions (using Twig instead of Volt is only a last resort, for performance issues), advices of what I'm doing wrong or pointers of useful articles on the topic. Thanks in advance!
Try using partials as documented in the Phalcon doc: Using Partials
Instead of doing this rendering of each slide in my TWIG like this (see line 6):
{# loop out the slides #}
{% for c in contents %}
{% set i=i+1 %} {# increase slide number #}
<div id="slide{{ i }}" class="slide" style="z-index:{{ i }};">
{# the slide itself, rendered by it's own template #}
{% include 'BizTVArchiveBundle:ContentTemplate:'~c.template~'/view.html.twig' with {'contents': c, 'ordernumber': i} %}
</div>
{% endfor %}
...Instead I would like to keep all of that logic in the controller, to just deliver to the view an array of ready slides. How can I do something like this (see line 9):
//do stuff...
foreach ($container->getContent() as $c) {
$content[$i]['title'] = $c->getTitle();
$content[$i]['sort'] = $c->getSortOrder();
$content[$i]['data'] = $c->getData();
$content[$i]['template'] = $c->getTemplate()->getId();
$content[$i]['duration'] = $this->extract_seconds($c);
$content[$i]['html'] = $this->render('BizTVArchiveBundle:ContentTemplate:'.$content[$i]['template'].'/view.html.twig', array(
'contents'=> $content[$i],
'ordernumber' => 99,
));
}
All it gives me at the moment (the contents of $content[$i]['html']) is
{"headers":{}}
Within a twig template, you can set the value of a printed variable like this:
{% set rendered %}
{{ var_to_print }}
{% endset %}
You can get content form rendered object like this:
$this->render('BizTVArchiveBundle:ContentTemplate:Some.html.twig', array())->getContent();
It's better if you include the template in the template, this way you keep the templating rendering in the view layer and the logic in the controller layer.
But well, if you really want it...
You can use 2 services to do that: twig is using the Twig_Environment or templating.engine.twig which is a templating layer build in Symfony2 for Twig, this can be easily switched ot templating.engine.php.
If you use the twig service, you can see the twig docs on how to use it:
$template = $this->get('twig')->render('/full/path/to/Resources/views/'.$content[$i]['template'].'/view.html.twig', array(...));
If you use the templating.engine.twig service, see the templating docs on how to use it, which is almost exact the same as the Twig_Environment.
That is because $this->render() returns a Response object.
Instead of $this->render(), use $this->renderView().
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.